Keepalived +Mysql 主主同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived +Mysql 主主同步相关的知识,希望对你有一定的参考价值。

一、配置mysql主主同步
1,修改/etc/my.cnf,创建同步用户
Master104
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server-id = 1                    #backup这台设置2
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema       #忽略写入binlog日志的库
auto-increment-increment = 2             #字段变化增量值
auto-increment-offset = 1              #初始字段ID为1
slave-skip-errors = all                       #忽略所有复制产生的错误
[[email protected] ~]# service mysqld restart
#查看log bin日志和pos值位置
[[email protected] ~]# mysql -uroot -p
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.9.%‘ identified by ‘replication‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)



Backup106
#106配置Mysql主主同步
[[email protected] ~]# vim /etc/my.cnf 
[mysqld]
server-id = 2                    
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema       
auto-increment-increment = 2             
auto-increment-offset = 1              
slave-skip-errors = all    
[[email protected] ~]# service mysqld restart
#查看log bin日志和pos值位置
[[email protected] ~]# mysql -uroot -p
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.9.%‘ identified by ‘replication‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


2、配置Mysql主主同步
Master 104
[[email protected] ~]# mysql -uroot -p
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000002 | 625 | | msyql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

mysql> change master to
 -> master_host=‘192.168.9.106‘,
 -> master_user=‘replication‘,
 -> master_password=‘replication‘,
 -> master_log_file=‘mysql-bin.000002‘,
 -> master_log_pos=356;
Query OK, 0 rows affected (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 192.168.9.106
 Master_User: replication
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000002
 Read_Master_Log_Pos: 356
 Relay_Log_File: 104-relay-bin.000002
 Relay_Log_Pos: 253
 Relay_Master_Log_File: mysql-bin.000002
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

Backup 106
[[email protected] ~]# mysql -uroot -p
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000002 | 356 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

mysql> change master to
-> master_host=‘192.168.9.104‘,
-> master_user=‘replication‘,
-> master_password=‘replication‘,
-> master_log_file=‘mysql-bin.000002‘,
-> master_log_pos=625;
Query OK, 0 rows affected (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 192.168.9.104
 Master_User: replication
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000002
 Read_Master_Log_Pos: 625
 Relay_Log_File: 106-relay-bin.000002
 Relay_Log_Pos: 253
 Relay_Master_Log_File: mysql-bin.000002
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

#如果Slave_IO_Running和Slave_SQL_Running为Yes,则配置主主正常,可以创建测试下

二、配置keepalived实现双主热备
1,#下载安装keepalived
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
[[email protected] src]# yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel cmake libnl* libpopt* popt-static openssl-devel
[[email protected] src]# tar xf keepalived-1.2.13.tar.gz 
[[email protected] src]# cd keepalived-1.2.13
[[email protected] keepalived-1.2.13]# ./configure --prefix=/usr/local/keepalived  --disable-fwmark 
[[email protected] keepalived-1.2.13]# make
[[email protected] keepalived-1.2.13]# make install
2,#配置keepalived配置成系统服务
[[email protected] keepalived-1.2.13]# cd /usr/local/keepalived/
[[email protected] keepalived]# cp etc/rc.d/init.d/keepalived /etc/init.d/
[[email protected] keepalived]# cp etc/sysconfig/keepalived /etc/sysconfig/
[[email protected] keepalived]# mkdir /etc/keepalived
[[email protected] keepalived]# cp etc/keepalived/keepalived.conf /etc/keepalived/
[[email protected] keepalived]# cp sbin/keepalived /usr/sbin/
[[email protected] keepalived]# chkconfig keepalived on
[[email protected] keepalived]# chkconfig --list keepalived 
keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  3,#配置mysql热备
Mater 104
[[email protected] keepalived]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
        router_id Mysql_HA    #标识,双主相同
}

vrrp_instance zabbix_mysql {
        state Backup               #俩台都设置Backup
        interface eth0
        virtual_router_id 53     #主备相同
        priority 100                 #优先级,backup设置90
        advert_int 1
        nopreempt                  #不主动抢占资源,只在master这台优先级高的设置,backup不设置
        authentication {
                auth_type PASS
                auth_pass sina 
        }
        virtual_ipaddress {
                192.168.9.123
         }
   }
   virtual_server 192.168.9.123 3306 {
         delay_loop 6
         nat_mask 255.255.255.0
         persistence_timeout 50
         protocol TCP
         real_server 192.168.9.104 3306 {
                  weight 3
                  notify_down "/etc/keepalived/mysql.sh"
                  TCP_CHECK {
                          connect_timeout 5
                          nb_get_retry 2
                          delay_before_retry 3
                  }
         }
 }
[[email protected] keepalived]# vim /etc/keepalived/mysql.sh
#!/bin/bash
/etc/init.d/keepalived stop

#启动keepalived
[[email protected] keepalived]# /etc/init.d/keepalived start
[[email protected] keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:ff:a5:92 brd ff:ff:ff:ff:ff:ff
 inet 192.168.9.104/24 brd 192.168.9.255 scope global eth0
 inet 192.168.9.123/32 scope global eth0
 inet6 fe80::20c:29ff:feff:a592/64 scope link 
 valid_lft forever preferred_lft forever


Backup106
[[email protected] keepalived]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
        router_id Mysql_HA    
}

vrrp_instance zabbix_mysql {
        state Backup               
        interface eth0
        virtual_router_id 53     
        priority 90                 
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass sina 
        }
        virtual_ipaddress {
                192.168.9.123
         }
   }
   virtual_server 192.168.9.123 3306 {
         delay_loop 6
         nat_mask 255.255.255.0
         persistence_timeout 50
         protocol TCP
         real_server 192.168.9.106 3306 {
                  weight 3
                  notify_down "/etc/keepalived/mysql.sh"
                  TCP_CHECK {
                          connect_timeout 5
                          nb_get_retry 2
                          delay_before_retry 3
                  }
         }
 }
[[email protected] keepalived]# vim /etc/keepalived/mysql.sh
#!/bin/bash
/etc/init.d/keepalived stop
[[email protected] keepalived]# chmod +x /etc/keepalived/mysql.sh
#启动keepalived
[[email protected] keepalived]# /etc/init.d/keepalived start
[[email protected] keepalived]# ip addr

4,验证
104停止mysqld
[[email protected] keepalived]# service mysqld stop
Shutting down MySQL.... SUCCESS! 
[[email protected] keepalived]# ip addr 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:ff:a5:92 brd ff:ff:ff:ff:ff:ff
 inet 192.168.9.104/24 brd 192.168.9.255 scope global eth0
 inet6 fe80::20c:29ff:feff:a592/64 scope link 
 valid_lft forever preferred_lft forever
106
[[email protected] keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:c1:76:6b brd ff:ff:ff:ff:ff:ff
 inet 192.168.9.106/24 brd 192.168.9.255 scope global eth0
 inet 192.168.9.123/32 scope global eth0
 inet6 fe80::20c:29ff:fec1:766b/64 scope link 
 valid_lft forever preferred_lft forever


以上是关于Keepalived +Mysql 主主同步的主要内容,如果未能解决你的问题,请参考以下文章

Keepalived+MySQL主主数据同步实现数据库高可用(HA)

MM(主主数据库)+keepalived主备高可用集群

inotify+rsync+mysql主主复制+keepalived实现zabbix高可用

mysql主主配置

mysql主主同步

[转帖]MySQL+keepalived用keepalived实现MySQL主主模式的高可用