Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6664159
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: 数据库开发技术

2015-03-12 15:59:28

环境:

OS:Red Hat Linux As 5

DB:redis-4.0.6

-------------------------------------主库安装-------------------------------

1.安装
[root@pxc01 soft]#tar –zxvf redis-4.0.6.tar.gz
[root@pxc01 soft]# cp -R ./redis-4.0.6 /opt/
[root@pxc01 opt]# cd redis-4.0.6
[root@pxc01 redis-4.0.6]# make
[root@pxc01 redis-4.0.6]# make test

继续安装
[root@pxc01 redis-4.0.6]# cd src
[root@pxc01 src]# make install

2.创建相应目录
[root@pxc01 opt]# cd redis-4.0.6/
[root@pxc01 redis-4.0.6]# mkdir conf  ##配置文件目录
[root@pxc01 redis-4.0.6]# mkdir log   ##日志文件目录
[root@pxc01 redis-4.0.6]# mkdir run   ##运行的pid文件目录
[root@pxc01 redis-4.0.6]# mkdir rdb   ##快照和AOF文件目录

3.主库配置文件(8379.conf)

  1. daemonize yes
  2. pidfile /opt/single_redis-4.0.6/run/redis-8379.pid
  3. port 8379
  4. tcp-backlog 511
  5. timeout 0
  6. tcp-keepalive 0
  7. loglevel notice
  8. logfile "/opt/single_redis-4.0.6/log/redis-8379.log"
  9. databases 16
  10. save 900 1
  11. save 300 10
  12. save 60 10000
  13. stop-writes-on-bgsave-error yes
  14. rdbcompression yes
  15. rdbchecksum yes
  16. dbfilename dump-8379.rdb
  17. dir /opt/single_redis-4.0.6/rdb
  18. slave-serve-stale-data yes
  19. slave-read-only yes
  20. repl-diskless-sync no
  21. repl-diskless-sync-delay 5
  22. repl-disable-tcp-nodelay no
  23. slave-priority 100
  24. appendonly yes
  25. appendfilename "redis-8379.aof"
  26. appendfsync everysec
  27. no-appendfsync-on-rewrite no
  28. auto-aof-rewrite-percentage 100
  29. auto-aof-rewrite-min-size 64mb
  30. aof-load-truncated yes
  31. lua-time-limit 5000
  32. slowlog-log-slower-than 10000
  33. slowlog-max-len 128
  34. latency-monitor-threshold 0
  35. notify-keyspace-events ""
  36. hash-max-ziplist-entries 512
  37. hash-max-ziplist-value 64
  38. list-max-ziplist-entries 512
  39. list-max-ziplist-value 64
  40. set-max-intset-entries 512
  41. zset-max-ziplist-entries 128
  42. zset-max-ziplist-value 64
  43. hll-sparse-max-bytes 3000
  44. activerehashing yes
  45. client-output-buffer-limit normal 0 0 0
  46. client-output-buffer-limit slave 256mb 64mb 60
  47. client-output-buffer-limit pubsub 32mb 8mb 60
  48. hz 10
  49. aof-rewrite-incremental-fsync yes
  50. # Generated by CONFIG REWRITE
  51. #masterauth "richinfo123"
  52. #requirepass "richinfo123"
  53. protected-mode no
4.启动主库
/opt/single_redis-4.0.6/src/redis-server /opt/single_redis-4.0.6/conf/8379.conf



-------------------------------------从库安装-------------------------------

1.安装
跟主库安装一样

2.从库配置文件
从库的配置文件就是在主库配置文件的基础上加上如下2行
slaveof 192.168.56.91 8379 ##表示从那个主库同步
slave-read-only yes              ##从库设置为只读

  1. daemonize yes
  2. pidfile /opt/single_redis_slave/run/redis-8379.pid
  3. port 8379
  4. tcp-backlog 511
  5. timeout 0
  6. tcp-keepalive 0
  7. loglevel notice
  8. logfile "/opt/single_redis_slave/log/redis-8379.log"
  9. databases 16
  10. save 900 1
  11. save 300 10
  12. save 60 10000
  13. stop-writes-on-bgsave-error yes
  14. rdbcompression yes
  15. rdbchecksum yes
  16. dbfilename dump-8379.rdb
  17. dir /opt/single_redis_slave/rdb
  18. slave-serve-stale-data yes
  19. slave-read-only yes
  20. repl-diskless-sync no
  21. repl-diskless-sync-delay 5
  22. repl-disable-tcp-nodelay no
  23. slave-priority 100
  24. appendonly yes
  25. appendfilename "redis-8379.aof"
  26. appendfsync everysec
  27. no-appendfsync-on-rewrite no
  28. auto-aof-rewrite-percentage 100
  29. auto-aof-rewrite-min-size 64mb
  30. aof-load-truncated yes
  31. lua-time-limit 5000
  32. slowlog-log-slower-than 10000
  33. slowlog-max-len 128
  34. latency-monitor-threshold 0
  35. notify-keyspace-events ""
  36. hash-max-ziplist-entries 512
  37. hash-max-ziplist-value 64
  38. list-max-ziplist-entries 512
  39. list-max-ziplist-value 64
  40. set-max-intset-entries 512
  41. zset-max-ziplist-entries 128
  42. zset-max-ziplist-value 64
  43. hll-sparse-max-bytes 3000
  44. activerehashing yes
  45. client-output-buffer-limit normal 0 0 0
  46. client-output-buffer-limit slave 256mb 64mb 60
  47. client-output-buffer-limit pubsub 32mb 8mb 60
  48. hz 10
  49. aof-rewrite-incremental-fsync yes
  50. # Generated by CONFIG REWRITE
  51. #masterauth "richinfo123"
  52. #requirepass "richinfo123"
  53. protected-mode no
  54. slaveof 192.168.56.91 8379


3.启动从库
/opt/single_redis_slave/src/redis-server /opt/single_redis_slave/conf/8379.conf


4.查看从库的同步日志

21636:S 23 Jan 04:40:33.028 * Ready to accept connections
21636:S 23 Jan 04:40:33.028 * Connecting to MASTER 192.168.56.91:8379
21636:S 23 Jan 04:40:33.028 * MASTER <-> SLAVE sync started
21636:S 23 Jan 04:40:33.030 * Non blocking connect for SYNC fired the event.
21636:S 23 Jan 04:40:33.031 * Master replied to PING, replication can continue...
21636:S 23 Jan 04:40:33.032 * Partial resynchronization not possible (no cached master)
21636:S 23 Jan 04:40:33.038 * Full resync from master: 4bfec2e8871336c8c86bb37a8b16905d57d5278b:0
21636:S 23 Jan 04:40:33.743 * MASTER <-> SLAVE sync: receiving 4469862 bytes from master
21636:S 23 Jan 04:40:33.772 * MASTER <-> SLAVE sync: Flushing old data
21636:S 23 Jan 04:40:33.785 * MASTER <-> SLAVE sync: Loading DB in memory
21636:S 23 Jan 04:40:34.032 * MASTER <-> SLAVE sync: Finished with success
21636:S 23 Jan 04:40:34.033 * Background append only file rewriting started by pid 21640
21636:S 23 Jan 04:40:34.387 * AOF rewrite child asks to stop sending diffs.
21640:C 23 Jan 04:40:34.387 * Parent agreed to stop sending diffs. Finalizing AOF...
21640:C 23 Jan 04:40:34.387 * Concatenating 0.00 MB of AOF diff received from parent.
21640:C 23 Jan 04:40:34.388 * SYNC append only file rewrite performed
21640:C 23 Jan 04:40:34.389 * AOF rewrite: 10 MB of memory used by copy-on-write
21636:S 23 Jan 04:40:34.439 * Background AOF rewrite terminated with success
21636:S 23 Jan 04:40:34.439 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
21636:S 23 Jan 04:40:34.439 * Background AOF rewrite finished successfully

从输出日志看出,从库启动后,会跟主库进行数据同步,把主库的数据同步过来,主库运行一段时间后,从库也会把主库的历史数据同步过来。


4.验证
主库的数据大小
[root@pxc01 src]# ./redis-cli -h 192.168.56.91 -p 8379
192.168.56.91:8379> dbsize
(integer) 300000

从库的数据大小
[root@pxc02 src]# ./redis-cli -h 192.168.56.92 -p 8379
192.168.56.92:8379> dbsize
(integer) 300000

5.故障切换
当主库当掉之后,可以执行如下命令将从库升级为主库
192.168.56.92:8379> slaveof no one
OK

等主库好了之后可以执行如下命令将当期的主库设置为从库
192.168.56.92:8379> slaveof 192.168.56.91 8379
OK

主库宕机到恢复完成的时间里,之前从库升级主库写入的数据,重新建立主从关系后,这些数据会被覆盖,因为从新建立了主从关系,从库重新同步一份之前主库的数据过来。


-----------------------------Sentine哨兵模式安装-----------------------
创建好主从之后,我们接下部署sentine哨兵模式监控主从的状态,哨兵模式的作用就是监控主从状态,发现主库宕机后,从备库中挑选一个出来当选主库,对外部应用继续提高访问服务。

1.安装
安装步骤跟主从安装redis一样,这里省略

2.配置文件
sentinel.conf

  1. port 26379
  2. dir "/tmp"
  3. sentinel myid 3154c22b52f5fdca833d7a972bb0104e11e63b82
  4. sentinel monitor mymaster 192.168.56.91 8379 1
  5. sentinel config-epoch mymaster 1
  6. sentinel leader-epoch mymaster 1
  7. sentinel down-after-milliseconds mymaster 15000
  8. logfile "/opt/redis_sentinel/log/sentinel.log"
  9. protected-mode no
关键的参数:
port 端口号,哨兵程序运行的端口号
sentinel monitor mymaster 192.168.56.91 8379 1
mymaster:哨兵监控redis组叫mymaster,这个名字可以任意命名,符合命名要求即可
192.168.56.91 8379:这个是哨兵监控的主库的ip和端口号
1:表示仲裁值,这里表示有1个哨兵发现主库无法访问的时候,就可以从从库中选举新的主库,在多个哨兵的环境下,这个值可以根据需要设置,比如2,3等,意味着至少需要有2、3个哨兵都发现主库无法访问的情况下才重新选举主库。

sentinel down-after-milliseconds mymaster 15000
----------这个是sentinel连接master的超时时间,超过这个时间就认为master挂了,实现自动切换。这个默认是30秒,这个时间得调节好,大了会在真正出现故障的时候切换时间会长,小了有时候master由于持久化数据,繁忙不响应,会导致自动切换,实际只是瞬间不可用,现在认为设置为15秒为宜.


3.启动
/opt/redis/src/redis-sentinel /opt/redis/conf/sentinel.conf

4.登陆哨兵查看主从情况
./redis-cli -h 192.168.56.93 -p 26379
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.56.91:8379,slaves=1,sentinels=1

5.外部应用同步哨兵访问集群
通常情况下,我们通过JedisSentinelPool创建跟集群的连接,这样的话,主从关系改变了,我们的应用程序也不用修改连接,但是主从改变的过程中,外部应用不能访问.


  1. package com.hxl;

  2. import java.util.HashSet;
  3. import java.util.Set;

  4. import redis.clients.jedis.Jedis;
  5. import redis.clients.jedis.JedisSentinelPool;

  6. public class SentineTest {
  7.     
  8.     public static void main(String[] args) {

  9.         Set<String> sentinels = new HashSet<String>();
  10.         String hostAndPort1 = "192.168.56.93:26379";
  11.         sentinels.add(hostAndPort1);
  12.         String clusterName = "mymaster" ;
  13.         JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName,sentinels);
  14.         Jedis jedis = null;
  15.         try {
  16.             jedis = redisSentinelJedisPool.getResource();
  17.             jedis.set("test4", "aaa");
  18.             System.out.println(jedis.get("test4"));
  19.             //System.out.println(jedis.get("test2"));
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.         } finally {
  23.             jedis.close();
  24.         }
  25.         redisSentinelJedisPool.close();
  26.     }        
  27.         
  28. }
String hostAndPort1 = "192.168.56.93:26379" ##这里是指定哨兵所在的机器ip和占用的端口;
String clusterName = "mymaster" ;  ##这里要跟服务器设置的一致.

通常情况下,我们会部署多个哨兵,避免一个哨兵宕机了导致外部应用无法访问,部署多个哨兵的部署跟部署一个哨兵的步骤一致,只是配置文件中仲裁值根据需要设定。

--The End --


阅读(9258) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~