官方文档中给出的安装过程比较简单:
1. 下载完成后进行编译:
$ wget http://download.redis.io/releases/redis-5.0.7.tar.gz
$ tar xzf redis-5.0.7.tar.gz
$ cd redis-5.0.7
$ make
2. 编译后直接使用src中的redis-server命令进行运行:
$ src/redis-server
3. 然后可以另开一个终端使用客户端进行连接并进行简单的测试:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
1. 下载地址:https://redis.io/download
2. 下载完成后复制到虚拟机中(这里复制到了/home/yitian/redis/目录下),然后创建/usr/redis/目录,将安装文件移动到该文件目录下:
yitian@heron01:/usr$ sudo mkdir redis
yitian@heron01:/usr$ sudo cp ~/redis/redis-4.0.9.tar.gz /usr/redis/
yitian@heron01:/usr/redis$ ll
-rw------- 1 root root 1737022 Apr 8 06:22 redis-4.0.9.tar.gz
1. 解压命令
yitian@heron01:/usr/redis$ sudo tar -zxvf redis-4.0.9.tar.gz
yitian@heron01:/usr/redis$ ll
drwxrwxr-x 6 root root 4096 Mar 26 09:04 redis-4.0.9/
-rw------- 1 root root 1737022 Apr 8 06:22 redis-4.0.9.tar.gz
2. 使用如下命令进行安装
yitian@heron01:/usr/redis/redis-4.0.9$ sudo make
yitian@heron01:/usr/redis/redis-4.0.9$ sudo make test
\o/ All tests passed without errors!
yitian@heron01:/usr/redis/redis-4.0.9$ sudo make install
cd src && make install
make[1]: Entering directory '/usr/redis/redis-4.0.9/src'
Hint: It's a good idea to run 'make test' ?
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory '/usr/redis/redis-4.0.9/src'
默认的安装目录为/usr/local/bin下:
yitian@heron01:/usr/redis/redis-4.0.9$ cd /usr/local/bin/
yitian@heron01:/usr/local/bin$ ll
total 23624
drwxr-xr-x 2 root root 4096 Apr 8 06:34 ./
drwxr-xr-x 10 root root 4096 Aug 1 2017 ../
-rwxr-xr-x 1 root root 3123 Feb 14 00:24 idea*
-rwxr-xr-x 1 root root 2701608 Apr 8 06:34 redis-benchmark*
-rwxr-xr-x 1 root root 6191096 Apr 8 06:34 redis-check-aof*
-rwxr-xr-x 1 root root 6191096 Apr 8 06:34 redis-check-rdb*
-rwxr-xr-x 1 root root 2894712 Apr 8 06:34 redis-cli*
lrwxrwxrwx 1 root root 12 Apr 8 06:34 redis-sentinel -> redis-server*
-rwxr-xr-x 1 root root 6191096 Apr 8 06:34 redis-server*
1. 为方便配置文件的管理,将解压得到的目录(/usr/redis)中的redis.conf文件复制到/etc/redis/目录下(这个目录需要创建,且可以自己指定别的目录)
yitian@heron01:/usr/redis/redis-4.0.9$ sudo mkdir /etc/redis
yitian@heron01:/usr/redis/redis-4.0.9$ sudo cp redis.conf /etc/redis/
yitian@heron01:/usr/redis/redis-4.0.9$ cd /etc/redis/
yitian@heron01:/etc/redis$ ll
-rw-r--r-- 1 root root 58766 Apr 8 06:38 redis.conf
2. 编辑复制后的redis.conf文件,将redis设置为可以后台运行(daemonize默认为no):
启动Redis服务命令如下:
yitian@heron01:/usr/local/bin$ redis-server /etc/redis/redis.conf
41761:C 08 Apr 06:42:35.524 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41761:C 08 Apr 06:42:35.524 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=41761, just started
41761:C 08 Apr 06:42:35.524 # Configuration loaded
yitian@heron01:/usr/local/bin$ redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> quit
通过执行命令ping,若输出为pong,则证明服务成功启动。执行quit命令退出。
Reids客户端关闭命令:
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的日志输出,则需要在/etc/redis/中的redis.conf配置文件中,在如下位置进行配置redis的日志文件路径:
由于这里使用的是非root用户启动的redis,所以这里将日志文件放到了启动用户yitian的home目录下:
对于root用户启动的情况,为了使redis相关文件集中,可以将其log文件输出到/usr/reids/log/目录下:
注:配置完logfile之后,重新启动redis时,将不会再有日志从控制台输出,但可以在log文件中进行查看。
为在window下使用redis客户端能够连接到虚拟机中的redis服务,因此这里修改了redis的绑定ip。如果需要所有的IP都可以请求连接,则将bind这一行注释掉即可。
在上述添加了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的安全模式关闭(protected-mode默认为yes),否则在使用Jedis等客户端连接Redis时会出现异常:
这里的Redis Window环境中的客户端选择为:Redis Desktop Manager,GitHub地址:https://github.com/uglide/RedisDesktopManager。下载windows下的客户端并安装后,编辑虚拟机种安装的redis-server连接信息如下,即可连接成功:
这里使用虚拟机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/db/目录,这个目录的权限是yitian的,这样在启动和关闭redis时即不会出现错误:
修改完成后重新启动redis然后执行shutdown命令,可以正常关闭,日志输出如下:
9009:M 10 Feb 16:23:36.807 * Ready to accept connections
# 上述日志为启动成功后的输出
# 下面为shutdown命令执行后的输出,可以正常关闭
9009:M 10 Feb 16:32:47.232 # User requested shutdown...
9009:M 10 Feb 16:32:47.233 * Saving the final RDB snapshot before exiting.
9009:M 10 Feb 16:32:47.243 * DB saved on disk
9009:M 10 Feb 16:32:47.243 * Removing the pid file.
9009:M 10 Feb 16:32:47.243 # Redis is now ready to exit, bye bye...
注:如果是root用户启动Redis服务则不会出现该问题。