3.4.2、分别修改redis主从服务器的配置文件
[root@mysqldb1 redis-3.0.5]# cp /root/redis-3.0.5/redis.conf master_6379/conf/master.conf
[root@mysqldb1 redis-3.0.5]# cp /root/redis-3.0.5/redis.conf slave_6380/conf/slave.conf
[root@mysqldb1 redis]# vim master_6379/conf/master.conf#修改主服务器的配置文件
47 pidfile /redis/master_6379/redis_master.pid
51 port
110 logfile "/redis/master_6379/log/master.log"
193 dir /redis/master_6379/data/
515 appendfilename "appendonly.aof"
[root@mysqldb1 redis]# vim /redis/slave_6380/conf/slave.conf #修改从服务器的配置文件
46 pidfile /redis/slave_6380/redis_slave.pid
50 port 6380
109 logfile "/redis/slave_6380/log/slave.log"
192 dir /redis/slave_6380/data/
212 slaveof 127.0.0.1 6379
514 appendfilename "appendonly.aof"
3.4.3、分别启动主、从服务器
[root@mysqldb1 master_6379]# redis-server /redis/master_6379/conf/master.conf
[root@mysqldb1 slave_6380]# redis-server /redis/slave_6380/conf/slave.conf
3.4.4、分别查看主、从服务器的日志文件
[root@mysqldb1 redis]# cat /redis/master_6379/log/master.log #主服务器的日志文件
... ...
14363:M 02 Nov 12:42:57.805 * DB loaded from disk: 0.000 seconds
14363:M 02 Nov 12:42:57.805 * The server is now ready to accept connections on port 6379#主服务器的启动端口
14363:M 02 Nov 12:43:36.361 * Slave 127.0.0.1:6380 asks for synchronization#要求同步的从服务器信息
14363:M 02 Nov 12:43:36.361 * Full resync requested by slave 127.0.0.1:6380
14363:M 02 Nov 12:43:36.361 * Starting BGSAVE for SYNC with target: disk
14363:M 02 Nov 12:43:36.496 * Background saving started by pid 14371
14371:C 02 Nov 12:43:36.528 * DB saved on disk
14371:C 02 Nov 12:43:36.528 * RDB: 4 MB of memory used by copy-on-write
14363:M 02 Nov 12:43:36.597 * Background saving terminated with success
14363:M 02 Nov 12:43:36.597 * Synchronization with slave 127.0.0.1:6380 succeeded
[root@mysqldb1 redis]# cat /redis/slave_6380/log/slave.log #从服务器的日志文件
... ...
14368:S 02 Nov 12:43:35.424 * DB loaded from disk: 0.000 seconds
14368:S 02 Nov 12:43:35.424 * The server is now ready to accept connections on port 6380#从服务器的启动端口
14368:S 02 Nov 12:43:36.360 * Connecting to MASTER 127.0.0.1:6379#正在连接主服务器
14368:S 02 Nov 12:43:36.361 * MASTER <-> SLAVE sync started#主从同步已经开始
14368:S 02 Nov 12:43:36.361 * Non blocking connect for SYNC fired the event.
14368:S 02 Nov 12:43:36.361 * Master replied to PING, replication can continue...
14368:S 02 Nov 12:43:36.361 * Partial resynchronization not possible (no cached master)
14368:S 02 Nov 12:43:36.497 * Full resync from master: dd7a9d178eb3434494fecd4c97cc05e8d6bc1a69:1
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: receiving 55 bytes from master
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: Flushing old data
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: Loading DB in memory
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: Finished with success
3.4.5、查看redis主、从数据文件md5指纹信息
[root@mysqldb1 redis]# find /redis/ -name *.rdb | xargs md5sum
81646a7364950775039f694b1ddd6c8a /redis/slave_6380/data/slave_dump.rdb
81646a7364950775039f694b1ddd6c8a /redis/master_6379/data/master_dump.rdb
通过指纹信息可以得到redis主、从服务器的数据是一致的
3.4.6、向主服务器添加数据
[root@mysqldb1 ~]# redis-cli -p 6379
127.0.0.1:6379> set key1 hello
OK
127.0.0.1:6379> set key2 liangge
OK
127.0.0.1:6379> set key3 OK
OK
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
3) "key3"
127.0.0.1:6379> quit
3.4.7、在从服务器上查看数据信息是否已同步
[root@mysqldb1 ~]# redis-cli -p 6380
127.0.0.1:6380> get key1
"hello"
127.0.0.1:6380> get key2
"liangge"
127.0.0.1:6380> get key3
"OK"
127.0.0.1:6380> keys *
1) "key1"
2) "key2"
3) "key3"
127.0.0.1:6380> quit
3.4.8、再次查看主、从服务器的md5数据指纹
[root@mysqldb1 ~]# find /redis/ -name *.rdb |xargs md5sum
81646a7364950775039f694b1ddd6c8a /redis/slave_6380/data/slave_dump.rdb
81646a7364950775039f694b1ddd6c8a /redis/master_6379/data/master_dump.rdb
3.4.9、查看redis主从服务器工作目录
[root@mysqldb1 redis]# tree /redis
/redis
├── master_6379
│ ├── conf
│ │ └── master.conf
│ ├── data
│ │ └── master_dump.rdb
│ └── log
│ └── master.log
└── slave_6380
├── conf
│ └── slave.conf
├── data
│ └── slave_dump.rdb
└── log
└── slave.log
8 directories, 6 files
4、PHP中安装redis插件
4.1、下载php_redis扩展模块
phpredis下载地址: https://codeload.github.com/owlient/phpredis/zip/master