redis单机主从搭建

时间:2022-11-27 23:38:30
tar zxvf redis-2.8.13.tar.gz
cd redis-2.8.13
make
1、安装主库
mkdir /opt/redis/sbin -p
mkdir    /opt/redis/bin -p
mkdir /data/redis/redis6379/
cp redis.conf /data/redis/redis6379/6379.conf【配置文件下拉到最底部】
cd src
cp redis-se* /opt/redis/sbin/
cp redis-check-dump /opt/redis/bin/
cp redis-check-aof /opt/redis/bin/
cp redis-cli /opt/redis/bin/
启动:
/opt/redis/sbin/redis-server /data/redis/redis6379/6379.conf
关闭:
/opt/redis/bin/redis-cli shutdown
 
系统初始化:
sed -i -r '/vm.overcommit_memory*/d' /etc/sysctl.conf
sed -i -r '/vm.swappiness*/d' /etc/sysctl.conf
echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
echo "vm.swappiness    = 1" >>/etc/sysctl.conf
/sbin/sysctl -q    -p /etc/sysctl.conf
sed -i -r '/redis soft nofile.*/d' /etc/security/limits.conf
sed -i -r '/redis hard nofile.*/d' /etc/security/limits.conf
echo "redis    soft nofile 288000" >> /etc/security/limits.conf
echo "redis hard nofile 288000" >> /etc/security/limits.conf
sed -i -r '/redis soft nproc.*/d'  /etc/security/limits.conf
sed -i -r '/redis hard nproc.*/d'  /etc/security/limits.conf
echo "redis soft nproc unlimited">>/etc/security/limits.conf
echo "redis hard nproc unlimited">>/etc/security/limits.conf
 
2、安装从库
mkdir -p /data/redis/redis6380/
cd /data/redis/redis6380/
cp ../redis6379/6379.conf 6380.conf
sed -i 's/6379/6380/g' 6380.conf     #使用替换配置文件中的6379为6380
echo "slaveof 192.168.98.199 6379" >> 6380.conf     #在6380.conf中添加slaveof ip port
 
3、启动主从
先启主库:
/opt/redis/sbin/redis-server /data/redis/redis6379/6379.conf
启动从库:
/opt/redis/sbin/redis-server /data/redis/redis6380/6380.conf
 
4、从库日志
[root@MyCentOS redis6380]# less /data/redis/6380.log
[5545] 13 Sep 22:30:18.164 # Server started, Redis version 2.8.13
[5545] 13 Sep 22:30:18.164 * The server is now ready to accept connections on port 6380
[5545] 13 Sep 22:30:18.164 * Connecting to MASTER 192.168.98.199:6379
[5545] 13 Sep 22:30:18.171 * MASTER <-> SLAVE sync started
[5545] 13 Sep 22:30:18.176 * Non blocking connect for SYNC fired the event.
[5545] 13 Sep 22:30:18.180 * Master replied to PING, replication can continue...
[5545] 13 Sep 22:30:18.180 * Partial resynchronization not possible (no cached master)
[5545] 13 Sep 22:30:18.180 * Full resync from master: 668093d0cb4a27a7d025f4d36feff13f622368d3:1
[5545] 13 Sep 22:30:18.207 * MASTER <-> SLAVE sync: receiving 18 bytes from master
[5545] 13 Sep 22:30:18.207 * MASTER <-> SLAVE sync: Flushing old data
[5545] 13 Sep 22:30:18.207 * MASTER <-> SLAVE sync: Loading DB in memory
[5545] 13 Sep 22:30:18.207 * MASTER <-> SLAVE sync: Finished with success
[5545] 13 Sep 22:30:18.208 * Background append only file rewriting started by pid 5549
[5549] 13 Sep 22:30:18.213 * SYNC append only file rewrite performed
[5549] 13 Sep 22:30:18.213 * AOF rewrite: 6 MB of memory used by copy-on-write
[5545] 13 Sep 22:30:18.277 * Background AOF rewrite terminated with success
[5545] 13 Sep 22:30:18.277 * Parent diff successfully flushed to the rewritten AOF (0 bytes)
[5545] 13 Sep 22:30:18.278 * Background AOF rewrite finished successfully
 
5、测试主从
主库:
[root@MyCentOS redis6380]# /opt/redis/bin/redis-cli -p 6379
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set fuzhi 'fuzhiceshi'
OK
127.0.0.1:6379> get fuzhi
"fuzhiceshi"
127.0.0.1:6379> info replication     【 利用 info Replication 查看复制关系】
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.98.199,port=6380,state=online,offset=1297,lag=1
master_repl_offset:1297
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1296
从库:
[root@MyCentOS ~]# /opt/redis/bin/redis-cli -p 6380
127.0.0.1:6380> keys *
1) "fuzhi"
127.0.0.1:6380> get fuzhi
"fuzhiceshi"
127.0.0.1:6380> set ceshiku 'shifouxieru'
(error) READONLY You can't write against a read only slave.  【测试库不能写入数据】
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:192.168.98.199
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1283
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380>
 
redis可执行文件:
redis-server : Redis服务器的daemon启动程序
redis-cli : Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark : Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat : Redis状态检测工具,可以检测Redis当前状态参数及延迟状况
 

6379.conf:
daemonize yes
pidfile 6379.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/data/redis/6379.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename 6379.rdb
dir /data/redis/redis6379
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 10000
maxmemory 100M
maxmemory-policy volatile-lru
appendonly yes
appendfilename "6379.aof"
appendfsync    everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes