利用keepalived构建高可用MYSQL-HA_第1页
利用keepalived构建高可用MYSQL-HA_第2页
利用keepalived构建高可用MYSQL-HA_第3页
利用keepalived构建高可用MYSQL-HA_第4页
利用keepalived构建高可用MYSQL-HA_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、利用keepalived构建高可用MySQL-HA在MySQL-HA环境中,MySQL互为主从关系,这样就保证了两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换。构架环境拓扑如下1. MySQL-VIP:  2. MySQL-master1:  3. MySQL-master2:   4. OS版本:redhat 5.55.6. Keepalived版本: 一、MySQL master-master配置1、修改MySQL配置文件两台MySQL均如要开启b

2、inlog日志功能,开启方法:在MySQL配置文件MySQLd段中加上log-bin=MySQL-bin选项两台MySQL的server-ID不能一样,默认情况下两台MySQL的serverID都是1,需将其中一台修改为2即可2、将设为的主服务器在上新建授权用户1. MySQL> grant replication slave on *.* to 'replication''%' identified by 'replication' 

3、60;2. Query OK, 0 rows affected (0.00 sec)  3.  4. MySQL> show master status;  5. +-+-+-+-+  6. | File             | Position | Binlog_Do_DB |

4、 Binlog_Ignore_DB |  7. +-+-+-+-+  8. | MySQL-bin.000003 |      374 |              |             

5、60;    |   9. +-+-+-+-+  10. 1 row in set (0.00 sec) 在上将设为自己的主服务器1. MySQL> change master to master_host='',master_user='replication',master_password='replication',master_log_file='MySQ

6、L-bin.000003',master_log_pos=374;  2. Query OK, 0 rows affected (0.05 sec)  3.  4. MySQL> start slave;  5. Query OK, 0 rows affected (0.00 sec)  6.  7. MySQL> show slave statu

7、sG  8. * 1. row *  9.              Slave_IO_State: Waiting for master to send event  10.               &#

8、160; Master_Host:   11.                 Master_User: replication  12.                 Master_Port: 3306  

9、13.               Connect_Retry: 60  14.             Master_Log_File: MySQL-bin.000003  15.         Read_Maste

10、r_Log_Pos: 374  16.              Relay_Log_File: MySQL-master2-relay-bin.000002  17.               Relay_Log_Pos: 235  18.   

11、    Relay_Master_Log_File: MySQL-bin.000003  19.            Slave_IO_Running: Yes  20.           Slave_SQL_Running: Yes  21.    &#

12、160;        Replicate_Do_DB:   22.         Replicate_Ignore_DB:   23.          Replicate_Do_Table:   24.      Replicate_

13、Ignore_Table:   25.     Replicate_Wild_Do_Table:   26. Replicate_Wild_Ignore_Table:   27.                  Last_Errno: 0  28.      

14、;            Last_Error:   29.                Skip_Counter: 0  30.         Exec_Master_Log_Pos: 374

15、 31.             Relay_Log_Space: 235  32.             Until_Condition: None  33.             &

16、#160;Until_Log_File:   34.               Until_Log_Pos: 0  35.          Master_SSL_Allowed: No  36.          M

17、aster_SSL_CA_File:   37.          Master_SSL_CA_Path:   38.             Master_SSL_Cert:   39.           Master_SSL

18、_Cipher:   40.              Master_SSL_Key:   41.       Seconds_Behind_Master: 0  42. 1 row in set (0.00 sec) 3、将设为的主服务器在上新建授权用户1. MySQL> 

19、;grant replication slave on *.* to 'replication''%' identified by 'replication'  2. Query OK, 0 rows affected (0.00 sec)  3.  4. MySQL> show master status;  5. +-+

20、-+-+-+  6. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  7. +-+-+-+-+  8. | MySQL-bin.000003 |      374 |  &#

21、160;           |                  |   9. +-+-+-+-+  10. 1 row in set (0.00 sec) 在上,将设为自己的主服务器1. MySQL> 

22、;change master to master_host='',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;  2. Query OK, 0 rows affected (0.05 sec)  3.  4. MySQL> 

23、start slave;  5. Query OK, 0 rows affected (0.00 sec)  6.  7. MySQL> show slave statusG  8. * 1. row *  9.              Slave_IO_State: W

24、aiting for master to send event  10.                 Master_Host:   11.                 Master_User:

25、 replication  12.                 Master_Port: 3306  13.               Connect_Retry: 60  14.     &#

26、160;       Master_Log_File: MySQL-bin.000003  15.         Read_Master_Log_Pos: 374  16.              Relay_Log_File: MySQL-master1-rela

27、y-bin.000002  17.               Relay_Log_Pos: 235  18.       Relay_Master_Log_File: MySQL-bin.000003  19.            Sl

28、ave_IO_Running: Yes  20.           Slave_SQL_Running: Yes  21.             Replicate_Do_DB:   22.         Replicate_Igno

29、re_DB:   23.          Replicate_Do_Table:   24.      Replicate_Ignore_Table:   25.     Replicate_Wild_Do_Table:   26. Replicate_Wild_Ignore_Table:   27.  &

30、#160;               Last_Errno: 0  28.                  Last_Error:   29.        

31、0;       Skip_Counter: 0  30.         Exec_Master_Log_Pos: 374  31.             Relay_Log_Space: 235  32.      

32、;       Until_Condition: None  33.              Until_Log_File:   34.               Until_Log_Pos: 0 

33、0;35.          Master_SSL_Allowed: No 36.          Master_SSL_CA_File:   37.          Master_SSL_CA_Path:   38.     

34、;        Master_SSL_Cert:   39.           Master_SSL_Cipher:   40.              Master_SSL_Key:   41.   &

35、#160;   Seconds_Behind_Master: 0  42. 1 row in set (0.00 sec) 4、MySQL同步测试如上述均正确配置,现在任何一台MySQL上更新数据都会同步到另一台MySQL,MySQL同步在此不再演示二、keepalived安装及配置1、服务器上keepalived安装及配置安装keepalived1. #tar zxvf keepalived-.tar.gz  2. #cd keepalived

36、-  3. #./configure -prefix=/usr/local/keepalived -with-kernel-dir=/usr/src/kernels/-164.el5-i686  4. #make && make install 配置keepalived我们自己在新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件1. #mkdir /etc/keepalived  2. #vi /etc/keepal

37、ived/keepalived.conf  3. ! Configuration File for keepalived  4. global_defs   5.      notification_email   6.      luwenju  7.        8.     

38、60;notification_email_from luwenju  9.10.      smtp_connect_timeout 30  11.      router_id MySQL-ha  12.        13.  14. vrrp_instance VI_1   15.     

39、 state BACKUP   #两台配置此处均是BACKUP  16.      interface eth0  17.      virtual_router_id 51  18.      priority 100   #优先级,另一台改为90  19.    &

40、#160; advert_int 1  20.      nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置  21.      authentication   22.      auth_type PASS  23.      auth_pass 111

41、1  24.        25.      virtual_ipaddress   26.       27.        28.        29.  30. virtual_server  3306   31. 

42、0;    delay_loop 2   #每个2秒检查一次real_server状态  32.      lb_algo wrr   #LVS算法  33.      lb_kind DR    #LVS模式  34.      persisten

43、ce_timeout 60   #会话保持时间  35.      protocol TCP  36.      real_server  3306   37.      weight 3  38.      notify_down /usr/local/My

44、SQL/bin/MySQL.sh  #检测到服务down后执行的脚本  39.      TCP_CHECK   40.      connect_timeout 10    #连接超时时间  41.      nb_get_retry 3       #重连

45、次数  42.      delay_before_retry 3   #重连间隔时间  43.      connect_port 3306   #健康检查端口  44.        45.       编写检测服务down后所要执行的脚本1. #vi /

46、usr/local/MySQL/bin/MySQL.sh  2. #!/bin/sh  3. pkill keepalived  4. #chmod +x /usr/local/MySQL/bin/MySQL.sh 注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkill keepalived强制杀死keepalived进程,从而实现了

47、MySQL故障自动转移。另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP启动keepalived1. #/usr/local/keepalived/sbin/keepalived D  2. #ps -aux | grep keepalived 测试找一台局域网PC,然后去ping  MySQL的VIP,这时候MySQL的VIP是可以ping的通的停止MySQL服务,看keepalived

48、健康检查程序是否会触发我们编写的脚本2、上keepalived安装及配置安装keepalived1. #tar zxvf keepalived-.tar.gz  2. #cd keepalived-  3. #./configure -prefix=/usr/local/keepalived -with-kernel-dir=/usr/src/kernels/-164.el5-i686  4. #make && make install 配置keepalive

49、d这台配置和上面基本一样,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP1. #mkdir /etc/keepalived  2. #vi /etc/keepalived/keepalived.conf  3. ! Configuration File for keepalived  4. global_defs   5.      notification_email   6. 

50、60;    luwenju  7.        8.      notification_email_from luwenju  9.10.      smtp_connect_timeout 30  11.      router_id MySQL-ha  12. 

51、0;      13.  14. vrrp_instance VI_1   15.      state BACKUP  16.      interface eth0  17.      virtual_router_id 51  18.      

52、;priority 90  19.      advert_int 1  20.      authentication   21.      auth_type PASS  22.      auth_pass 1111  23.       &#

53、160;24.      virtual_ipaddress   25.        26.        27.        28.  29. virtual_server  3306   30.      delay_loop 2

54、 31.      lb_algo wrr  32.      lb_kind DR  33.      persistence_timeout 60  34.      protocol TCP  35.      real_server 

55、0;3306   36.      weight 3  37.      notify_down /usr/local/MySQL/bin/MySQL.sh  38.      TCP_CHECK   39.      connect_timeout 10  40.   

56、0;  nb_get_retry 3  41.      delay_before_retry 3  42.      connect_port 3306  43.        44.       编写检测服务down后所要执行的脚本1. #vi /usr/local/MySQL/bin

57、/MySQL.sh  2. #!/bin/sh  3. pkill keepalived  4. #chmod +x /usr/local/MySQL/bin/MySQL.sh  5.  6. 启动keepalived  7. #/usr/local/keepalived/sbin/keepalived D  8. #ps -aux | grep keepalived 测试停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本三、测试MySQL远程登录测试我们找一台安装有MySQL客户端的windows,然后登录VIP,看是否能登录,在登录之两台MySQL服务器都要授权允许从远程登录1. MySQL> grant all privileges on *.* to 'root''%'

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论