CentOS_7.6 安装 Redis 6.0.1 以及开启自动启动相关问题[通俗易懂]

CentOS_7.6 安装 Redis 6.0.1 以及开启自动启动相关问题[通俗易懂]背景 阿里云的 ECS 服务器,什么软件都没有,需要的软件需要自己装。 下载 Redis 下载地址:https://redis.io/download 当前版本 Redis 6.0.1 升级 gcc…

CentOS_7.6 安装 Redis 6.0.1 以及开启自动启动相关问题

下载 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版本号即可。

代码100分

安装 Redis

进入 /usr/local 目录下

代码100分[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 文件中

代码100分[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

处理警告

配置完开机自动启动 Redis 服务之后,通过 redis-cli 登录发现有以下的警告:

2007:M 13 May 2020 23:19:49.615 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

2531:M 13 May 2020 23:29:58.615 # 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.

2531:M 13 May 2020 23:29:58.615 # 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.

第1个警告 (WARNING: The TCP backlog setting of 511 ……)解决办法

  • 方法1: 临时设置生效: sysctl -w net.core.somaxconn = 1024
  • 方法2: 永久生效: 修改/etc/sysctl.conf文件,增加一行 net.core.somaxconn= 1024 ,然后执行命令 sysctl -p

补充:

net.core.somaxconn是linux中的一个kernel参数,表示socket监听(listen)的backlog上限。

backlog是socket的监听队列,当一个请求(request)尚未被处理或建立时,他会进入backlog。

而socket server可以一次性处理backlog中的所有请求,处理后的请求不再位于监听队列中。

当server处理请求较慢,以至于监听队列被填满后,新来的请求会被拒绝。

所以说net.core.somaxconn限制了接收新 TCP 连接侦听队列的大小。

对于一个经常处理新连接的高负载 web服务环境来说,默认的 128 太小了。大多数环境这个值建议增加到 1024 或者更多。

第2个警告 (WARNING overcommit_memory is set to 0! ……)同样也有两个解决办法

  • 方法1: 临时设置生效: sysctl -w vm.overcommit_memory = 1
  • 方法2: 永久生效: 修改/etc/sysctl.conf文件,增加一行 vm.overcommit_memory = 1 然后执行命令 sysctl -p

补充:

overcommit_memory参数说明:

设置内存分配策略(可选,根据服务器的实际情况进行设置)

/proc/sys/vm/overcommit_memory

可选值:0、1、2。

0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。

1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。

2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

注意:redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。

第三个警告(WARNING you have Transparent Huge Pages (THP)),一样的道理,两种解决办法:

  • 方法1: 临时设置生效: echo never > /sys/kernel/mm/transparent_hugepage/enabled
  • 方法2: 永久生效: 在 /etc/init.d/redis_6379 写入代码,然后重新启动 redis。
    if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
        echo never > /sys/kernel/mm/transparent_hugepage/enabled
    fi
    

参考资料

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8475.html

(0)
上一篇 2023-02-27 16:30
下一篇 2023-02-27

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注