下載 Redis
下載地址:https://redis.io/download
當前版本 Redis 6.0.1
升級 gcc
# 查看gcc版本是否在5.3以上,centos7.6默認安裝4.8.5
[root@liukai-ecs-01 system]# gcc -v
# 升級gcc到5.3及以上,如下:
升級到gcc 9.3:
[root@liukai-ecs-01 system]# yum -y install centos-release-scl
[root@liukai-ecs-01 system]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@liukai-ecs-01 system]# scl enable devtoolset-9 bash
需要注意的是scl命令啓用只是臨時的,退出shell或重啓就會恢復原系統gcc版本。
如果要長期使用gcc 9.3的話:
[root@liukai-ecs-01 system]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
這樣退出shell重新打開就是新版的gcc了
以下其他版本同理,修改devtoolset版本號即可。
安裝 Redis
進入 /usr/local 目錄下
[root@liukai-ecs-01 local]# ls
redis-6.0.1.tar.gz
[root@liukai-ecs-01 local]# tar xf redis-6.0.1.tar.gz -C redis/
[root@liukai-ecs-01 local]# cd redis/
[root@liukai-ecs-01 local]# make && make install
其他命令
# 編譯出錯時,清出編譯生成的文件
make distclean
# 編譯安裝到指定目錄下
make PREFIX=/usr/local/redis install
# 卸載
make uninstall
設置開機自動啓動
方法一:通過 systemctl 配置自動啓動
進入 cd /usr/local/redis 目錄下,修改 redis.conf 文件中
[root@liukai-ecs-01 local]# cd /usr/local/redis
[root@liukai-ecs-01 redis]# vim redis.conf
...
# By default Redis does not run as a daemon. Use "yes" if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# 將 no 改爲 yes,表示以後臺方式啓動服務
daemonize yes
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
# 將 no 改爲 systemd,表示以 CentOS systemd 系統服務方式啓動
supervised systemd
進入 /etc/systemd/system 目錄,創建 redis-server.service 文件
[root@liukai-ecs-01 redis]# cd /etc/systemd/system
[root@liukai-ecs-01 system]# vim redis-server.service
# example systemd service unit file for redis-server
#
# In order to use this as a template for providing a redis service in your
# environment, _at the very least_ make sure to adapt the redis configuration
# file you intend to use as needed (make sure to set "supervised systemd"), and
# to set sane TimeoutStartSec and TimeoutStopSec property values in the unit"s
# "[Service]" section to fit your needs.
#
# Some properties, such as User= and Group=, are highly desirable for virtually
# all deployments of redis, but cannot be provided in a manner that fits all
# expectable environments. Some of these properties have been commented out in
# this example service unit file, but you are highly encouraged to set them to
# fit your needs.
#
# Please refer to systemd.unit(5), systemd.service(5), and systemd.exec(5) for
# more information.
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
#Before=your_application.service another_example_application.service
#AssertPathExists=/var/lib/redis
[Service]
#ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize yes
## Alternatively, have redis-server load a configuration file:
#ExecStart=/usr/local/bin/redis-server /path/to/your/redis.conf
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
LimitNOFILE=10032
NoNewPrivileges=yes
#OOMScoreAdjust=-900
#PrivateTmp=yes
#Type=notify
# 注意 notify 會失敗,換成 forking 方式啓動,讓主進程複製一個子進程的方式執行
Type=forking
#TimeoutStartSec=100
#TimeoutStopSec=100
UMask=0077
#User=root
#Group=root
#WorkingDirectory=/var/lib/redis
[Install]
WantedBy=multi-user.target
重新加載系統服務文件
[root@liukai-ecs-01 system]# systemctl daemon-reload
以系統服務方式啓動 redis-server
[root@liukai-ecs-01 system]# systemctl start redis-server.service
查看服務狀態
[root@liukai-ecs-01 system]# systemctl status redis-server.service
● redis-server.service - Redis data structure server
Loaded: loaded (/etc/systemd/system/redis-server.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2020-05-13 21:43:35 CST; 38min ago
Docs: https://redis.io/documentation
Main PID: 16153 (redis-server)
CGroup: /system.slice/redis-server.service
└─16153 /usr/local/bin/redis-server 127.0.0.1:6379
5月 13 21:43:35 liukai-ecs-01 systemd[1]: Starting Redis data structure server...
5月 13 21:43:35 liukai-ecs-01 redis-server[16152]: 16152:C 13 May 2020 21:43:35.196 # oO0OoO0OoO0...0Oo
5月 13 21:43:35 liukai-ecs-01 redis-server[16152]: 16152:C 13 May 2020 21:43:35.196 # Redis versi...ted
5月 13 21:43:35 liukai-ecs-01 redis-server[16152]: 16152:C 13 May 2020 21:43:35.196 # Configurati...ded
5月 13 21:43:35 liukai-ecs-01 redis-server[16152]: 16152:C 13 May 2020 21:43:35.196 # WARNING sup...it.
5月 13 21:43:35 liukai-ecs-01 redis-server[16152]: 16152:C 13 May 2020 21:43:35.196 # systemd sup...und
5月 13 21:43:35 liukai-ecs-01 systemd[1]: Started Redis data structure server.
Hint: Some lines were ellipsized, use -l to show in full.
查看 redis 是否啓動
[root@liukai-ecs-01 ~]# ps -ef | grep redis
root 519 1 0 22:24 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
root 1046 1028 0 22:25 pts/0 00:00:00 grep --color=auto redis
設置開機啓動啓動 redis 服務
[root@liukai-ecs-01 system]# systemctl enable redis-server.service
方法二:通過配置 /etc/init.d 啓動
/usr/local/redis/utils目錄下,有個 redis_init_script 腳本
將 redis_init_script 腳本拷貝到 /etc/init.d 目錄中
[root@liukai-ecs-01 redis] /usr/local/redis/utils
[root@liukai-ecs-01 ~] cp redis_init_script /etc/init.d/
# 將文件修改爲 redis_6379,6379 是 redis 的默認端口號
[root@liukai-ecs-01 init.d] cd /etc/init.d/
[root@liukai-ecs-01 init.d] mv redis_init_script redis_6379
創建兩個目錄:
/etc/redis(存放 redis 的配置文件)
/var/redis/6379(存放 redis 的持久化文件)
[root@liukai-ecs-01 init.d] mkdir /etc/redis
[root@liukai-ecs-01 init.d] mkdir /var/redis/
[root@liukai-ecs-01 init.d] mkdir /var/redis/6379
修改 redis 配置文件 redis.conf
該文件默認在 redis 安裝目錄下,拷貝到 /etc/redis 目錄中,修改名稱爲 6379.conf
[root@liukai-ecs-01 init.d] cp /usr/local/redis-3.2.8/redis.conf /etc/redis/
[root@liukai-ecs-01 init.d] cd /etc/redis/
[root@liukai-ecs-01 init.d] mv redis.conf 6379.conf
這裏爲什麼要這樣修改呢?是因爲 redis_init_script 腳本中的 conf 配置指定了該目錄下的 端口號.conf 文件
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
修改 redis.conf(6379.conf) 中的部分配置爲生產環境
daemonize yes // 讓redis以daemon進程運行
pidfile /var/run/redis_6379.pid // 設置redis的pid文件位置
port 6379 // 設置 redis的監聽端口號
dir /var/redis/6379 //設置持久化文件的存儲位置
啓動 redis
# 執行 redis_6379 腳本
[root@liukai-ecs-01 init.d] cd /etc/init.d
# 如果沒有執行權限的話,修改執行權限 ,可以使用 chmod u+x redis_6379
# chmod 777 redis_6379
[root@liukai-ecs-01 init.d] ./redis_6379 start
確認 redis 進程是否啓動,ps -ef | grep redis
讓 redis 跟隨系統啓動自動啓動
使用 chkconfig 命令開啓該文件的系統服務,
可以在 redis_6379 配置文件中上面添加 chkconfig 的註釋信息
如下,不要在 #!/bin/sh 上面添加
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
添加完成之後,使用以下命令開啓隨系統啓動
chkconfig redis_6379 on