【目标】
配置 redis 单机主从,6378 端口连接 master,6379 端口连接 slave,master 可读写,slave 只读。
【安装及测试】
sudo apt update
sudo apt install redis
walker@Ubuntu:~$ redis-cli
127.0.0.1:6379> ping
PONG
# 查看状态
$ systemctl status redis
# 启动redis
$ systemctl start redis
#重启redis
$ systemctl restart redis
# 停止redis
$ systemctl stop redis
# 将 redis 服务加入开机启动
$ systemctl enable redis
# 禁止开机启动
$ systemctl disable redis
【配置单机主从】
mkdir -p /data/redis/{6378-master,6379-slave}/{conf,db,log}
cp /etc/redis/redis.conf /data/redis/6378-master/conf/
cp /etc/redis/redis.conf /data/redis/6379-slave/conf/
daemonize yes
pidfile /data/redis/6378-master/redis.pid
bind 0.0.0.0
port 6378
logfile /data/redis/6378-master/log/redis.log
dir /data/redis/6378-master/db/
daemonize yes
pidfile /data/redis/6379-slave/redis.pid
bind 0.0.0.0
port 6379
logfile /data/redis/6379-slave/log/redis.log
dir /data/redis/6379-slave/db/
slaveof 127.0.0.1 6378
sudo systemctl stop redis
sudo systemctl disable redis
sudo redis-server /data/redis/6378-master/conf/redis.conf # 启动 master
sudo redis-server /data/redis/6379-slave/conf/redis.conf # 启动 slave
# ps aux | grep redis
root 6069 0.0 0.1 50148 3952 ? Ssl 03:15 0:00 redis-server 0.0.0.0:6378
root 6451 0.0 0.0 50148 3852 ? Ssl 03:27 0:00 redis-server 0.0.0.0:6379
root 6457 0.0 0.0 13136 1084 pts/0 S+ 03:28 0:00 grep --color=auto redis
redis-cli -h 127.0.0.1 -p 6378 shutdown # 关闭 master
redis-cli -h 127.0.0.1 -p 6379 shutdown # 关闭 slave
【持久化】
默认已启用了 rdb 方式的持久化。(/data/redis/6378-master/conf/redis.conf)
# save <seconds> <changes>
# save ""
save 900 1 # 若 900 秒内有 1 个更改则将内存中的数据快照写入磁盘
save 300 10 # 若 300 秒内有 10 个更改则将内存中的数据快照写入磁盘
save 60 10000 # 若 60 秒内有 10000 个更改则将内存中的数据快照写入磁盘
walker 猜想实现方式应该是时间从小到大:设置定时器监测,60 秒内有 10000 个更改则保存,300 秒内有 10 个更改则保存,900 秒内有 1 个更改则保存。
【最大内存】
# maxmemory <bytes>
# 1G
maxmemory 1073741824
【查看版本】
# redis-server -v
Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=1bc80a08306a3efd
【查看配置】
$redis-cli -h 127.0.0.1 -p 6378 -a "mypass"
127.0.0.1:6378> info
【配置开机启动】
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
创建文件:/etc/rc.local
在 rc.local 里面添加如下内容
#!/bin/bash
redis-server /data/redis/6378-master/conf/redis.conf
redis-server /data/redis/6379-slave/conf/redis.conf
/home/walker/Python3Project/proxy_gather/start.sh
exit 0
sudo chmod 754 /etc/rc.local
sudo systemctl enable rc-local
# 等价于
ln -s /etc/systemd/system/rc-local.service /etc/systemd/system/multi-user.target.wants/
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
*** walker ***