下载 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