Redis官网

安装操作步骤

# 安装必要工具包
[root@centos7 ~]#yum -y install gcc jemalloc-devel

# 解压
[root@centos7 ~]#tar xvf redis-5.0.9.tar.gz

# 进入目录
[root@centos7 ~]#cd redis-5.0.9/

# 编译安装
[root@centos7 redis-5.0.9]#make PREFIX=/apps/redis install

redis配置

配置环境变量

[root@centos7 redis-5.0.9]#echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
[root@centos7 redis-5.0.9]#. /etc/profile.d/redis.sh

准备目录

[root@centos7 redis-5.0.9]#mkdir /apps/redis/{etc,log,data,run}

准备配置文件

[root@centos7 redis-5.0.9]#cp ~/redis-5.0.9/redis.conf /apps/redis/etc/

修改配置文件

[root@centos7 redis-5.0.9]#vim /apps/redis/etc/
bind 0.0.0.0                                #绑定监听IP
daemonize yes                               #后台启动redis
logfile /apps/redis/log/redis.log           #log日志存放路径
dir /apps/redis/data                        #RDB,AOF文件存放路径
pidfile /apps/redis/run/redis_6379.pid      #pidfile存放路径

启动redis

[root@centos7 redis-5.0.9]#redis-server /apps/redis/etc/redis.conf

准备redis.service文件

#可以参照yum下载后自动生成的service文件进行修改编写。
[root@centos7 redis-5.0.9]#cat > redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

创建redis用户

[root@centos7 redis-5.0.9]#useradd -r -s /sbin/nologin redis

杀掉redis服务,使用systemctl来启动redis

[root@centos7 ~]#killall redis-server
[root@centos7 ~]#systemctl start redis
Job for redis.service failed because the control process exited with error code. 
See "systemctl status redis.service" and "journalctl -xe" for details.

# 这里报错是因为我们刚刚已经是用root账号启动过一次redis
# 生成了一些文件,这次我们使用redis账号启动没有权限打开这些文件导致的。

# 查看一下文件
[root@centos7 ~]#ll /apps/redis/log/ /apps/redis/data/
/apps/redis/data/:
total 4
-rw-r--r-- 1 root root 92 Oct 22 10:10 dump.rdb

/apps/redis/log/:
total 4
-rw-r--r-- 1 root root 2880 Oct 22 10:10 redis.log

# 根据提示使用journalctl -xe命令查看一下log,可以找到 Can't open the log file: Permission denied

-- Subject: Unit redis.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit redis.service has begun starting up.
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: redis.service: main process exited, code=exited, status=1/FAILURE
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: *** FATAL CONFIG FILE ERROR ***
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: Reading the configuration file, at line 171
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: >>> 'logfile /apps/redis/log/redis.log'
Oct 22 10:10:20 centos7.wuvikr.4 redis-server[28068]: Can't open the log file: Permission denied
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: Failed to start Redis persistent key-value database.
-- Subject: Unit redis.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit redis.service has failed.
--
-- The result is failed.
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: Unit redis.service entered failed state.
Oct 22 10:10:20 centos7.wuvikr.4 systemd[1]: redis.service failed.
Oct 22 10:10:20 centos7.wuvikr.4 polkitd[689]: Unregistered Authentication Agent for unix-process:28062:113465 (system bus name :1.23, object path /org/freedesktop/PolicyKit1/Authe
lines 2050-2090/2090 (END)

修改权限

[root@centos7 redis-5.0.9]#chown -R redis.redis /apps/redis/

再次启动redis

# 正常启动
[root@centos7 ~]#systemctl start redis
[root@centos7 ~]#redis-cli
127.0.0.1:6379>

解决直接启动redis的三个警告

# 查看log文件会发现有三个警告,如果没有配置后台启动redis,下面的内容会出现在终端中。
[root@centos7 redis-5.0.9]#vim /apps/redis/log/redis.log
6278:C 20 Oct 2020 16:02:31.460 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6278:C 20 Oct 2020 16:02:31.460 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=6278, just started
6278:C 20 Oct 2020 16:02:31.460 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
6278:M 20 Oct 2020 16:02:31.460 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6278
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

6278:M 20 Oct 2020 16:02:31.461 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6278:M 20 Oct 2020 16:02:31.461 # Server initialized
6278:M 20 Oct 2020 16:02:31.461 # WARNING overcommit_memory is set to 0! Background savemay 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.
6278:M 20 Oct 2020 16:02:31.461 # 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.
6278:M 20 Oct 2020 16:02:31.461 * DB loaded from disk: 0.000 seconds
6278:M 20 Oct 2020 16:02:31.461 * Ready to accept connections

tcp-backlog

backlog 参数控制的是三次握手后server端收到的client ack确认号之后的队列值,即全连接队列(accept queue)

# 根据警告提示修改为大于511以上的值即可
[root@centos7 redis-5.0.9]#vim /etc/sysctl.conf
net.core.somaxconn=1024
net.ipv4.tcp_max_syn_backlog=1024

[root@centos7 redis-5.0.9]#sysctl -p

vm.overcommit_memory

内核控制进程的内存申请的策略,有0,1,2三种。

  • 0表示内核会检查是否有足够的物理内存,有就允许,没有就拒绝;
  • 1表示内核允许分配所有物理内存而不管当前内存使用状态如何;
  • 2表示内核允许分配超过物理内存和交换空间总和的内存。
# 根据警告提示修改为1
[root@centos7 redis-5.0.9]#vim /etc/sysctl.conf
vm.overcommit_memory=1

[root@centos7 redis-5.0.9]#sysctl -p

Transparent Huge Pages

大页内存动态分配

# 根据警告提示关闭大页,并写入到rc.local文件中。
[root@centos7 redis-5.0.9]#echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
[root@centos7 redis-5.0.9]#chmod +x /etc/rc.local
[root@centos7 redis-5.0.9]#. /etc/rc.local

配置完重新启动redis,就没有这三个警告了。

以上