准备
这里使用 redis-6.2.6 版本
安装
下载并解压到安装目录
[root@localhost local]# wget https://download.redis.io/releases/redis-6.2.6.tar.gz
[root@localhost local]# tar -zxf redis-6.2.6.tar.gz
[root@localhost local]# mv redis-6.2.6 redis
[root@localhost local]# ll
总用量 2424
drwxrwxr-x. 7 root root 4096 10月 4 2021 redis
-rw-r--r--. 1 root root 2476542 10月 4 2021 redis-6.2.6.tar.gz
[root@localhost local]# cd redis
检查编译环境无则安装
[root@localhost redis]# rpm -q gcc gcc-c++
未安装软件包 gcc
未安装软件包 gcc-c++
[root@localhost redis]# yum -y install gcc gcc-c++
源码编译
# 注意执行编译的工作目录必须为redis
# make install默认会把bin放到/usr/local/bin/下面,需要加上PREFIX=/usr/local/redis/指定目录
[root@localhost redis]# make && make install PREFIX=/usr/local/redis/
cd src && make install
make[1]: 进入目录“/usr/local/redis/src”
启动服务
自启动服务配置参考:笔记【配置自启动服务 in Centos7.x】
# 日志路径
[root@localhost redis]# mkdir log
# 修改配置
[root@localhost redis]# cp redis.conf redis.my.conf
[root@localhost redis]# vi redis.my.conf
...
bind 0.0.0.0
protected-mode no
daemonize yes
logfile "/usr/local/redis/log/redis.log"
...
# 创建服务
[root@localhost redis]# vi /usr/lib/systemd/system/redis.service
...
[Unit]
Description=redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.my.conf
ExecReload=/usr/local/redis/bin/redis-server -s reload
ExecStop=/usr/local/redis/bin/redis-server -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
...
## 启动服务
# 查看当前服务状态如下
[root@localhost redis]# systemctl status redis
● redisd.service - redis
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: inactive (dead)
# 加入开机启动项
[root@localhost redis]# systemctl enable redis
# 启动服务
[root@localhost redis]# systemctl start redis
# 再次查看当前服务状态
[root@localhost redis]# systemctl status redis
● redisd.service - redis
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2022-05-13 13:05:55 CST; 27s ago
Main PID: 25766 (redis-server)
CGroup: /system.slice/redis.service
└─25766 /usr/local/redis/bin/redis-server 0.0.0.0:6379
- 常用命令:
- systemctl start redis(启动redis服务)
- systemctl restart redis(重启redis服务)
- systemctl stop redis(停止redis服务)
- systemctl enable redis(添加开机自启)
- systemctl disable redis(禁止开机自启)
- systemctl status redis(查看状态)
开启防火墙端口
[root@localhost redis]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2022-05-13 10:59:02 CST; 2h 42min ago
Docs: man:firewalld(1)
Main PID: 670 (firewalld)
CGroup: /system.slice/firewalld.service
└─670 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
[root@localhost redis]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost redis]# firewall-cmd --reload
success
[root@localhost redis]# firewall-cmd --list-ports
6379/tcp
可能遇到的问题
Increased maximum number of open files to 10032 (it was originally set to 1024).
- ulimit -n 10032
The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
- 将net.core.somaxconn = 1024添加到/etc/sysctl.conf中,然后执行sysctl -p生效配置。
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.
- 将vm.overcommit_memory = 1添加到/etc/sysctl.conf中,然后执行sysctl -p生效配置。
you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix thisissue 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 thesetting after a reboot. Redis must be restarted after THP is disabled.
- 将echo never > /sys/kernel/mm/transparent_hugepage/enabled添加到/etc/rc.local中,然后执行source /etc/rc.local生效配置
测试
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name zangh
OK
127.0.0.1:6379> get name
"zangh"