1. 访问 redis官网redis.io/download 下载压缩包, 或者使用wget命令直接下载
[root@localhost software]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz
2. 解压缩
[root@localhost software]# tar -zxvf redis-5.0.5.tar.gz
3. 安装redis之前,需要先安装 gcc-c++
[root@localhost software]# yum install gcc-c++
4. 进入解压后的目录,进行编译
[root@localhost software]# cd redis-5.0.5
[root@localhost redis-5.0.5]# make
如果安装的是6.0版本的redis,此处make会报错
expected specifier-qualifier-list before '_Atomic'
_Atomic unsigned int lruclock; /* Clock for LRU eviction */
因为最新版的redis用到了c11,系统自带gcc版本是4.8.5,太低了,需要升级到5.4以上 查看当前gcc版本
[root@localhost redis-5.0.5]# gcc -v
升级gcc
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
gcc -v
升级完成之后在进行 make 操作。
5. 编译完成后,进行安装
[root@localhost redis-5.0.5]# make install
6. 安装完成后,进行redis配置,先进入 utils 目录
[root@localhost redis-5.0.5]# cd utils/
7. 将 redis_init_script 文件 拷贝到 /etc/init.d/ 目录下
[root@localhost utils]# cp redis_init_script /etc/init.d/
8. 拷贝完成后,进入到 /etc/init.d/ 目录下,查看是否拷贝成功
[root@localhost utils]# cd /etc/init.d/
[root@localhost init.d]# ll
总用量 44
-rw-r--r--. 1 root root 18281 8月 19 2019 functions
-rwxr-xr-x. 1 root root 4569 8月 19 2019 netconsole
-rwxr-xr-x. 1 root root 7928 8月 19 2019 network
-rw-r--r--. 1 root root 1160 4月 1 09:30 README
-rwxr-xr-x. 1 root root 1352 9月 24 15:51 redis_init_script
[root@localhost init.d]#
9. 新建一个目录,用于存放 redis 配置文件,然后将 redis.conf 配置文件拷贝到新建的目录下
[root@localhost redis-5.0.5]# mkdir /usr/local/redis -p
[root@localhost redis-5.0.5]# cp redis.conf /usr/local/redis/
[root@localhost redis-5.0.5]# cd /usr/local/redis/
[root@localhost redis]# ll
总用量 64
-rw-r--r--. 1 root root 61797 9月 24 15:57 redis.conf
[root@localhost redis]#
10. 修改拷贝过来的配置文件
[root@localhost redis]# vim redis.conf
# 修改为可以远程调用:
在69行:将 bind 127.0.0.1 修改为 bind 0.0.0.0
# 改为后台运行:
在136行:将 daemonize no 修改为 daemonize yes
# 修改工作空间:
在263行:将 dir ./ 修改为 dir /usr/local/redis/
设置redis密码:
在507行:将 # requirepass foobared 注释放开,并重新设置密码,
例如:requirepass mypassword1234
修改完成后,保存即可!
redis版本不同,所对应的行数可能不同,提供的行数仅供参考!!!
11. 回到 /etc/init.d/ 目录下,编辑 redis_init_script 脚本文件
[root@localhost redis]# cd /etc/init.d/
[root@localhost init.d]# ll
总用量 44
-rw-r--r--. 1 root root 18281 8月 19 2019 functions
-rwxr-xr-x. 1 root root 4569 8月 19 2019 netconsole
-rwxr-xr-x. 1 root root 7928 8月 19 2019 network
-rw-r--r--. 1 root root 1160 4月 1 09:30 README
-rwxr-xr-x. 1 root root 1352 9月 24 15:51 redis_init_script
[root@localhost init.d]#
将 CONF="/etc/redis/${REDISPORT}.conf" 更改为刚刚修改的配置文件路径,CONF="/usr/local/redis/redis.conf"
12. 给 redis_init_script 脚本添加权限
[root@localhost init.d]# chmod 777 redis_init_script
13. 启动 redis
[root@localhost init.d]# ./redis_init_script start
Starting Redis server...
59922:C 24 Sep 2020 16:24:29.724 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
59922:C 24 Sep 2020 16:24:29.724 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=59922, just started
59922:C 24 Sep 2020 16:24:29.724 # Configuration loaded
14. 查看 redis 是否启动成功
[root@localhost init.d]# ps -ef | grep redis
root 59923 1 0 16:24 ? 00:00:00 /usr/local/bin/redis-server 0.0.0.0:6379
root 60482 1272 0 16:25 pts/0 00:00:00 grep --color=auto redis
启动成功!
15. 设置 redis 开机自启动
[root@localhost init.d]# chkconfig redis_init_script on
使用命令 ./redis_init_script stop 停止redis报错
[root@localhost init.d]# ./redis_init_script stop
Stopping …
OK
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
…
redi设置密码登录后,想关闭redis服务器,需要
redis-cli -a 密码 shutdown
redis启动报错 /var/run/redis_6379.pid exists, process is already running or crashed
[root@localhost init.d]# ./redis_init_script start
/var/run/redis_6379.pid exists, process is already running or crashed
查看redis进程号是否一致:
[root@localhost init.d]# vi /var/run/redis_6379.pid
[root@localhost init.d]# ps -ef | grep redis
root 25515 1263 0 09:13 pts/0 00:00:00 grep --color=auto redis
不一致则删除错误进程,然后重启redis
[root@localhost init.d]# rm -rf /var/run/redis_6379.pid
[root@localhost init.d]# ./redis_init_script start
redis无法远程连接
如果是本地测试机,可以关闭防火墙
关闭防火墙
systemctl stop firewalld.service
禁止开机自启动
systemctl disable firewalld.service
如果是线上(阿里云或腾讯云),在安全组放开对应端口即可