1、下载源码

1
2
3
4
将下载的包存储在新创建的/opt/soft目录下
[root@nues ~]# mkdir /opt/soft #创建soft文件夹
[root@nues ~]# cd /opt/soft
[root@nues soft]# wget http://download.redis.io/releases/redis-5.0.6.tar.gz

2、安装组件

1
2
[root@nues soft]# yum groupinstall 'Development Tools'
[root@nues soft]# tar -xf redis-5.0.6.tar.gz -C /usr/local/ #解压到 /usr/local/路径

3、编译安装

1
2
3
4
5
[root@nues soft]# yum -y install gcc make  #安装组件
[root@nues soft]# cd /usr/local/
[root@nues local]# mv redis-5.0.6 redis #修改名字
[root@nues local]# cd redis
[root@nues redis]# make #编译

编译redis.webp

1
[root@nues redis]# make install PREFIX=/usr/local/redis  # 参数:后面跟 PREFIX=/usr/local/redis  指定redis的安装路径

安装redis.webp

1
2
3
拷贝执行文件到/usr/local/bin下
[root@nues redis]# cd /usr/local/redis/bin
[root@nues bin]# cp * /usr/local/bin/

4、修改配置

1
2
3
4
5
6
7
8
9
10
[root@localhost redis]# mkdir -p /data/redis/logs #创建数据目录和日志存储目录
[root@nues ~]# vim /usr/local/redis/redis.conf
修改以下行
#行号#
69 #bind 127.0.0.1 # 添加#号 注释掉,以便外网访问
88 protected-mode no #yes改成no 关闭保护模式
136 daemonize yes #no 改成ye 开启守护进程模式
171 logfile "/data/redis/logs/redis.log" #指定redis log 日志存储路径以及日志文件名称
263 dir /data/redis #指定AOF和RDB存储路径
507 requirepass nues1234 #去掉注释, 后面设置redis的密码

5、创建用户

1
2
3
4
5
#赋权(注意要跟systemd中指定的用户一样)
[root@nues ~]# groupadd appadmin
[root@nues ~]# useradd -g appadmin -s /sbin/nologin appadmin
[root@nues ~]# chown -R appadmin. /usr/local/redis
[root@nues ~]# chown -R appadmin. /data/redis/

6、系统管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@nues ~]# vim /usr/local/redis/redis.conf
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
ExecRepload=/bin/kill -s HUP $MAINPID
Type=notify
User=appadmin
Group=appadmin
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target

7、启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@nues ~]# systemctl daemon-reload  #重新加载某个服务的配置文件,如果新安装了一个服务,归属于 systemctl 管理,要是新服务的服务程序配置文件生效,需重新加载。
[root@nues ~]# systemctl start redis
[root@nues ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
[root@nues ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2021-09-14 23:29:40 EDT; 10s ago
Main PID: 102290 (redis-server)
CGroup: /system.slice/redis.service
└─102290 /usr/local/redis/bin/redis-server *:6379

9月 14 23:29:40 nues systemd[1]: Starting Redis persistent key-value database...
9月 14 23:29:40 nues systemd[1]: Started Redis persistent key-value database.
9月 14 23:29:46 nues systemd[1]: [/usr/lib/systemd/system/redis.service:7] Unknown lvalue 'ExecRepload' in section 'Service'
[root@nues ~]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 102290 appadmin 6u IPv6 450647 0t0 TCP *:6379 (LISTEN)
redis-ser 102290 appadmin 7u IPv4 450648 0t0 TCP *:6379 (LISTEN)
[root@nues ~]# ss -tnlp | grep 6379 #socket statistics 命令即为ss
LISTEN 0 128 *:6379 *:* users:(("redis-server",pid=102290,fd=7))
LISTEN 0 128 :::6379 :::* users:(("redis-server",pid=102290,fd=6))

8、密码管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@nues ~]#  vim /usr/local/redis/redis.conf
#行号#
507 requirepass nues1234 # requirepass 后面的为修改后的密码
[root@nues ~]# systemctl restart redis #重启服务 修改密码生效
验证密码
[root@nues ~]# redis-cli
127.0.0.1:6379> auth nues1234
OK
127.0.0.1:6379> ping
PONG
远程访问,注意选项后面必须有个空格,然后再写密码和IP
[root@nues ~]# redis-cli -p 6379 -c -h 10.15.3.233
10.15.3.233:6379> auth nues1234
OK
10.15.3.233:6379>