本案例要求先快速搭建好两台Redis服务器,实现两台服务器之间自动数据同步,具体要求如下:
主服务器IP为192.168.4.10从服务器IP为192.168.4.20主服务器认证密码为password测试主从数据是否正常通过 方案通过修改Redis配置文件,实现两台服务器之间的自动主从同步功能,方案拓扑如图-1所示。
图-1
步骤实现此案例需要按照如下步骤进行。
步骤一:配置主从服务器设置
1)主服务器安装Redis
[root@svr10 ~]# tar -xzf redis-3.0.6.tar.gz
[root@svr10 ~]# cd redis-3.0.6
[root@svr10 ~]# make
[root@svr10 ~]# make install
[root@svr10 ~]# cd utils/
[root@svr10 ~]#./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]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
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!
2)从服务器安装Redis
[root@svr20 ~]# tar -xzf redis-3.0.6.tar.gz
[root@svr20 ~]# cd redis-3.0.6
[root@svr20 ~]# make
[root@svr20 ~]# make install
[root@svr20 ~]# cd utils/
[root@svr20 ~]#./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]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
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!
步骤二:配置主从服务器设置
1)修改主服务器/etc/redis/6379.conf配置文件
[root@svr10 ~]# vim /etc/redis/6379.conf
requirepass redis123 //设置服务器密码
[root@svr10 ~]# /etc/init.d/redis_6379 restart //重启服务
2)修改从服务器/etc/redis/6379.conf配置文件
[root@svr20 ~]# vim /etc/redis/6379.conf
slaveof 192.1684.10 6379
masterauth redis123
[root@svr20 ~]# /etc/init.d/redis_6379 restart
步骤三:客户端验证效果
[root@client ~]# redis-cli –h 192.168.4.10 –a redis123 //登录主服务器设置数据
192.168.4.10:6379> set test 123456
OK
[root@client ~]# redis-cli –h 192.168.4.20 //登录主服务器查看数据同步效果
192.168.4.20:6379> set test
“123456”