centOS7 安装 redis6.0.6

下载

linux 上执行:

1
2
3
4
5
6
7
8
9
10
11
12
[root@iZx068ismxxjaoZ src]#  wget http://download.redis.io/releases/redis-6.0.6.tar.gz
--2020-12-17 12:58:12-- http://download.redis.io/releases/redis-6.0.6.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2228781 (2.1M) [application/octet-stream]
Saving to: ‘redis-6.0.6.tar.gz’

100%[========================================================================================================================>] 2,228,781 24.6KB/s in 69s

2020-12-17 12:59:25 (31.6 KB/s) - ‘redis-6.0.6.tar.gz’ saved [2228781/2228781]

解压

1
[root@iZx068ismxxjaoZ src]# tar -xzf redis-6.0.6.tar.gz 

编译(源码)

1
2
[root@iZx068ismxxjaoZ src]# cd redis-6.0.6
[root@iZx068ismxxjaoZ redis-6.0.6]# make

报错:

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
......
^
server.c:5212:31: error: ‘struct redisServer’ has no member named ‘server_cpulist’
redisSetCpuAffinity(server.server_cpulist);
^
server.c: In function ‘hasActiveChildProcess’:
server.c:1480:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘allPersistenceDisabled’:
server.c:1486:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘writeCommandsDeniedByDiskError’:
server.c:3826:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘iAmMaster’:
server.c:5000:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make[1]: *** [server.o] Error 1
make[1]: Leaving directory `/usr/local/src/redis-6.0.6/src'
make: *** [all] Error 2

image-20201217140530481

依次执行:

1
2
3
4
yum install centos-release-scl
yum install devtoolset-7-gcc*
scl enable devtoolset-7 bash
# 过程中提示 y/n 按 y

再次执行编译:make 。成功了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
......
CC setcpuaffinity.o
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof

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

make[1]: Leaving directory `/usr/local/src/redis-6.0.6/src'
[root@iZx068ismxxjaoZ redis-6.0.6]#

安装到指定目录

1
2
# 执行
make install PREFIX=/usr/local/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

[root@iZx068ismxxjaoZ bin]# cd /usr/local/redis/bin/
[root@iZx068ismxxjaoZ bin]# ./redis-server
31845:C 17 Dec 2020 14:18:30.709 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31845:C 17 Dec 2020 14:18:30.709 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=31845, just started
31845:C 17 Dec 2020 14:18:30.709 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 31845
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

31845:M 17 Dec 2020 14:18:30.711 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
31845:M 17 Dec 2020 14:18:30.711 # Server initialized
31845:M 17 Dec 2020 14:18:30.711 # 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.
31845:M 17 Dec 2020 14:18:30.711 # 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.
31845:M 17 Dec 2020 14:18:30.711 * Ready to accept connections

后台启动

从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@iZx068ismxxjaoZ bin]# cp /usr/local/src/redis-6.0.6/redis.conf /usr/local/redis/bin/
[root@iZx068ismxxjaoZ bin]# ll
total 35792
-rw-r--r-- 1 root root 92 Dec 17 14:19 dump.rdb
-rwxr-xr-x 1 root root 4719584 Dec 17 14:14 redis-benchmark
-rwxr-xr-x 1 root root 8952248 Dec 17 14:14 redis-check-aof
-rwxr-xr-x 1 root root 8952248 Dec 17 14:14 redis-check-rdb
-rwxr-xr-x 1 root root 4975064 Dec 17 14:14 redis-cli
-rw-r--r-- 1 root root 83392 Dec 17 14:21 redis.conf
lrwxrwxrwx 1 root root 12 Dec 17 14:14 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8952248 Dec 17 14:14 redis-server
[root@iZx068ismxxjaoZ bin]#

修改 redis.conf 文件,把 daemonize no 改为 daemonize yes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.

启动

1
2
3
4
5
6
7
8
9
[root@iZx068ismxxjaoZ bin]# ./redis-server redis.conf 
32114:C 17 Dec 2020 14:23:50.018 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32114:C 17 Dec 2020 14:23:50.018 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=32114, just started
32114:C 17 Dec 2020 14:23:50.018 # Configuration loaded
[root@iZx068ismxxjaoZ bin]#
[root@iZx068ismxxjaoZ bin]# ps -ef |grep redis
root 32115 1 0 14:23 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 32196 31555 0 14:25 pts/1 00:00:00 grep --color=auto redis
# kill -9 32115

设置开机启动

添加开机启动服务文件:

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
vim /etc/systemd/system/redis.service
# 输入以下内容并保存

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
# 这里填写自己的路径
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

#执行:
systemctl daemon-reload #重新加载服务配置文件
systemctl enable redis.service
systemctl start redis.service

[root@iZx068ismxxjaoZ system]# ps -ef | grep redis
root 337 1 0 14:36 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root 345 31555 0 14:36 pts/1 00:00:00 grep --color=auto redis
[root@iZx068ismxxjaoZ system]#

服务操作命令

systemctl start redis.service #启动redis服务

systemctl stop redis.service #停止redis服务

systemctl restart redis.service #重新启动服务

systemctl status redis.service #查看服务当前状态

systemctl enable redis.service #设置开机自启动

systemctl disable redis.service #停止开机自启动

开启远程连接

编辑 redis.conf

注释掉 bind 127.0.0.1 这行。

修改 protected-mode 为 no

(阿里云服务器没有配的话 配置下安全组 开放 6379 端口 )

重启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

参考:

redis 中文网 http://www.redis.cn/download.html

博客:https://www.cnblogs.com/heqiuyong/p/10463334.html

博客:https://blog.csdn.net/ermaner666/article/details/98193721