1、准备 redis 安装包
这里下载的是 redis-5.0.8.tar.gz 安装包,并将其直接放在了 root 目录下
2、解压安装包
在 /usr/local/ 目录下创建 redis ⽂件夹
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mkdir redis
将 redis 安装包解压到 /usr/local/redis 中
[root@localhost ~]# tar -zxvf redis-5.0.8.tar.gz -C /usr/local/redis
解压完之后, /usr/local/redis 目录中会出现⼀个 redis-5.0.8 的目录
3、编译并安装
进入 redis-5.0.8 目录编译安装
[root@localhost ~]# cd /usr/local/redis/redis-5.0.8
[root@localhost redis-5.0.8]# make && make install
4、将 redis 安装为系统服务并后台启动
进入 utils ⽬录,并执行如下脚本:
注意:我这里全部选择的是默认配置(直接回车),有需要可以按需配置
[root@localhost redis-5.0.8]# cd utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selected default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
5、查看 redis 服务启动情况
执行如下命令查看 redis 启动结果:
[root@localhost utils]# systemctl status redis_6379.service
● redis_6379.service - LSB: start and stop redis_6379
Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
Active: active (running) since Thu 2021-05-06 11:48:47 CST; 3s ago
Docs: man:systemd-sysv-generator(8)
Process: 13382 ExecStop=/etc/rc.d/init.d/redis_6379 stop (code=exited, status=0/SUCCESS)
Process: 13385 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/redis_6379.service
└─13388 /usr/local/bin/redis-server 0.0.0.0:6379
May 06 11:48:47 localhost:localdomain systemd[1]: Starting LSB: start and stop redis_6379...
May 06 11:48:47 localhost:localdomain redis_6379[13385]: Starting Redis server...
May 06 11:48:47 localhost:localdomain systemd[1]: Started LSB: start and stop redis_6379.
6、启动 redis 客户端并测试
启动自带的 redis-cli 客户端,测试通过:
[root@localhost utils]# redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
但此时只能在本地访问,无法远程连接,因此还需要做部分设置
6、设置允许远程连接和访问密码
编辑 redis 配置文件
[root@localhost utils]# vim /etc/redis/6379.conf
将其中的 bind 127.0.0.1 修改为 bind 0.0.0.0
找到如下内容:
#requirepass foobared
去掉注释,将 foobared 修改为自己的密码,如:
requirepass 123456
保存,重启 redis 服务:
[root@localhost utils]# systemctl restart redis_6379.service
这样后续的访问需要先输入密码认证通过
[root@localhost utils]# redis-cli
127.0.0.1:6379> get foo
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get foo
"bar"