http://www.linuxidc.com/Linux/2013-11/92287p3.htm
1、安装redis
--------------------------------------------------------------------------------
首页地址:http://redis.io/
下载地址:http://download.redis.io/ 下载最新的源码包(当前最新稳定版2.6.16)
#wget http://download.redis.io/releases/redis-2.6.16.tar.gz
#tar xzf redis-2.6.16.tar.gz
#cd redis-2.6.16
#make
#make install
#cp redis.conf /etc/
--------------------------------------------------------------------------------
2、运行
--------------------------------------------------------------------------------
#redis-server /etc/redis.conf & (加载配置文件并后台运行)
#redis-cli (命令行工具)
--------------------------------------------------------------------------------
3、测试
--------------------------------------------------------------------------------
# redis-benchmark --help 获取帮助信息,其中包含压力测试命令样例
Examples:
Run the benchmark with the default configuration against 127.0.0.1:6379:
$ redis-benchmark
Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:
$ redis-benchmark -t set -n 1000000 -r 100000000
[root@php1 ~]# redis-benchmark -t set -c 20 -n 1000000 -r 100000000
====== SET ======
1000000 requests completed in 8.92 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
112095.06 requests per second
4、主从配置及测试
主php1 172.17.16.7 6379
从mysql1 172.17.16.21 6379
从mysql3 172.17.16.22 6379
#从服务器上
vim /etc/redis.conf
#做如下修改,其他配置保持默认即可
slaveof 172.17.16.7 6379
masterauth redispass
requirepass redispass
#主服务器上
vim /etc/redis.conf
#做如下修改,其他配置保持默认即可
masterauth redispass
requirepass redispass
#启动主、从
#主16.7
#redis-server /etc/redis.conf &
#从16.21、16.22
#redis-server /etc/redis.conf &
#主服务器显示
[13997] 03 Nov 09:57:21.045 * Slave ask for synchronization
[13997] 03 Nov 09:57:21.045 * Starting BGSAVE for SYNC
[13997] 03 Nov 09:57:21.046 * Background saving started by pid 14002
[14002] 03 Nov 09:57:21.058 * DB saved on disk
[14002] 03 Nov 09:57:21.059 * RDB: 0 MB of memory used by copy-on-write
[13997] 03 Nov 09:57:21.070 * Background saving terminated with success
[13997] 03 Nov 09:57:21.070 * Synchronization with slave succeeded
#从服务器显示
[3496] 03 Nov 09:56:41.953 * Connecting to MASTER...
[3496] 03 Nov 09:56:41.953 * MASTER <-> SLAVE sync started
[3496] 03 Nov 09:56:41.954 * Non blocking connect for SYNC fired the event.
[3496] 03 Nov 09:56:41.954 * Master replied to PING, replication can continue...
[3496] 03 Nov 09:56:42.055 * MASTER <-> SLAVE sync: receiving 18 bytes from master
[3496] 03 Nov 09:56:42.055 * MASTER <-> SLAVE sync: Loading DB in memory
[3496] 03 Nov 09:56:42.055 * MASTER <-> SLAVE sync: Finished with success
[3499] 03 Nov 09:56:42.056 * SYNC append only file rewrite performed
[3499] 03 Nov 09:56:42.057 * AOF rewrite: 0 MB of memory used by copy-on-write
[3496] 03 Nov 09:56:42.057 * Background append only file rewriting started by pid 3499
[3496] 03 Nov 09:56:42.154 * Background AOF rewrite terminated with success
[3496] 03 Nov 09:56:42.154 * Parent diff successfully flushed to the rewritten AOF (0 bytes)
[3496] 03 Nov 09:56:42.154 * Background AOF rewrite finished successfully
#主服务器查看主从信息
#[root@php1 ~]# redis-cli -h 127.0.0.1 -a redispass info replication
# Replication
role:master
connected_slaves:2
slave0:172.17.16.21,6379,online
slave1:172.17.16.22,6379,online
#从服务器查看主从信息
#[root@mysql1 ~]# redis-cli -h 127.0.0.1 -a redispass info replication
# Replication
role:slave
master_host:172.17.16.7
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_priority:100
slave_read_only:1
connected_slaves:0
#写同步测试
#主服务器
[root@php1 ~]# redis-cli -h 127.0.0.1 -a redispass
redis 127.0.0.1:6379> set a 123
OK
redis 127.0.0.1:6379> get a
"123"
#从服务器
[root@mysql1 ~]# redis-cli -h 127.0.0.1 -a redispass
redis 127.0.0.1:6379> get a
"123"
[root@mysql3 ~]# redis-cli -h 127.0.0.1 -a redispass
redis 127.0.0.1:6379> get a
"123"
redis 127.0.0.1:6379> set b 234
(error) READONLY You can't write against a read only slave. (开起了只读模式,所以从将不能写入数据,可以保证数据只从主服务器同步至从服务器)
#故障测试
#从服务器宕机
将自动从主服务器的主从关系中解除
#主服务器宕机,从服务器显示,并且不能自动切换
[15932] 03 Nov 09:46:25.465 * Connecting to MASTER...
[15932] 03 Nov 09:46:25.465 * MASTER <-> SLAVE sync started
[15932] 03 Nov 09:46:25.466 # Error condition on socket for SYNC: Connection refused
5、Redis sentinel配置
--------------------------------------------------------------------------------
主页地址:http://redis.io/topics/sentinel
sentinel主要功能就是为Redis M-S(master,slaves)集群提供了
1)master存活检测
2)集群中M-S服务监控
3) 自动故障转移,M-S角色转换等能力,
从一个方面说是提高了redis集群的可用性.
#添加并编辑配置文件/etc/sentinel.conf,新增内容如下,也可在安装文件中负责sentinel配置文件并作适当修改:
[root@php1 ~]# cat /etc/sentinel.conf
port 26379
sentinel monitor mymaster 172.17.16.7 6379 2
sentinel auth-pass mymaster redispass
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
[root@mysql1 ~]# cat /etc/sentinel.conf
port 26479
sentinel monitor mymaster 172.17.16.7 6379 2
sentinel auth-pass mymaster redispass
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel parallel-syncs mymaster 1
[root@mysql3 ~]# cat /etc/sentinel.conf
port 26579
sentinel monitor mymaster 172.17.16.7 6379 2
sentinel auth-pass mymaster redispass
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel parallel-syncs mymaster 1
#在三个服务器中以sentinel模式启动redis-server
[root@php1 ~]# redis-server /etc/sentinel.conf --sentinel
[root@mysql1 ~]# redis-server /etc/sentinel.conf --sentinel
[root@mysql3 ~]# redis-server /etc/sentinel.conf --sentinel
#最后三台服务器sentinel输出如下:(注意每个机器输出对应的都是另外两台机器的IP)
#php1
[14047] 03 Nov 10:18:55.109 * +slave slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[14047] 03 Nov 10:18:55.109 * +slave slave 172.17.16.21:6379 172.17.16.21 6379 @ mymaster 172.17.16.7 6379
[14047] 03 Nov 10:19:42.272 * +sentinel sentinel 172.17.16.21:26479 172.17.16.21 26479 @ mymaster 172.17.16.7 6379
[14047] 03 Nov 10:20:46.338 * +sentinel sentinel 172.17.16.22:26579 172.17.16.22 26579 @ mymaster 172.17.16.7 6379
#mysql1
[3512] 03 Nov 10:23:36.473 * +slave slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:23:36.473 * +slave slave 172.17.16.21:6379 172.17.16.21 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:23:40.446 * +sentinel sentinel 172.17.16.7:26379 172.17.16.7 26379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:24:45.567 * +sentinel sentinel 172.17.16.22:26579 172.17.16.22 26579 @ mymaster 172.17.16.7 6379
#mysql3
[21428] 03 Nov 10:24:40.561 * +slave slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[21428] 03 Nov 10:24:40.561 * +slave slave 172.17.16.21:6379 172.17.16.21 6379 @ mymaster 172.17.16.7 6379
[21428] 03 Nov 10:24:41.367 * +sentinel sentinel 172.17.16.7:26379 172.17.16.7 26379 @ mymaster 172.17.16.7 6379
[21428] 03 Nov 10:24:42.280 * +sentinel sentinel 172.17.16.21:26479 172.17.16.21 26479 @ mymaster 172.17.16.7 6379
#关掉mysql3上的redis-server服务
#每个sentinel将输出如下信息,表示认为slave 22已经宕机,剔除
[3512] 03 Nov 10:30:08.359 # +sdown slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
#重新启动mysql3上的redis-server服务
#每个sentinel将输出如下信息,表示认为slave 22已经重新启动,新增到集群中
[3512] 03 Nov 10:31:28.901 * +reboot slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:31:29.115 # -sdown slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
#关掉主php1的redis-server服务
#将重新选举主服务器并重新配置连接到新的主服务器:
#php1上输出:
[14047] 03 Nov 10:29:49.888 # +sdown master mymaster 172.17.16.7 6379
[14047] 03 Nov 10:29:51.188 # +odown master mymaster 172.17.16.7 6379 #quorum 3/2
[14047] 03 Nov 10:29:59.189 # +failover-detected master mymaster 172.17.16.7 6379
[14047] 03 Nov 10:29:59.689 * +slave-reconf-inprog slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[14047] 03 Nov 10:30:00.789 * +slave-reconf-done slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[14047] 03 Nov 10:30:00.888 # +failover-end master mymaster 172.17.16.7 6379
[14047] 03 Nov 10:30:00.888 # +switch-master mymaster 172.17.16.7 6379 172.17.16.21 6379
[14047] 03 Nov 10:30:00.990 * +slave slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.21 6379
[14047] 03 Nov 10:30:01.058 * +sentinel sentinel 172.17.16.21:26479 172.17.16.21 26479 @ mymaster 172.17.16.21 6379
[14047] 03 Nov 10:30:05.956 * +sentinel sentinel 172.17.16.22:26579 172.17.16.22 26579 @ mymaster 172.17.16.21 6379
[14047] 03 Nov 10:30:30.988 # +sdown slave 172.17.16.7:6379 172.17.16.7 6379 @ mymaster 172.17.16.21 6379
#mysql1上输出:
[3512] 03 Nov 10:33:49.516 # +sdown master mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:49.716 # +odown master mymaster 172.17.16.7 6379 #quorum 3/2
[3512] 03 Nov 10:33:49.716 # +failover-triggered master mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:49.716 # +failover-state-wait-start master mymaster 172.17.16.7 6379 #starting in 8139 milliseconds
[3512] 03 Nov 10:33:57.893 # +failover-state-select-slave master mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:57.997 # +selected-slave slave 172.17.16.21:6379 172.17.16.21 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:57.997 * +failover-state-send-slaveof-noone slave 172.17.16.21:6379 172.17.16.21 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:58.101 * +failover-state-wait-promotion slave 172.17.16.21:6379 172.17.16.21 6379 @ mymaster 172.17.16.7 6379
[3496] 03 Nov 10:33:58.101 * MASTER MODE enabled (user request)
[3512] 03 Nov 10:33:58.741 # +promoted-slave slave 172.17.16.21:6379 172.17.16.21 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:58.741 # +failover-state-reconf-slaves master mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:58.849 * +slave-reconf-sent slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:33:58.957 * +slave-reconf-inprog slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[3496] 03 Nov 10:33:59.080 * Slave ask for synchronization
[3496] 03 Nov 10:33:59.080 * Starting BGSAVE for SYNC
[3496] 03 Nov 10:33:59.081 * Background saving started by pid 3515
[3515] 03 Nov 10:33:59.086 * DB saved on disk
[3515] 03 Nov 10:33:59.087 * RDB: 0 MB of memory used by copy-on-write
[3496] 03 Nov 10:33:59.188 * Background saving terminated with success
[3496] 03 Nov 10:33:59.189 * Synchronization with slave succeeded
[3512] 03 Nov 10:33:59.994 * +slave-reconf-done slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[3512] 03 Nov 10:34:00.093 # +failover-end master mymaster 172.17.16.7 6379
[3512] 03 Nov 10:34:00.093 # +switch-master mymaster 172.17.16.7 6379 172.17.16.21 6379
[3512] 03 Nov 10:34:00.194 * +slave slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.21 6379
[3512] 03 Nov 10:34:00.325 * +sentinel sentinel 172.17.16.7:26379 172.17.16.7 26379 @ mymaster 172.17.16.21 6379
[3512] 03 Nov 10:34:05.192 * +sentinel sentinel 172.17.16.22:26579 172.17.16.22 26579 @ mymaster 172.17.16.21 6379
[3512] 03 Nov 10:34:30.134 # +sdown slave 172.17.16.7:6379 172.17.16.7 6379 @ mymaster 172.17.16.21 6379
#mysql3上输出:
[21428] 03 Nov 10:33:49.343 # +sdown master mymaster 172.17.16.7 6379
[21428] 03 Nov 10:33:49.543 # +odown master mymaster 172.17.16.7 6379 #quorum 2/2
[21428] 03 Nov 10:33:58.896 # +failover-detected master mymaster 172.17.16.7 6379
[21428] 03 Nov 10:33:59.836 * +slave-reconf-inprog slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[21428] 03 Nov 10:33:59.836 * +slave-reconf-done slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.7 6379
[21428] 03 Nov 10:33:59.940 # +failover-end master mymaster 172.17.16.7 6379
[21428] 03 Nov 10:33:59.940 # +switch-master mymaster 172.17.16.7 6379 172.17.16.21 6379
[21428] 03 Nov 10:34:00.045 * +slave slave 172.17.16.22:6379 172.17.16.22 6379 @ mymaster 172.17.16.21 6379
[21428] 03 Nov 10:34:00.293 * +sentinel sentinel 172.17.16.21:26479 172.17.16.21 26479 @ mymaster 172.17.16.21 6379
[21428] 03 Nov 10:34:00.325 * +sentinel sentinel 172.17.16.7:26379 172.17.16.7 26379 @ mymaster 172.17.16.21 6379
[21428] 03 Nov 10:34:29.989 # +sdown slave 172.17.16.7:6379 172.17.16.7 6379 @ mymaster 172.17.16.21 6379
#查看主从关系:(mysql1为主,mysql3为从,如果php1重新启动,也将为从服务器加入到新的集群)
[root@mysql1 ~]# redis-cli -h 127.0.0.1 -p 6379 -a redispass info replication
# Replication
role:master
connected_slaves:1
slave0:172.17.16.22,6379,online
[root@mysql3 ~]# redis-cli -h 127.0.0.1 -p 6379 -a redispass info replication
# Replication
role:slave
master_host:172.17.16.21
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_priority:100
slave_read_only:1
connected_slaves:0
#重新启动php1,检查mysql1的主从关系
[root@mysql1 ~]# redis-cli -h 127.0.0.1 -p 6379 -a redispass info replication
# Replication
role:master
connected_slaves:2
slave0:172.17.16.22,6379,online
slave1:172.17.16.7,6379,online
切换成功。
注意:
如果原来的 redis master停止后,并已经选取出新的master后,原来停止的master启动前需要指定
slaveof {目前master_ip} 6379