Redis 分为 客户端与服务端
- redis 服务端
- redis-server
- redis 客户单
- redis-cli [option] -h 指定连接的主机 默认为127.0.0.1 -p 指定端口 默认为6379
1. 安装 Redis
- 解压包
[root@localhost ~]# tar -zxf redis-4.0.11.tar.gz -C /usr/src/
- 编译安装
[root@localhost ~]# cd /usr/src/redis-4.0.11/
[root@localhost redis-4.0.11]# make && make instal
2. 运行Redis
1) 首次运行 redis-server
19719:C 16 Dec 15:59:44.660 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
警告:没有指定配置文件,使用默认配置。要指定配置文件,请使用redis-server /path/to/redis.conf
解决方法:
在启动时指定配置文件
19719:M 16 Dec 15:59:44.672 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
警告:由于/proc/sys/net/core/somaxconn设置为较低的值128,所以无法强制执行511的TCP backlog设置。
解决方法:
[root@localhost redis-4.0.11]# echo 511 > /proc/sys/net/core/somaxconn
[root@localhost redis-4.0.11]# cat /proc/sys/net/core/somaxconn
511
19719:M 16 Dec 15:59:44.673 # 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.
警告:overcommit_memory设置为0!在内存不足的情况下,后台保存可能会失败。要修复这个问题,请添加“vm”。overcommit_memory = 1' to /etc/sysctl.conf,然后重新启动或运行命令'sysctl vm。overcommit_memory=1'使其生效。
解决方法:
[root@localhost redis-4.0.11]# echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
[root@localhost redis-4.0.11]# sysctl -p
vm.overcommit_memory = 1
19719:M 16 Dec 15:59:44.673 # 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.
警告:您的内核中启用了透明大页面(THP)支持。这将在Redis中产生延迟和内存使用问题。要修复这个问题,可以运行命令'echo never > /sys/kernel/mm/ transparency t_hugepage/enabled'作为根目录,并将其添加到/etc/rc.中本地,以便在重新启动后保留设置。禁用THP后必须重新启动Redis。
解决方法:
[root@localhost redis-4.0.11]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
- 启动服务
[root@localhost redis-4.0.11]# pwd
/usr/src/redis-4.0.11
[root@localhost redis-4.0.11]# redis-server redis.conf
19749:M 16 Dec 16:10:18.315 # Server initialized
19749:M 16 Dec 16:10:18.315 * DB loaded from disk: 0.000 seconds
19749:M 16 Dec 16:10:18.315 * Ready to accept connections
- role(角色)
- M:主服务器 S :从服务器
3. Client 登录 redis-cli
- 登录进去可以ping出PONG表示没有问题
[root@localhost redis-4.0.11]# redis-cli
127.0.0.1:6379> ping
PONG
-
结束Redis服务端的方式
-
- 在命令行中
redis-cli -h host --p port shutdown
-
- 结束进程
killall redis
-
- 在客户端命令执行
-
127.0.0.1:6379> shutdown
4.解决阻塞问题
- 让Redis 在后台运行
[root@localhost redis-4.0.11]# vim /usr/src/redis-4.0.11/redis.conf
bind 0.0.0.0 ## 将bind 127.0.0.1 改为 bind 0.0.0.0 表示任何地址都可以登录redis
port 6379 ## 当前Redis所监听的端口号
daemonize yes ## 是否后台运行,将 daemonize no 改为 daemonize yes 以支持后台运行
logfile "/root/logredis.log" ## 指定日志文件