redis 安装部署

Scroll Down

第一章:安装部署

  • 此案例以 redis5.0.7版本为例

00. reids 官网

https://redis.io/download

01. 目录规划

/data/soft 						下载软件存放目录
/data/redis_6379			 	 数据目录
/opt/redis_6379/{conf,logs,pid}	   配置目录,日志目录,pid目录

02. 配置目录列表

/opt/redis_6379/conf/redis_6379.conf	主要配置文件
/opt/redis_6379/pid/redis_6379.pid		pid文件
/opt/redis_6379/logs/redis_6379.log		日志文件

/usr/lib/systemd/system/redis.service	system启动启动配置文件

03. 安装命令

# 创建目录
mkdir -p /data/{soft, redis_6379}
mkdir -p /opt/redis_6379/{conf,logs,pid}

# 下载redis
cd /data/soft
wget http://download.redis.io/releases/redis-5.0.7.tar.gz

# 解压
tar xf redis-5.0.7.tar.gz -C /opt
cd /opt
ln -s redis-5.0.7 redis

# 安装
cd /opt/redis
make
make install

04. 编写配置文件

cat > /opt/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 127.0.0.1 10.0.0.51
port 6379
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
EOF

05. 启动命令

redis-server /opt/redis_6379/conf/redis_6379.conf

06. 检查是否启动成功

ps -ef | grep redis
netstat -lntup | grep 6379

07. 连接redis

[root@db01 opt]# redis-cli
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"

08. 关闭命令

第一种:

[root@db01 opt]# redis-cli shutdown

第二种:

[root@db01 opt]# redis-cli
127.0.0.1:6379> SHUTDOWN

第三种:

kill
pkill

09. system 启动配置

1)创建运行用户,并授权

# 关闭redis
redis-cli shutdown

# 创建运行 redis 用户
groupadd redis -g 1000
useradd redis -u 1000 -g 1000 -M -s /sbin/noloagin

# 授权
chown -R redis.redis /opt/redis*
chown -R redis.redis /data/redis*

2)配置文件

cat >/usr/lib/systemd/system/redis.service<<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6379/conf/redis_6379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

3) 启动测试

systemctl daemon-reload
systemctl restart redis

10. 优化警告

警告1:maximum open files过低

1630:M 25 Jun 2020 10:38:46.680 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
1630:M 25 Jun 2020 10:38:46.680 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
1630:M 25 Jun 2020 10:38:46.680 # 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'.

解决方法:在systemd启动配置文件添加参数

vim /usr/lib/systemd/system/redis.service
[Service]
..............
LimitNOFILE=65536

警告2:overcommit_memory设置 虚拟内存相关

1630:M 25 Jun 2020 10:38:46.681 # 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.

解决方法:

sysctl vm.overcommit_memory=1

警告3:关闭THP大内存页

1630:M 25 Jun 2020 10:38:46.681 # 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.

解决:

echo never > /sys/kernel/mm/transparent_hugepage/enabled

警告4:

1630:M 25 Jun 2020 10:38:46.681 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

解决:

echo "511" > /proc/sys/net/core/somaxconn
sysctl net.core.somaxconn=4096

重启reddis

systemctl daemon-reload
systemctl restart redis

查看日志:所有警告都优化完毕

[root@db01 opt]# tailf /opt/redis_6379/logs/redis_6379.log
1754:M 25 Jun 2020 11:37:06.050 # User requested shutdown...
1754:M 25 Jun 2020 11:37:06.050 * Removing the pid file.
1754:M 25 Jun 2020 11:37:06.050 # Redis is now ready to exit, bye bye...
1815:C 25 Jun 2020 11:37:06.055 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1815:C 25 Jun 2020 11:37:06.055 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=1815, just started
1815:C 25 Jun 2020 11:37:06.055 # Configuration loaded
1815:C 25 Jun 2020 11:37:06.055 * supervised by systemd, will signal readiness
1815:M 25 Jun 2020 11:37:06.056 * Running mode=standalone, port=6379.
1815:M 25 Jun 2020 11:37:06.056 # Server initialized
1815:M 25 Jun 2020 11:37:06.056 * Ready to accept connections