Redis主從同步

1.環境說明
192.168.60.204 redis master
192.168.60.205 redis slaveredis

2.安裝編譯環境
yum -y install gcc libcshell

4.3.下載redis
選擇版本:vim

Redis借鑑了Linux操做系統對於版本號的命名規則:
版本號第二位若是是奇數,則爲非穩定版本(例如2.七、2.九、3.1),若是是偶數,則爲穩定版本(例如2.六、2.八、3.0、3.2),
當前奇數版本就是下一個穩定版本的開發版本,例如2.9版本是3.0版本的開發版本,因此咱們在生產環境一般選取偶數版的Redis。
進入目錄/usr/local下載Redis:
cd /usr/local
wget http://download.redis.io/releases/redis-5.0.0.tar.gzide

4.安裝redis
192.168.60.2014和192.168.60.205都要安裝ui

tar -xf redis-5.0.0.tar.gz
make && make installthis

5.將redis設置成開機啓動
cd /usr/local/redis-5.0.0/utils/
./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /etc/redis/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis.log
Please select the data directory for this instance [/var/lib/redis/6379] /var/lib/redis/db
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/redis.conf #redis配置文件
Log file : /var/log/redis.log #redis日誌
Data dir : /var/lib/redis/db #redis DB目錄
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
啓動redis
redis-server /etc/redis/redis.conf
[root@master ~]# ss -ntlp |grep redis
LISTEN 0 128 :6379 :* users:(("redis-server",pid=17696,fd=6))操作系統

6.配置redis主從
主庫 192.168.60.204日誌

vim /etc/redis/redis.conf
修改如下幾項code

bind 0.0.0.0 #任何主機均可以鏈接
requirepass 123456 #設置redis登陸密碼
從庫 192.168.60.205server

bind 0.0.0.0 #任何主機均可以鏈接
slaveof 192.168.60.204 6379 #主庫IP 端口
requirepass 123456 #設置redis登陸密碼
masterauth 123456 #主庫的登陸密碼
注意:主從配置完兩個redis都要重啓生效

例如192.168.60.204
[root@master ~]# ps -ef |grep redis
root 983 1 0 16:49 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 1728 1542 0 16:54 pts/0 00:00:00 grep --color=auto redis
[root@master ~]# kill 983
[root@master ~]# ps -ef |grep redis
root 1742 1542 0 16:55 pts/0 00:00:00 grep --color=auto redis
[root@master ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@master ~]# ps -ef |grep redis
root 1751 1 0 16:55 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 1759 1542 0 16:55 pts/0 00:00:00 grep --color=auto redis
[root@master ~]#

7.驗證主從配置

登陸從庫192.168.60.205
[root@slave ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> 
127.0.0.1:6379> info Replication
# Replication
role:slave  角色從庫
master_host:192.168.60.204  主庫IP
master_port:6379            主庫端口
master_link_status:up       主庫狀態
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:7952
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:57de84c0a6d43857a92eb1a2b9b60cc054522717
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:7952
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:7952
127.0.0.1:6379>

驗證主從是否同步數據
[root@master ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> set bool AAA
OK
127.0.0.1:6379> get bool
"AAA"
127.0.0.1:6379> 
從庫查看下是否有值
[root@slave ~]# redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> get bool
"AAA"
127.0.0.1:6379>
相關文章
相關標籤/搜索