1、编译环境准备
- 替换国内yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum -y install perl gcc gcc-c++ make cmake kernel kernel-headers kernel-devel net-tools git wget bash-completion
2、下载redis源码文件
wegt https://download.redis.io/releases/redis-5.0.13.tar.gz?_ga=2.167320253.2093650531.1628407064-1440117025.1628407064
tar -zxvf redis-5.0.13.tar.gz -C /usr/local/src/
3、编译redis
cd /usr/local/src/redis-5.0.13
make
当出现Hint: It's a good idea to run 'make test' ;)
为编译成功
编译成功之后,进入src目录,执行如下命令:
make install PREFIX=/usr/local/redis
如果出现如上图所示,提示"Hint: It’s a good idea to run ‘make test’ 😉",代表安装成功
4、启动服务:
(1)前台启动
进入cd /usr/local/src/redis-5.0.13/src目录,输入如下命令启动
cd /usr/local/src/redis-5.0.13/src
./redis-server
(2)后台启动
1)进入/usr/local/redis,该目录存在一个bin文件存放这可执行文件,在/usr/local/redis中新建一个conf目录用于存放redis的配置文件,并将/usr/local/src/redis-5.0.13中的redis.conf复制到/usr/local/redis/conf中
cd /usr/local/redis
mkdir conf
cp /usr/local/src/redis-5.0.13/redis.conf /usr/local/redis/conf/
2)输入如下命令查看redis.conf文件
vi redis.conf
按"i"键进入编辑模式,把 daemonize no改为 daemonize yes,
bind ‘127.0.0.0.1’ 改为 bind ‘0.0.0.0’
logfile “” 改为 logfile “/var/log/redis/redis.log”
按"ESC"键退出,按":wq"保存并退出
3)输入如下命令进行后台启动
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
5、设置开机启动
(1)输入如下命令
vi /etc/systemd/system/redis.service
按"i"键进入编辑模式,输入如下代码,按"ESC"键退出编辑模式,输入":wq!"命令保存并退出
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注意:ExecStart配置成自己的路径
(2)输入如下命令设置开机启动
systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service
6、创建redis命令软连接
ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
这样在/usr/bin/目录下就创建了一个redis文件
7、测试redis
redis
ping
8、redis常用命令如下
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 #停止开机自启动
9、redis 日志查看
查看日志:
tailf -n30 /var/log/redis/redis.log
27339:M 08 Aug 2021 16:07:16.157 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.13 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 27339
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
27339:M 08 Aug 2021 16:07:16.161 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
27339:M 08 Aug 2021 16:07:16.161 # Server initialized
27339:M 08 Aug 2021 16:07:16.161 # 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.
27339:M 08 Aug 2021 16:07:16.161 # 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.
27339:M 08 Aug 2021 16:07:16.161 * DB loaded from disk: 0.000 seconds
27339:M 08 Aug 2021 16:07:16.161 * Ready to accept connections
10、redis启动日志三个警告提示
1) tcp-backlog
backlog 参数控制的是三次握手的时候server端收到client ack确认号之后的队列值,即全连接队列
# vim /etc/sysctl.conf
net.core.somaxconn = 1024
# sysctl -p
2) vm.overcommmit_memory
内核参数说明:\
- 0、表示内核将检查是否有足够的可用内存供应用进程使用:如果有,内存允许申请,否则,内存申请失败,并把错误返回给应用进程.
- 1、表示内核允许分配所有的物理内存,而不管当前的内存状态如何
- 2、表示内核允许分配超过所有物理内存和交换空间总和的内存
范例:
# vim /etc/sysctl.conf
vm.overcommit_memory = 1
# sysctl -p
3) transparent hugepage
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.
警告:内核中启用了透明大页面(THP)支持。这将造成Redis的延迟和内存使用问题。要解决此问题,请以root用户身份运行命令’echo never>/sys/kernel/mm/transparent\u hugepage/enabled’,并将其添加到/etc/rc.local,以便在重新启动后保留该设置。禁用THP后,必须重新启动Redis。
范例:
# 永久禁用(开机执行禁用操作)
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
# 临时禁用,重启后失效
echo never > /sys/kernel/mm/transparent_hugepage/enabled