【目标】 配置 Redis 单机主从,6378 端口连接 master,6379 端口连接 slave,master 可读写,slave 只读。【安装及测试】安装sudo apt update sudo
【目标】
配置 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 管理 redis
# 查看状态
$ 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/
编辑 /data/redis/6378-master/conf/redis.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/
编辑 /data/redis/6379-slave/conf/redis.conf 文件,修改或添加如下内容
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
关闭并禁用系统 redis
sudo systemctl stop redis
sudo systemctl disable redis
启动 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
【配置开机启动】
参考:ubuntu-18.04 设置开机启动脚本
创建文件:/etc/systemd/system/rc-local.service
在 rc-local.service 里面添加如下内容
[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
给 rc.local 添加可执行权限
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 ***
--结束END--
本文标题: Ubuntu Server 18.04 配置 Redis 单机主从
本文链接: https://www.lsjlt.com/news/42694.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0