Redis常用命令和常见问题

Redis的启动和停止

启动命令:

yitian@heron01:/usr/local/bin$ redis-server /etc/redis/redis.conf

停止命令:

yitian@heron01:/usr/local/bin$ redis-cli shutdown

如果命令停止失败,使用如下命令查看redis进程并使用kill命令停止:

yitian@heron01:/usr/local/bin$ ps axu|grep redis
yitian    10106  0.1  0.0  44764  3188 ?        Ssl  20:29   0:01 redis-server 127.0.0.1:6379
yitian    10112  0.1  0.3 656136 15616 ?        Sl   20:29   0:01 gedit /home/yitian/redis/redis.log
yitian    10818  0.0  0.0  14224   932 pts/18   S+   20:51   0:00 grep --color=auto redis

Redis的日志路径配置

redis在默认情况下没有日志输出,如果需要配置redis的日志输出,则需要在/etc/redis/中的redis.conf配置文件中,在如下位置进行配置redis的日志文件路径:

image

Redis添加绑定IP

为在window下使用redis客户端能够连接到虚拟机中的redis服务,因此这里修改了redis的绑定ip:

image

Redis远程连接

在上述添加了redis的绑定ip后,指定连接主机和端口的远程连接命令:

yitian@heron01:/usr/local/bin$ redis-cli -h 192.168.209.137 -p 6379
192.168.209.137:6379> get test
(nil)
192.168.209.137:6379> 

Redis连接客户端

http://database.51cto.com/art/201505/477692.htm

https://www.cnblogs.com/liuconglin/p/5847351.html

https://github.com/uglide/RedisDesktopManager

https://www.cnblogs.com/zheting/p/7670154.html

https://download.csdn.net/download/sailorhdx/10685314?utm_source=bbsseo

下载windows下的客户端并安装后,编辑虚拟机种安装的redis-server连接信息如下,即可连接成功:

image

这里使用虚拟机ip进行连接的前提是,上述修改了redis的绑定ip才可以实现,并且这里的虚拟机网络配置为NAT模式,如果是桥接模式请使用虚拟机指定的IP进行连接。

 

常见问题

在执行redis关闭的命令时,在redis的log文件中出现如下错误信息:

10106:M 29 Oct 21:14:32.128 # Background saving error
10106:M 29 Oct 21:14:38.080 * 1 changes in 900 seconds. Saving...
10106:M 29 Oct 21:14:38.082 * Background saving started by pid 11141
11141:C 29 Oct 21:14:38.082 # Failed opening the RDB file dump.rdb (in server root dir /usr/local/bin) for saving: Permission denied
10106:M 29 Oct 21:14:38.183 # Background saving error
10106:M 29 Oct 21:14:44.040 * 1 changes in 900 seconds. Saving...
10106:M 29 Oct 21:14:44.041 * Background saving started by pid 11142
11142:C 29 Oct 21:14:44.041 # Failed opening the RDB file dump.rdb (in server root dir /usr/local/bin) for saving: Permission denied
10106:M 29 Oct 21:14:44.142 # Background saving error
10106:M 29 Oct 21:14:50.093 * 1 changes in 900 seconds. Saving...
10106:M 29 Oct 21:14:50.093 * Background saving started by pid 11143
11143:C 29 Oct 21:14:50.094 # Failed opening the RDB file dump.rdb (in server root dir /usr/local/bin) for saving: Permission denied
10106:M 29 Oct 21:14:50.194 # Background saving error
10106:M 29 Oct 21:14:56.040 * 1 changes in 900 seconds. Saving...
10106:M 29 Oct 21:14:56.041 * Background saving started by pid 11145
11145:C 29 Oct 21:14:56.041 # Failed opening the RDB file dump.rdb (in server root dir /usr/local/bin) for saving: Permission denied

解决方法:

问题原因是redis.conf中dir配置项中设置的为./,即redis-server命令所在目录,这里为/usr/local/bin目录,该目录的权限为root用户的权限,而我使用的为普通用于yitian启动的redis,因此这里出现了权限的问题。修改方法为,在/etc/redis/redis.conf配置文件中,将其中的dir配置项这是为/home/yitian/redis/目录,这个目录的权限是yitian的,这样在启动和关闭redis时即不会出现错误:

image

重新尝试后,正常启动和关闭redis:

image