0%

Linux(CentOS)环境下安装Redis 5

安装Redis

从Redis官网下载最新版本的redis。

1
$ sudo wget http://download.redis.io/releases/redis-5.0.8.tar.gz

如需安装 Redis 4 版本,可参考 Linux(CentOS)环境安装Redis 4

新建redis安装目录

1
$ sudo mkdir /usr/local/redis

解压Redis

1
$ sudo tar -zxvf redis-5.0.8.tar.gz -C /usr/local/redis

编译Redis

先安装gcc

1
$ sudo yum install gcc-c++

编译

1
2
cd /usr/local/redis/redis-5.0.8/
sudo make

安装

把redis安装在目录/usr/local/redis/redis-5.0.8/中,如果不写 PREFIX 参数,即默认安装在/usr/local/bin下面

1
2
3
4
5
6
7
8
9
10
$ cd src/
$ sudo make install PREFIX=/usr/local/redis/redis-5.0.8/

Hint: It's a good idea to run 'make test' ;)

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
1
2
3
$ make test
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1

安装tcl

1
$ sudo yum install tcl
1
2
3
4
sudo make test
\o/ All tests passed without errors!

Cleanup: may take some time... OK

安装完成后,在目录 /usr/local/redis/redis-5.0.8 下面会出现一个 bin 目录

1
2
3
4
5
6
7
8
$ ll bin/
total 32772
-rwxr-xr-x. 1 root root 4366824 Mar 20 13:14 redis-benchmark
-rwxr-xr-x. 1 root root 8125024 Mar 20 13:14 redis-check-aof
-rwxr-xr-x. 1 root root 8125024 Mar 20 13:14 redis-check-rdb
-rwxr-xr-x. 1 root root 4807816 Mar 20 13:14 redis-cli
lrwxrwxrwx. 1 root root 12 Mar 20 13:14 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8125024 Mar 20 13:14 redis-server

启动Redis服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ bin/redis-server 
36959:C 20 Mar 2020 13:28:02.467 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
36959:C 20 Mar 2020 13:28:02.467 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=36959, just started
36959:C 20 Mar 2020 13:28:02.467 # Warning: no config file specified, using the default config. In order to specify a config file use bin/redis-server /path/to/redis.conf
36959:M 20 Mar 2020 13:28:02.467 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
36959:M 20 Mar 2020 13:28:02.467 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
36959:M 20 Mar 2020 13:28:02.467 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 36959
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

36959:M 20 Mar 2020 13:28:02.468 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
36959:M 20 Mar 2020 13:28:02.468 # Server initialized
36959:M 20 Mar 2020 13:28:02.468 # 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.
36959:M 20 Mar 2020 13:28:02.468 # 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.
36959:M 20 Mar 2020 13:28:02.468 * Ready to accept connections

通过客户端连接redis服务

1
2
3
4
5
6
$ bin/redis-cli 
127.0.0.1:6379> set name key
OK
127.0.0.1:6379> get name
"key"
127.0.0.1:6379>

如果通过非127.0.0.1连接,会报如下错误:

1
2
$ bin/redis-cli -h 192.168.229.130
192.168.229.130:6379> get name
1
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

修改配置文件

1
$ sudo vi redis.conf

修改 bind 参数

1
bind 0.0.0.0

重启 redis 服务

通过 -h 指定redis服务的地址

1
2
bin/redis-cli -h 192.168.229.130
192.168.229.130:6379>

运行完成以后,如果需要关闭redis服务

1
2
127.0.0.1:6379> SHUTDOWN SAVE
not connected>