Redis安装配置
1. 从源码安装并编译Redis
这里安装的是最新的稳定版
wget https://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz
cd redis-stable
make
make install
2.调整系统参数
在文件/etc/sysctl.conf在末尾加上:
cat >> /etc/sysctl.d/99-sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p
修改文件/etc/security/limits.d/20-nproc.conf
* soft nofile 10241
* hard nofile 10241
立即生效
ulimit -n 10241
创建用户redis
mkdir /etc/redis
# 存放日志
mkdir /var/log/redis
chown redis:redis /var/log/redis
# 存放PID文件
mkdir /var/run/redis
chown -R redis:redis /var/run/redis
3. 编辑配置文件
cp /usr/local/redis-6.2.3/redis.conf /etc/redis/redis_6379.conf
# /etc/redis/redis_6379.conf
bind 0.0.0.0
daemonize yes
pidfile /var/run/redis/redis_6379.pid
logfile /var/log/redis/redis_6379.log
dbfilename dump_6379.rdb # 多实例时需要修改
appendfilename "appendonly_6379.aof" # 多实例时需要修改
dir /var/lib/redis
4. 使用system管理
可以参考源码中的 utils/systemd-redis_server.service
文件
cat >/usr/lib/systemd/system/redis_server.service <<EOF
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
LimitNOFILE=10032
PIDFile=/var/run/redis_6399.pid
ExecStart=/usr/local/bin/redis-server /etc/redis_6399.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
设置开机自启动
systemctl daemon-reload
systemctl start redis_server.service
systemctl enable redis_server.service
多实例管理
参考/usr/local/redis-6.2.3/utils/systemd-redis_multiple_servers@.service
文件
[Unit]
Description=Redis data structure server - instance %i
Documentation=https://redis.io/documentation
# This template unit assumes your redis-server configuration file(s)
# to live at /etc/redis/redis_server_<INSTANCE_NAME>.conf
AssertPathExists=/etc/redis/redis_%i.conf
#Before=your_application.service another_example_application.service
AssertPathExists=/var/lib/redis
[Service]
Type=forking
LimitNOFILE=10032
PIDFile=/var/run/redis/redis_%i.pid
ExecStart=/usr/local/bin/redis-server /etc/redis/redis_%i.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
User=redis
Group=redis
WorkingDirectory=/var/lib/redis
[Install]
WantedBy=multi-user.target