目前为止,官网最新Redis版本为5.0.3,本文以5.0.3版本为例进行安装
Download, extract and compile Redis with:
$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz
$ tar xzf redis-5.0.3.tar.gz
$ ln -s redis-5.0.3 redis
$ cd redis-5.0.3
$ make
$ make install
注释:
1、下载Redis制定版本的源码压缩包到当前目录
2、解压源码压缩包
3、建立一个redis软链接
4、进入Redis目录
5、编译
6、安装
这里对建立软链接说一下,这样做是为了不把Redis目录固定在指定的版本上,有利于Redis未来的版本升级,这是安装软件的一个好习惯。
安装后会将Redis运行的相关文件放到/usr/local/bin下,这样可以在任何目录下执行Redis命令
查看Redis的版本:
$ redis-cli -v
注:在用make编译时如果发生错误退出,检查是否安装gcc,如果否,则用 yum -y install gcc 进行安装,安装完成后再进行make编译安装即可。
将Redis的配置写到指定的文件中,如我安装的Redis配置文件在/opt/redis-5.0.3/redis.conf,则启动命令为:
$ redis-server /opt/redis-5.0.3/redis.conf
成功启动后,出现如下界面:
43118:C 04 Mar 2019 13:52:10.190 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
43118:C 04 Mar 2019 13:52:10.190 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=43118, just started
43118:C 04 Mar 2019 13:52:10.190 # Configuration loaded
43118:M 04 Mar 2019 13:52:10.190 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 43118
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
43118:M 04 Mar 2019 13:52:10.192 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
43118:M 04 Mar 2019 13:52:10.192 # Server initialized
43118:M 04 Mar 2019 13:52:10.192 # 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.43118:M 04 Mar 2019 13:52:10.192 # 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.43118:M 04 Mar 2019 13:52:10.192 * DB loaded from disk: 0.000 seconds
43118:M 04 Mar 2019 13:52:10.192 * Ready to accept connections
打开Redis的配置文件,发现有大概六十多项信息需要配置,后面再专门写一篇文章进行说明,再此列出几项重要配置:
① port:端口,Redis默认端口为6379
② logfile:日志文件
③ dir:Redis工作目录,用来存放持久化文件和日志文件
④ daemonize:是否以守护进程的方式启动Redis
使用redis-cli连接Redis服务器
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>
这样就进入了交互界面,其中127.0.0.1为默认本地host、6379为默认端口,在启动时可以不写:
$redis-cli
127.0.0.1:6379>
在交互界面就可以开始输入命令进行工作了:
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379>
学习的时候资料上说使用redis-cli shutdown 命令关闭Redis,但是在远程交互的时候我们已经在控制台,此时只需要在交互界面输入:
127.0.0.1:6379> shutdown
not connected>
此时Redis已经关闭
43579:C 04 Mar 2019 14:28:01.408 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
43579:C 04 Mar 2019 14:28:01.408 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=43579, just started
43579:C 04 Mar 2019 14:28:01.408 # Configuration loaded
43579:M 04 Mar 2019 14:28:01.409 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 43579
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
43579:M 04 Mar 2019 14:28:01.410 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
43579:M 04 Mar 2019 14:28:01.410 # Server initialized
43579:M 04 Mar 2019 14:28:01.410 # 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.43579:M 04 Mar 2019 14:28:01.410 # 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.43579:M 04 Mar 2019 14:28:01.411 * DB loaded from disk: 0.000 seconds
43579:M 04 Mar 2019 14:28:01.411 * Ready to accept connections
43579:M 04 Mar 2019 14:28:39.189 # User requested shutdown...
43579:M 04 Mar 2019 14:28:39.189 * Saving the final RDB snapshot before exiting.
43579:M 04 Mar 2019 14:28:39.191 * DB saved on disk
43579:M 04 Mar 2019 14:28:39.191 * Removing the pid file.
43579:M 04 Mar 2019 14:28:39.191 # Redis is now ready to exit, bye bye...
这是正确的、比较温柔的方法关闭Redis,还有比较暴力的方法就是利用kill -9 直接杀死进程,除非特殊情况,否则不建议使用此方法…