源碼編譯安裝
安裝操作步驟
# 安裝必要工具包
[root@centos7 ~]#yum -y install gcc jemalloc-devel
# 解壓
[root@centos7 ~]#tar xvf redis-5.0.9.tar.gz
# 進入目錄
[root@centos7 ~]#cd redis-5.0.9/
# 編譯安裝
[root@centos7 redis-5.0.9]#make PREFIX=/apps/redis install
redis配置
配置環境變量
[root@centos7 redis-5.0.9]#echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
[root@centos7 redis-5.0.9]#. /etc/profile.d/redis.sh
准備目錄
[root@centos7 redis-5.0.9]#mkdir /apps/redis/{etc,log,data,run}
准備配置文件
[root@centos7 redis-5.0.9]#cp ~/redis-5.0.9/redis.conf /apps/redis/etc/
修改配置文件
[root@centos7 redis-5.0.9]#vim /apps/redis/etc/
bind 0.0.0.0 #綁定監聽IP
daemonize yes #后台啟動redis
logfile /apps/redis/log/redis.log #log日志存放路徑
dir /apps/redis/data #RDB,AOF文件存放路徑
pidfile /apps/redis/run/redis_6379.pid #pidfile存放路徑
啟動redis
[root@centos7 redis-5.0.9]#redis-server /apps/redis/etc/redis.conf
准備redis.service文件
#可以參照yum下載后自動生成的service文件進行修改編寫。
[root@centos7 redis-5.0.9]#cat > redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
創建redis用戶
[root@centos7 redis-5.0.9]#useradd -r -s /sbin/nologin redis
殺掉redis服務,使用systemctl來啟動redis
[root@centos7 ~]#killall redis-server
[root@centos7 ~]#systemctl start redis
Job for redis.service failed because the control process exited with error code.
See "systemctl status redis.service" and "journalctl -xe" for details.
# 這里報錯是因為我們剛剛已經是用root賬號啟動過一次redis
# 生成了一些文件,這次我們使用redis賬號啟動沒有權限打開這些文件導致的。
# 查看一下文件
[root@centos7 ~]#ll /apps/redis/log/ /apps/redis/data/
/apps/redis/data/:
total 4
-rw-r--r-- 1 root root 92 Oct 22 10:10 dump.rdb
/apps/redis/log/:
total 4
-rw-r--r-- 1 root root 2880 Oct 22 10:10 redis.log
# 根據提示使用journalctl -xe命令查看一下log,可以找到 Can't open the log file: Permission denied
-- Subject: Unit redis.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit redis.service has begun starting up.
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: redis.service: main process exited, code=exited, status=1/FAILURE
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: *** FATAL CONFIG FILE ERROR ***
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: Reading the configuration file, at line 171
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: >>> 'logfile /apps/redis/log/redis.log'
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: Can't open the log file: Permission denied
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: Failed to start Redis persistent key-value database.
-- Subject: Unit redis.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit redis.service has failed.
--
-- The result is failed.
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: Unit redis.service entered failed state.
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: redis.service failed.
Oct 22 10:10:20 centos7.wuvikr.4 polkitd[689]: Unregistered Authentication Agent for unix-process:28062:113465 (system bus name :1.23, object path /org/freedesktop/PolicyKit1/Authe
lines 2050-2090/2090 (END)
修改權限
[root@centos7 redis-5.0.9]#chown -R redis.redis /apps/redis/
再次啟動redis
# 正常啟動
[root@centos7 ~]#systemctl start redis
[root@centos7 ~]#redis-cli
127.0.0.1:6379>
解決直接啟動redis的三個警告
# 查看log文件會發現有三個警告,如果沒有配置后台啟動redis,下面的內容會出現在終端中。
[root@centos7 redis-5.0.9]#vim /apps/redis/log/redis.log
6278:C 20 Oct 2020 16:02:31.460 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6278:C 20 Oct 2020 16:02:31.460 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=6278, just started
6278:C 20 Oct 2020 16:02:31.460 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
6278:M 20 Oct 2020 16:02:31.460 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 6278
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
6278:M 20 Oct 2020 16:02:31.461 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6278:M 20 Oct 2020 16:02:31.461 # Server initialized
6278:M 20 Oct 2020 16:02:31.461 # WARNING overcommit_memory is set to 0! Background savemay 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.
6278:M 20 Oct 2020 16:02:31.461 # 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.
6278:M 20 Oct 2020 16:02:31.461 * DB loaded from disk: 0.000 seconds
6278:M 20 Oct 2020 16:02:31.461 * Ready to accept connections
tcp-backlog
backlog 參數控制的是三次握手后server端收到的client ack確認號之后的隊列值,即全連接隊列(accept queue)
# 根據警告提示修改為大於511以上的值即可
[root@centos7 redis-5.0.9]#vim /etc/sysctl.conf
net.core.somaxconn=1024
net.ipv4.tcp_max_syn_backlog=1024
[root@centos7 redis-5.0.9]#sysctl -p
vm.overcommit_memory
內核控制進程的內存申請的策略,有0,1,2三種。
- 0表示內核會檢查是否有足夠的物理內存,有就允許,沒有就拒絕;
- 1表示內核允許分配所有物理內存而不管當前內存使用狀態如何;
- 2表示內核允許分配超過物理內存和交換空間總和的內存。
# 根據警告提示修改為1
[root@centos7 redis-5.0.9]#vim /etc/sysctl.conf
vm.overcommit_memory=1
[root@centos7 redis-5.0.9]#sysctl -p
Transparent Huge Pages
大頁內存動態分配
# 根據警告提示關閉大頁,並寫入到rc.local文件中。
[root@centos7 redis-5.0.9]#echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
[root@centos7 redis-5.0.9]#chmod +x /etc/rc.local
[root@centos7 redis-5.0.9]#. /etc/rc.local