Centos7 生产环境安装 Redis(单机)

系统环境

1
2
CentOS Linux release 7.6.1810 (Core)
Linux centos7 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

安装 Supervisor

1
2
3
4
5
6
7
8
9
10
11
12
13
# 提示:supervisor主要用于管理redis的开机自启动(带守护进程)

# 安装
# yum install -y supervisor

# 开机自启动
# systemctl enable supervisord

# 启动服务
# systemctl start supervisord

# 查看服务状态
# systemctl status supervisord

更改系统的最大打开文件描述符数

创建 Redis 用户和用户组

1
2
3
4
5
6
7
8
# 切换root用户
$ sudo -i

# 创建redis用户组
# groupadd redis

# 创建redis用户(不设置用户密码,不允许远程登录)
# useradd redis -g redis

下载 Redis

1
2
3
4
5
6
7
8
9
10
11
12
# 创建下载目录
# mkdir -p /home/redis/software

# 下载
# cd /home/redis/software
# wget http://download.redis.io/releases/redis-5.0.5.tar.gz

# 解压
# tar -xvf redis-5.0.5.tar.gz

# 删除下载文件
# rm -f redis-5.0.5.tar.gz

编译安装 Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 进入解压目录
# cd /home/redis/software/redis-5.0.5

# 编译
# make

# 创建安装目录
# mkdir -p /usr/local/redis

# 安装
# cd /home/redis/software/redis-5.0.5/src
# make PREFIX=/usr/local/redis install

# 备份配置文件
# cp /home/redis/software/redis-5.0.5/redis.conf /etc/redis.conf
# cp /home/redis/software/redis-5.0.5/redis.conf /etc/redis.conf.default

# 文件授权
# chown -R redis:redis /usr/local/redis
# chown redis:redis /etc/redis.conf
# chown redis:redis /etc/redis.conf.default

Redis 基础配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 切换redis用户
$ sudo -i su redis

# 创建数据目录、日志目录、日志文件
$ mkdir /usr/local/redis/data
$ mkdir /usr/local/redis/logs
$ touch /usr/local/redis/logs/redis.log

# 编辑配置文件
$ vim /etc/redis.conf
maxclients 10000 #最大连接数
# bind 127.0.0.1 #注释掉IP绑定
protected-mode no #关闭保护模式
requirepass yourPassword #设置访问密码
dir /usr/local/redis/data/ #指定数据目录
logfile "/usr/local/redis/logs/redis.log" #指定日志文件

# 注意:当前使用RDB持久化机制,由于Redis默认启用RDB,因此上面无需手动配置,除非需要更改快照存盘的频率

开机自启动 Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 切换root用户
$ sudo -i

# 配置redis前台运行
# vim /usr/local/redis/conf/redis.conf
daemonize no

# 创建redis的supervistor配置文件
# touch /etc/supervisord.d/redis.ini

# 编辑redis的supervistor配置文件,添加以下配置内容(考虑到服务器安全,必须指定使用redis用户启动redis服务)
# vim /etc/supervisord.d/redis.ini
[program:redis]
directory=/usr/local/redis
command=/usr/local/redis/bin/redis-server /etc/redis.conf
user=redis
numprocs=1
autostart=true
autorestart=true
startretries=10
process_name=%(program_name)s

# 重载supervistor的配置文件,会自动启动redis服务
# supervisorctl reload

# 查看redis的运行状态
# supervisorctl status redis
redis RUNNING pid 31513, uptime 0:03:23 #如果输出此日志信息,说明redis启动成功,否则查看redis的启动日志来排查问题

# 注意:当redis通过supervistor管理自启动的情况下,不能简单使用“redis-cli shutdown“命令来关闭redis服务,具体的redis服务管理可参考下面的内容。

配置防火墙

1
2
3
4
5
6
7
8
# 开放端口
# firewall-cmd --zone=public --permanent --add-port=6379/tcp

# 保存防火墙配置
# firewall-cmd --reload

# 查看已开放的端口
# firewall-cmd --list-ports

管理 Redis 服务

1
2
3
4
5
6
7
8
9
10
11
# 关闭
# supervistorctl stop redis

# 启动
# supervistorctl start redis

# 重启
# supervistorctl restart redis

# 查看状态
# supervistorctl status redis

配置概述

1
2
3
4
5
配置文件:/etc/redis.conf
安装目录:/usr/local/redis/
数据目录:/usr/local/redis/data
日志文件:/usr/local/redis/logs/redis.log
redis的supervistor配置文件:/etc/supervisord.d/redis.ini

Redis 启动错误一

1
2
3
44969:M 23 Nov 2018 12:57:46.920 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
44969:M 23 Nov 2018 12:57:46.920 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
44969:M 23 Nov 2018 12:57:46.920 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.

一般情况下,如果已经更改了系统的最大打开文件描述符数,不会再出现以上的错误信息,请检查最大打开文件描述符数的更改是否生效。

Redis 启动错误二

1
2
3
4
44969:M 23 Nov 2018 12:57:46.921 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
44969:M 23 Nov 2018 12:57:46.921 # Server initialized
44969:M 23 Nov 2018 12:57:46.921 # 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.
1
2
3
4
5
6
7
8
9
10
11
# 编辑配置文件,添加以下内容
# vim /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 1024

# 重启系统
# reboot

# 验证是否生效
# sysctl net.core.somaxconn
# sysctl vm.overcommit_memory

Redis 启动错误三

1
2
44969:M 23 Nov 2018 12:57:46.922 # 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
2
3
4
5
6
7
8
9
10
11
12
13
14
# 执行以下命令
# echo never > /sys/kernel/mm/transparent_hugepage/enabled

# 编辑配置文件,在文件的末尾添加以下内容
# vim /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi

# 配置文件授权
# chmod +x /etc/rc.d/rc.local

# 重启系统
# reboot