一、基本信息说明

【DB1】

IP: 192.168.102.144

hostname: LVS-Real1

【DB2】

IP: 192.168.102.145

hostname: LVS-Real2

【VIP】

IP:  192.168.102.146

二、MySQL配置主主互备

 1.配置DB1和DB2的/etc/my.cnf

【DB1】

  1.  
  1. [root@LVS-Real1 ~]# more /etc/my.cnf
  1. [client]
  2. port =
  3. socket = /tmp/mysql.sock
  4.  
  5. [mysqld]
  6. user=mysql
  7. port =
  8. server_id = 1 #需保证唯一性
  9. socket=/tmp/mysql.sock
  10. basedir =/usr/local/mysql
  11. datadir =/usr/local/mysql/data
  12. pid-file=/usr/local/mysql/data/mysqld.pid
  13. log-error=/usr/local/mysql/log/mysql-error.log
  14.  
  15. log-bin=mysql-bin #开启二进制日志
  16. relay-log=mysql-relay-bin
  17.  
  18. replicate-wild-ignore-table=mysql.% #忽略复制mysql数据库下的所有对象,以下依次类推
  19. replicate-wild-ignore-table=test.%
  20. replicate-wild-ignore-table=information_schema.%

【DB2】

  1. [root@LVS-Real2 ~]# more /etc/my.cnf
  2. [client]
  3. port =
  4. socket = /tmp/mysql.sock
  5.  
  6. [mysqld]
  7. user=mysql
  8. port =
  9. server_id = 2 #需保证唯一性
  10. socket=/tmp/mysql.sock
  11. basedir =/usr/local/mysql
  12. datadir =/usr/local/mysql/data
  13. pid-file=/usr/local/mysql/data/mysqld.pid
  14. log-error=/usr/local/mysql/log/mysql-error.log
  15.  
  16. log-bin=mysql-bin #开启二进制日志
  17. relay-log=mysql-relay-bin
  18. replicate-wild-ignore-table=mysql.%
  19. replicate-wild-ignore-table=test.%
  20. replicate-wild-ignore-table=information_schema.%

 2.手动同步数据库

如果DB1上有数据,在执行主主互备之前,需要将DB1和DB2上两个数据库保持同步,首先在DB1上执行备份,执行如下语句:

  1. mysql>flush tables with read lock;

在关闭上述终端的情况下,新开启一个终端打包数据库。

3.创建复制用户并授权

  • 首先在【DB1】上的MySQL库中创建复制用户
  1. mysql>grant replication slave on *.* to 'repl_user'@'192.168.102.145' identified by 'repl_passwd';
  • 在【DB1】上执行如下语句,并记下File和Position的值
  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +------------------+----------+--------------+------------------+-------------------+
  5. | mysql-bin.000002 | 1004 | | | |
  6. +------------------+----------+--------------+------------------+-------------------+
  7. row in set (0.02 sec)
  • 然后在【DB2】上将DB1设为自己的主服务器,如下:
  1. change master to \
  2. master_host='192.168.102.144', #DB1的IP地址
  3. master_user='repl_user',
  4. master_password='repl_passwd',
  5. master_log_file='mysql-bin.000002', #DB1上查询出的File值
  6. master_log_pos=; #DB1上查询出的Position值
  • 在【DB2】上启动slave服务,并查询slave的运行状态
  1. mysql>start slave;

查看Slave的运行状态,这里需要关注Slave_IO_Running和Slave_SQL_Running.这两个就是在Slave节点上运行的主从复制线程,正常情况下两个值都应该为Yes.

  1. mysql> show slave status\G;
  2. *************************** . row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.102.144
  5. Master_User: repl_user
  6. Master_Port:
  7. Connect_Retry:
  8. Master_Log_File: mysql-bin.
  9. Read_Master_Log_Pos:
  10. Relay_Log_File: mysql-relay-bin.
  11. Relay_Log_Pos:
  12. Relay_Master_Log_File: mysql-bin.
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table: mysql.%,test.%,information_schema.%
  21. Last_Errno:
  22. Last_Error:
  23. Skip_Counter:
  24. Exec_Master_Log_Pos:
  25. Relay_Log_Space:
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos:
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master:
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno:
  38. Last_IO_Error:
  39. Last_SQL_Errno:
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id:
  43. Master_UUID: 64e9b20f-2eee-11e8-ab62-000c29889112
  44. Master_Info_File: /usr/local/mysql/data/master.info
  45. SQL_Delay:
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  48. Master_Retry_Count:
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position:
  57. Replicate_Rewrite_DB:
  58. Channel_Name:
  59. Master_TLS_Version:
  60. row in set (0.00 sec)
  • 接下来开始配置从DB2到DB1的MySQL主从复制,这个配置过程和上面一样。
  • 首先在【DB2】上的MySQL库中创建复制用户,并查看数据库状态,记下File和Position值。
  1. grant replication slave on *.* to 'repl_user'@'192.168.102.144' identified by 'repl_passwd';
  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +------------------+----------+--------------+------------------+-------------------+
  5. | mysql-bin.000002 | 1004 | | | |
  6. +------------------+----------+--------------+------------------+-------------------+
  7. row in set (0.02 sec)
  • 然后在【DB1】上将DB2设为自己的主服务器
  1. change master to \
  2. master_host='192.168.102.145',
  3. master_user='repl_user',
  4. master_password='repl_passwd',
  5. master_log_file='mysql-bin.000002',
  6. master_log_pos=;
  • 在【DB1】上启动slave服务
  1. mysql>start slave;

在【DB1】查看Slave的运行状态,这里需要关注Slave_IO_Running和Slave_SQL_Running.这两个就是在Slave节点上运行的主从复制线程,正常情况下两个值都应该为Yes.

  1. mysql> show slave status\G;
  2. *************************** . row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.102.145
  5. Master_User: repl_user
  6. Master_Port:
  7. Connect_Retry:
  8. Master_Log_File: mysql-bin.
  9. Read_Master_Log_Pos:
  10. Relay_Log_File: mysql-relay-bin.
  11. Relay_Log_Pos:
  12. Relay_Master_Log_File: mysql-bin.
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table: mysql.%,test.%,information_schema.%
  21. Last_Errno:
  22. Last_Error:
  23. Skip_Counter:
  24. Exec_Master_Log_Pos:
  25. Relay_Log_Space:
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos:
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master:
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno:
  38. Last_IO_Error:
  39. Last_SQL_Errno:
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id:
  43. Master_UUID: a35a032d-2ef8-11e8-bd3c-000c2910f959
  44. Master_Info_File: /usr/local/mysql/data/master.info
  45. SQL_Delay:
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  48. Master_Retry_Count:
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position:
  57. Replicate_Rewrite_DB:
  58. Channel_Name:
  59. Master_TLS_Version:
  60. row in set (0.00 sec)
  • 至此,主主复制配置完毕;

三、Keepalived的安装与配置

1.下载Keepalived

  1. http://www.keepalived.org/download.html

2.安装Keepalived

  1. #.安装依赖包
  2. yum -y install gcc openssl-devel libnfnetlink libnfnetlink-devel
  3.  
  4. #.开始安装
  5. tar -xvf keepalived-1.2..tar.gz
  6. cd keepalived-1.2.
  7. ./configure --prefix=/usr/local/keepalived
    make
    make install

3.复制文件到相应目录

  1. cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  2.  
  3. cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  4.  
  5. cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

  6. mkdir -p /etc/keepalived
  7. cp -r /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived

4.配置keepalived.conf

 【DB1】

  1. [root@LVS-Real1 keepalived]# more /etc/keepalived/keepalived.conf
  2. global_defs {
  3. notification_email {
  4. guanyy0911@.com
  5. }
  6. notification_email_from guanyy0911@.com
  7. smtp_server 127.0.0.1
  8. smtp_connect_timeout
  9. router_id MySQL-ha
  10. }
  11.  
  12. vrrp_instance VI_1 {
  13. state BACKUP
  14. interface eth0
  15. virtual_router_id
  16. priority 100 #设置优先级
  17. advert_int
  18. nopreempt #设置不抢占,当因为故障切换到DB2后,如果DB1恢复,则不再切回DB1,直到DB2出现故障才切换回DB1
  19. authentication {
  20. auth_type PASS
  21. auth_pass
  22. }
  23. virtual_ipaddress {
  24. 192.168.102.146 #设置虚拟IP,即VIP
  25. }
  26. }
  27.  
  28. virtual_server 192.168.102.146 {
  29. delay_loop
  30. lb_algo wrr
  31. lb_kind DR
  32. persistence_timeout
  33. protocol TCP
  34. real_server 192.168.102.144 {
  35. weight
  36. notify_down /etc/keepalived/mysql.sh
  37. TCP_CHECK {
  38. connect_timeout
  39. nb_get_retry
  40. delay_before_retry
  41. connect_port
  42. }
  43. }
  44. }

mysql.sh脚本的内容如下:

  1. [root@LVS-Real1 keepalived]# more /etc/keepalived/mysql.sh
  2. #!/bin/bash
  3. pkill keepalived

说明:该脚本主要用来当MySQL服务关闭时杀掉keepalived进程,进而达到切换的目的。

【DB2】

  1. [root@LVS-Real2 keepalived]# more /etc/keepalived/keepalived.conf
  2. global_defs {
  3. notification_email {
  4. guanyy0911@.com
  5. }
  6. notification_email_from guanyy0911@.com
  7. smtp_server 127.0.0.1
  8. smtp_connect_timeout
  9. router_id MySQL-ha
  10. }
  11.  
  12. vrrp_instance VI_1 {
  13. state BACKUP
  14. interface eth0
  15. virtual_router_id
  16. priority 90 #设置优先级,要比DB1低
  17. advert_int
  18. authentication {
  19. auth_type PASS
  20. auth_pass
  21. }
  22. virtual_ipaddress {
  23. 192.168.102.146 #设置虚拟IP,即VIP
  24. }
  25. }
  26.  
  27. virtual_server 192.168.102.146 {
  28. delay_loop
  29. lb_algo wrr
  30. lb_kind DR
  31. persistence_timeout
  32. protocol TCP
  33. real_server 192.168.102.145 {
  34. weight
  35. notify_down /etc/keepalived/mysql.sh
  36. TCP_CHECK {
  37. connect_timeout
  38. nb_get_retry
  39. delay_before_retry
  40. connect_port
  41. }
  42. }
  43. }

mysql.sh脚本的内容如下:

  1. [root@LVS-Real1 keepalived]# more /etc/keepalived/mysql.sh
  2. #!/bin/bash
  3. pkill keepalived

说明:该脚本主要用来当MySQL服务关闭时杀掉keepalived进程,进而达到切换的目的。

5.启动keepalived

  1. service keepalived start

5.检查并测试VIP是否可用

如果在/var/log/messages文件中有如下信息,说明VIP已经可用。

  1. Mar :: LVS-Real1 Keepalived[]: Stopping Keepalived v1.2.18 (/,)
  2. Mar :: LVS-Real1 Keepalived[]: Starting Keepalived v1.2.18 (/,)
  3. Mar :: LVS-Real1 Keepalived[]: Starting Healthcheck child process, pid=
  4. Mar :: LVS-Real1 Keepalived[]: Starting VRRP child process, pid=
  5. Mar :: LVS-Real1 Keepalived_vrrp[]: Netlink reflector reports IP 192.168.102.144 added
  6. Mar :: LVS-Real1 Keepalived_vrrp[]: Netlink reflector reports IP fe80::20c:29ff:fe88: added
  7. Mar :: LVS-Real1 Keepalived_vrrp[]: Registering Kernel netlink reflector
  8. Mar :: LVS-Real1 Keepalived_vrrp[]: Registering Kernel netlink command channel
  9. Mar :: LVS-Real1 Keepalived_healthcheckers[]: Netlink reflector reports IP 192.168.102.144 added
  10. Mar :: LVS-Real1 Keepalived_vrrp[]: Registering gratuitous ARP shared channel
  11. Mar :: LVS-Real1 Keepalived_vrrp[]: Opening file '/etc/keepalived/keepalived.conf'.
  12. Mar :: LVS-Real1 Keepalived_healthcheckers[]: Netlink reflector reports IP fe80::20c:29ff:fe88: added
  13. Mar :: LVS-Real1 Keepalived_vrrp[]: Configuration is using : Bytes
  14. Mar :: LVS-Real1 Keepalived_healthcheckers[]: Registering Kernel netlink reflector
  15. Mar :: LVS-Real1 Keepalived_healthcheckers[]: Registering Kernel netlink command channel
  16. Mar :: LVS-Real1 Keepalived_healthcheckers[]: Opening file '/etc/keepalived/keepalived.conf'.
  17. Mar :: LVS-Real1 Keepalived_vrrp[]: Using LinkWatch kernel netlink reflector...
  18. Mar :: LVS-Real1 Keepalived_healthcheckers[]: Configuration is using : Bytes
  19. Mar :: LVS-Real1 Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering BACKUP STATE
  20. Mar :: LVS-Real1 Keepalived_vrrp[]: VRRP sockpool: [ifindex(), proto(), unicast(), fd(,)]
  21. Mar :: LVS-Real1 Keepalived_healthcheckers[]: Using LinkWatch kernel netlink reflector...

此时从外部的客户端可以ping通该VIP.

  1. C:\Users\Sakura>ping 192.168.102.146
  2.  
  3. 正在 Ping 192.168.102.146 具有 字节的数据:
  4. 来自 192.168.102.146 的回复: 字节= 时间<1ms TTL=
  5. 来自 192.168.102.146 的回复: 字节= 时间<1ms TTL=
  6. 来自 192.168.102.146 的回复: 字节= 时间<1ms TTL=
  7. 来自 192.168.102.146 的回复: 字节= 时间<1ms TTL=
  8.  
  9. 192.168.102.146 Ping 统计信息:
  10. 数据包: 已发送 = ,已接收 = ,丢失 = (% 丢失),
  11. 往返行程的估计时间(以毫秒为单位):
  12. 最短 = 0ms,最长 = 0ms,平均 = 0ms

6. 同时启动DB1和DB2上的mysql和keepalived服务

  1. service mysql.server start
  2. service keepalived start

7.从第三方客户端通过VIP来登录数据库。看是否可以登录。

我们通过navicat进行登录,发现是可以通过VIP登录。之后查询当前使用是哪个数据库。如下图查询到,使用的是DB1.

  7.我们停掉DB1数据库,看是否会切换到DB2上。

     通过实验发现,发现已经切换到DB2上了。

8. 我们再次启动DB1上的Mysql服务和keepalived服务(已经通过脚本实现MySQL服务关闭的同时,脚本会杀掉keepalived进程)

通过实验发现,由于我们设置的是不抢占,在DB1启动后,并没有切换回DB2. 达到预期的目的。

9.这次我们停掉DB2上的MySQL服务,看是否会切换回DB1.

通过实验发现,在停掉DB2上的MySQL服务后,已经自动切换回DB1上。达到预期目的。

特别提示:当MySQL被关闭时,其所在的主机的keepalived也同时被关闭。但在重新启动MySQL服务时,keepalived不会自动启动,需要手动启动。

10.至此,整个配置过程完毕!

【Keepalived+MySQL】MySQL双主互备+高可用的更多相关文章

  1. 企业级-Mysql双主互备高可用负载均衡架构(基于GTID主从复制模式)(原创)

    前言:          原理与思想        这里选用GTID主从复制模式Mysql主从复制模式,是为了更加确保主从复制的正确性.健康性与易配性.这里做的是两服务器A,B各有Mysql实例331 ...

  2. Mysql+Keepalived双主热备高可用操作记录

    我们通常说的双机热备是指两台机器都在运行,但并不是两台机器都同时在提供服务.当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,而且切换的时间非常短.MySQL双主复制,即互为Mast ...

  3. 使用Keepalived实现Nginx的自动重启及双主热备高可用

    1.概述 之前我们使用Keepalived实现了Nginx服务的双机主备高可用,但是有几个问题没有解决,今天一起探讨一下. 1)在双机主备机制中,Keepalived服务如果宕了,会自动启用备机进行服 ...

  4. mysql+keepalived 双主热备高可用

    理论介绍:我们通常说的双机热备是指两台机器都在运行,但并不是两台机器都同时在提供服务.当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,而且切换的时间非常短.MySQL双主复制,即互 ...

  5. haproxy + keepalived 实现web 双主模型的高可用负载均衡

    参考文章 http://xz159065974.blog.51cto.com/8618592/1405812 http://blog.chinaunix.net/uid-25266990-id-398 ...

  6. [Mysql高可用]——双主互备+keepalived

    实验架构图    实验环境 主机名 操作系统 Mysql版本 keepalived版本 主机IP VIP lyj1(Master/Slave) Red Hat release 6.5 Mysql5.6 ...

  7. Mysql双主互备+keeplived高可用架构介绍

    一.Mysql双主互备+keeplived高可用架构介绍 Mysql主从复制架构可以在很大程度保证Mysql的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力.但是在这种 ...

  8. Mysql双主互备+keeplived高可用架构(部分)

    一.Mysql双主互备+keeplived高可用架构介绍 Mysql主从复制架构可以在很大程度保证Mysql的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力.但是在这种 ...

  9. mysql双主互备

    mysql主从同步使得数据可以从一个数据库服务器复制到其他服务器上,在复制数据时,一个服务器充当主服务器(master),其余的服务器充当从服务器(slave),备服务器从主服务器同步数据,完成数据的 ...

随机推荐

  1. Second Highest Salary

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  2. 浅谈.htaccess文件--避免滥用.htaccess文件

    .htaccess文件提供了一种目录级别的修改配置的方式. NOTE: 如果你拥有修改apache配置文件的权限,那么完全没有必要使用.htaccess文件.使用.htaccess文件会拖慢apach ...

  3. window.returnValue使用方法

    returnValue是javascript中html的window对象的属性,目的是返回窗口值,当用window.showModalDialog函数打开一个IE的模式窗口(模式窗口知道吧,就是打开后 ...

  4. instanceof 关键字

    boolean = Object(类引用名) instanceof  Class(类名) 作用:判断符号左边的引用指向的对象是否是右边这个类的对象:

  5. idea字体模糊

    用jdk1.8的jre替换idea的jre64,但是记得在lib里加上jdk的lib中的tools.jar. 如图: 然后 将原来jre64的TOOLS.jar拷贝到替换后的jre的lib目录下,重启 ...

  6. 详解Unity 4.6新UI的布局

    本文所讲的是Unity 4.6中新加入的uGUI,官方称Unity UI,而不是过去的OnGUI式的旧UI(官方称Legacy GUI). 我曾经在8月份对照4.6 Beta的文档写过一篇笔记学习Un ...

  7. WordPress企业建站心得

    回头聊聊我用WordPress做企业网站的事.说是企业网站,其实就是一个小的企业展示网站.事情要从我爸开了一家自行车店开始说起,自从他开了自行车店,不但开始学着玩起了微信(因为要做微信营销),又想到了 ...

  8. 剑指Offer的学习笔记(C#篇)-- 和为S的连续正数序列

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  9. idea右侧的工具栏不见,包括maven以及其他

    通用,设置后右侧工具栏可见 可以通过view-->windows tool看到maven的

  10. NSCopying

    ///< .h @interface ChatManager : NSObject <NSCopying> @property (nonatomic) NSUInteger inde ...