前言
Redis下载列表:http://download.redis.io/releases
安装redis
下载redis
提示:为了方便管理下载后的安装包,建议将需要编译的安装包统一放置在 /usr/src 目录下
进入目录
cd /usr/src
下载软件包
wget http://download.redis.io/releases/redis-5.0.6.tar.gz
也可以在电脑下载好,通过FTP传输到服务器目录
解压redis包
tar -zxvf redis-5.0.6.tar.gz
进入目录
cd redis-5.0.6
编译环境
编译安装redis需要 gcc 编译环境,如果没有需安装一下
yum install -y gcc
安装
make PREFIX=/usr/local/redis install
如果一切正常的话,会返回以下结果
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
创建配置文件
在安装包目录里执行,并将其复制到正确的位置
mkdir /usr/local/redis/etc
cp redis.conf /usr/local/redis/etc/redis.conf
cp sentinel.conf /usr/local/redis/etc/redis-sentinel.conf
添加redis用户
如果原先没有创建 redis 用户和 redis 用户组,请按运行以下命令,如果已创建,请直接查看下一步骤。
检测是否已创建redis用户
id redis
输出
id: redis: no such user
创建redis用户组
groupadd redis
创建redis用户,并设置不允许登录
useradd -g redis -s /sbin/nologin redis
再次检查redis用户
id redis
输出
uid=1001(redis) gid=1001(redis) groups=1001(redis)
配置redis
redis配置文件位置:/usr/local/redis/etc/redis.conf
开启守护进程
修改 redis.conf
文件,将 daemonize no 改成 daemonize yes
# 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.
daemonize yes
设置外网可访问
找到
bind 127.0.0.1
改成
#bind 127.0.0.1
找到
protected-mode no
改成
protected-mode yes
设置密码
为了安全性,最好设置密码
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
#
requirepass 密码
设置日志目录
找到
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""
改成
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile /usr/local/redis/var/log/redis.log
创建日志目录
mkdir -p /usr/local/redis/var/log && chown redis:redis /usr/local/redis/var/log
设置备份目录
找到
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
改成
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /usr/local/redis/var/lib
创建备份目录
mkdir -p /usr/local/redis/var/lib && chown redis:redis /usr/local/redis/var/lib
启动redis
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
输出
5748:C 27 Sep 2019 22:08:18.958 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5748:C 27 Sep 2019 22:08:18.958 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=5748, just started
5748:C 27 Sep 2019 22:08:18.958 # Configuration loaded
进入控制台
如果设置了密码,需输入密码登录才有执行权限,不然控制台会提示 (error) NOAUTH Authentication required.
无密码登录
/usr/local/redis/bin/redis-cli -u 127.0.0.1 -p 6379
密码登录
/usr/local/redis/bin/redis-cli -u 127.0.0.1 -p 6379 -a 密码
配置环境变量
新建一个 redis 环境变量文件
touch /etc/profile.d/redis.sh
打开redis.sh
文件并写入
vim /etc/profile.d/redis.sh
将内容修改成
PATH=$PATH:/usr/local/redis/bin
export PATH
使配置文件生效
source /etc/profile
然后可以运行
redis-server -v
输出
Redis server v=5.0.6 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=6d3d6820935bb747
redis服务化
新建redis服务文件
新建服务文件
touch /usr/lib/systemd/system/redis.service
打开服务文件
vim /usr/lib/systemd/system/redis.service
将内容修改成
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf --supervised systemd
ExecStop=/usr/local/redis/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
重新加载systemctll配置
systemctl daemon-reload
新建redis关闭脚本
新建脚本文件
mkdir /usr/local/redis/libexec
touch /usr/local/redis/libexec/redis-shutdown
chmod 755 /usr/local/redis/libexec/redis-shutdown
打开脚本文件
vim /usr/local/redis/libexec/redis-shutdown
将内容修改成
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/redis/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/usr/local/redis/etc/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
PORT=${PORT:-6379}
else
PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
停止运行中的redis进程
查看当前进程列表是否存在 redis
进程:
ps aux | grep redis
输出:
root 18512 0.1 1.6 163112 16248 ? Ssl 14:37 0:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root 18525 0.0 0.0 112728 972 pts/0 R+ 14:48 0:00 grep --color=auto redis
如果存在 redis 进程,需先终止该进程
kill 18512
使用systemctl管理redis
启动服务
systemctl start redis
查看状态
systemctl status redis
停止服务
systemctl stop redis
重启服务
systemctl restart redis
开机自启动
systemctl enable redis
禁止开机自启动
systemctl disable redis
清理临时文件
要养成好习惯,每次编译完后都要把应用包解压出来的文件或目录进行删除。
rm -rf /usr/src/redis-5.0.6
到这里,Centos7 安装redis就安装配置好了。
解决问题
在redis启动后,redis.log日志出现一些错误信息,我们来解决一下。
2539:M 29 Sep 2019 00:18:47.555 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2539:M 29 Sep 2019 00:18:47.555 # Server initialized
2539:M 29 Sep 2019 00:18:47.555 # 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.
2539:M 29 Sep 2019 00:18:47.555 # 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.
2539:M 29 Sep 2019 00:18:47.555 * Ready to accept connections
2539:M 29 Sep 2019 00:19:55.969 # User requested shutdown...
2539:M 29 Sep 2019 00:19:55.969 * Saving the final RDB snapshot before exiting.
2539:M 29 Sep 2019 00:19:55.970 * DB saved on disk
2539:M 29 Sep 2019 00:19:55.970 * Removing the pid file.
2539:M 29 Sep 2019 00:19:55.971 # Redis is now ready to exit, bye bye...
2575:C 29 Sep 2019 00:20:22.094 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2575:C 29 Sep 2019 00:20:22.094 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=2575, just started
2575:C 29 Sep 2019 00:20:22.094 # Configuration loaded
2575:C 29 Sep 2019 00:20:22.094 * supervised by systemd, will signal readiness
2575:M 29 Sep 2019 00:20:22.095 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
2575:M 29 Sep 2019 00:20:22.095 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
2575:M 29 Sep 2019 00:20:22.095 # 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'.
问题: 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 "net.core.somaxconn = 1024" >> /etc/sysctl.d/99-sysctl.conf
使其生效:
sysctl -p
重启redis
问题: 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.
解决:
echo "vm.overcommit_memory = 1" >> /etc/sysctl.d/99-sysctl.conf
使其生效:
sysctl -p
重启redis
问题: 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.
解决:
在 /etc/rc.d/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
最后重启系统
重启后再查看redis日志,发现已经没报错了
1231:M 02 Oct 2019 11:26:05.334 # Server initialized
1231:M 02 Oct 2019 11:26:05.338 * DB loaded from disk: 0.004 seconds
1231:M 02 Oct 2019 11:26:05.338 * Ready to accept connections