千家信息网

redis安装及简单应用

发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,1.系统环境:centos7.2 -- x86_64[root@salt1 ~]# cat /etc/centos-releaseCentOS Linux release 7.2.1511 (Core
千家信息网最后更新 2025年11月08日redis安装及简单应用


1.系统环境:centos7.2 -- x86_64

[root@salt1 ~]# cat /etc/centos-release

CentOS Linux release 7.2.1511 (Core)

[root@salt1 ~]# uname -a

Linux salt1 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux



2.安装redis

先决条件:首先安装epel-release(epel源)

[root@salt1 ~]# yum -y install epel-release

然后安装redis

[root@salt1 ~]# yum -y install redis

[root@salt1 ~]# rpm -q redis

redis-2.8.19-2.el7.x86_64

[root@salt1 ~]# redis-

redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server redis-shutdown


redis-server:redis服务器的daemon启动程序

redis-cli: redis命令行客户端操作工具,当让也可以用telnet根据其纯文本协议来操作;

redis-benchmark:redis性能测试工具,测试redis在你的系统及你的配置下的读写性能;

redis-check-aof:对于更新日志appendonly.conf检查,是否可用,类似检查mysql binlog的工具;

redis-check-dump:用于本地数据库rdb文件的检查;



3.启动redis服务

先备份一下redis的配置文件(个人习惯,先备份再操作)

[root@salt1 ~]# cp /etc/redis.conf{,.bak}

redis启动时最好指定其配置文件,因为redis几乎所有的控制都在其配置文件;

redis-server : redis服务器的daemon启动程序

/etc/redis.conf : redis的配置文件

& : 后台启动

[root@salt1 ~]# redis-server /etc/redis.conf &

[1] 9462

[root@salt1 ~]# ps -ef | grep redis

root 9462 2767 0 03:59 pts/0 00:00:00 redis-server 127.0.0.1:6379

root 9466 2767 0 03:59 pts/0 00:00:00 grep --color=auto redis

然后再看下日志

[root@salt1 ~]# tail -30 /var/log/redis/redis.log

[9462] 11 Aug 03:59:54.037 * Increased maximum number of open files to 10032 (it was originally set to 1024).

_._

_.-``__ ''-._

_.-`` `. `_. ''-._ Redis 2.8.19 (00000000/0) 64 bit

.-`` .-```. ```\/ _.,_ ''-._

( ' , .-` | `, ) Running in stand alone mode

|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379

| `-._ `._ / _.-' | PID: 9462

`-._ `-._ `-./ _.-' _.-'

|`-._`-._ `-.__.-' _.-'_.-'|

| `-._`-._ _.-'_.-' | http://redis.io

`-._ `-._`-.__.-'_.-' _.-'

|`-._`-._ `-.__.-' _.-'_.-'|

| `-._`-._ _.-'_.-' |

`-._ `-._`-.__.-'_.-' _.-'

`-._ `-.__.-' _.-'

`-._ _.-'

`-.__.-'

[9462] 11 Aug 03:59:54.038 # Server started, Redis version 2.8.19

[9462] 11 Aug 03:59:54.039 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[9462] 11 Aug 03:59:54.039 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

[9462] 11 Aug 03:59:54.040 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

[9462] 11 Aug 03:59:54.040 * The server is now ready to accept connections on port 6379


×××字体为:问题

红色字体为:解决办法



×××字体解释:

1.后台保存在低内存下可能会失败

vm.overcommit_memory 参数

默认值为0

0:当用户空间请求更多的内存时,内核尝试估算出剩余可用的内存;

1:内核允许超量使用内存,直到用完为止;主要用于科学计算;

2:内核会使用一个绝不过量使用内存的算法,即系统整个内存地址空间不能超过swap+50%的ram值,50%参数的设定是在overcommit_ratio中设定;

2.启用了THP(页面内存透明化)在你的内核具体解释看 后边的网址,里头解释的更详细 http://os.51cto.com/art/201103/249821.htm

3.tcp backlog 设置为511 不能执行,原因是/proc/sys/net/core/somaxconn的值较低

backlog 是网络连接过程中,某种状态的队列长度,如果并发高,那么会导致backlog的队列被占满,服务器就会丢掉传进来的其他连接,然后就会出现客户点连接失败的情形;

http://jingyan.baidu.com/article/84b4f565e60f8560f6da3227.html


所以底下就开始执行对应的操作

注意:蓝色字体为需要注意的地方

echo "vm.overcommit_memory = 1" >> /etc/sysctl.confsysctl -p echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.localecho "echo 511 > /proc/sys/net/core/somaxconn" >> /etc/rc.local


下面是一个截图,需要对比的伙伴可以对一下


相信经过上面3行的配置,你的redis不会再报类似的警告了,当然你还需要在redis.conf文件中指定redis允许使用的内存大小(下章介绍),否则你的服务器撑不了两天



4.关闭redis服务

1)redis-shutdown #默认会保存后关闭

2)redis-cli shutdown save #



5.连接redis

[root@salt1 ~]# redis-cli

127.0.0.1:6379>



6.简单操作

127.0.0.1:6379> set id 001 #创建key-vlaue

OK

127.0.0.1:6379> get id #查找key

"001"

127.0.0.1:6379> del id #删除key

(integer) 1

127.0.0.1:6379> get id

(nil)

127.0.0.1:6379> exists id #查询id是否存在

(integer) 1

127.0.0.1:6379> del id

(integer) 1

127.0.0.1:6379> exists id

(integer) 0

127.0.0.1:6379> keys * #获取所有key

127.0.0.1:6379> set k1 v1

OK

127.0.0.1:6379> set k2 v2

OK

127.0.0.1:6379> set k3 v3

OK

127.0.0.1:6379> keys * #获取所有key

1) "k2"

2) "k3"

3) "k1"

127.0.0.1:6379> dbsize #获取所有key-value数

(integer) 3



redis默认有16个库,但是无法查看到(库的多少可以在redis.conf中配置)

127.0.0.1:6379> select 1 #切换到第二个库(从0开始数)

OK

127.0.0.1:6379[1]> keys *

(empty list or set)

127.0.0.1:6379[1]> set name 123

OK

127.0.0.1:6379[1]> keys *

1) "name"

127.0.0.1:6379[1]> select 0

OK

127.0.0.1:6379> keys *

1) "k2"

2) "k3"

3) "k1"

127.0.0.1:6379> select 15

OK

127.0.0.1:6379> select 16

(error) ERR invalid DB index


好了redis的安装就先说到这儿了


内存 配置 文件 服务 内核 字体 服务器 工具 系统 检查 解释 参数 后台 备份 客户 性能 日志 程序 空间 队列 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 游戏行业网络技术解决方案 从数据库提取同一文件的不同版本 金蝶 eas 服务器 幻视九歌服务器多少人 网络安全方面的考试题 数据库表创建完成后加约束 台湾仓储生鲜配送软件开发 服务器上打开sql管理器 独立性检验怎么建立数据库 网络安全宣传工作亮点 mysql数据库打不开 中国最牛的网络安全战队 魔兽世界正式服阿拉希服务器 河北森盛网络技术有限公司 java软件开发学前端还是后端 软件开发599.99啥意思 威纶通配方数据库灰色的 计算机网络技术专业士官眼镜要求 皇家战争部落服务器 我想看网络安全为人民的手抄报 青岛天驰网络技术有限公司 吉林国产化服务器供应公司 农行总行软件开发中心地址 网络安全产品和平台运营模式 数据库约束表达式属性 软件开发的产品生产怎么写 字节跳动云服务器怎么升级 网络技术标准四代升级到五代 网络安全教育日动画 服务器与时间服务器同步
0