四、MySQL主主同步与高可用

Zabbix高可用搭建系列:
一、Zabbix高可用架构
二、编译安装Zabbix
三、实现Zabbix高可用
四、MySQL主主同步与高可用


1.主主同步

Master:192.168.164.152

~]# cat /etc/my.cnf			#我们这儿使用GTID实现复制
    [mysqld]
    datadir = /opt/mysql/data
    basedir = /opt/mysql
    socket = /tmp/mysql.sock
    symbolic-links = 0
 
    skip-name-resolve
 
    slave_skip_errors = 1062                 #跳过1062错误, 1062错误是指一些主键重复
 
    server_id = 1                            #数据库唯一ID,主从ID不能相同
    log-bin = /opt/mysql/log/binary-log      #开启binlog日志
    relay-log = /opt/mysql/log/relay-log     #开启relay日志
    binlog_format = mixed                    #复制模式,混合复制
    sync_binlog = 1                          #开启binlog日志同步到磁盘
    auto-increment-increment = 2             #自增步长
    auto-increment-offset = 1                #自增起始值
 
    binlog-do-db = zabbix                    #同步的库
    binlog-ignore-db = mysql
    binlog-ignore-db = sys 
    binlog-ignore-db = performance_schema
    binlog-ignore-db = information_schema 
 
    replicate-do-db = zabbix     
    replicate-ignore-db = mysql
    replicate-ignore-db = sys 
    replicate-ignore-db = performance_schema
    replicate-ignore-db = information_schema
 
    gtid_mode=on                             #启用GTID复制
    enforce_gtid_consistency=on
 
    [mysqld_safe]
    log-error=/opt/mysql/log/error.log
    pid-file=/opt/mysql/data/mysql.pid
 
    !includedir /etc/my.cnf.d
 
mysql> grant replication slave on *.* to 'zabbixM'@'192.168.164.153' identified by 'zabbixM';    #给backup同步master数据创建用户
mysql> flush privileges;

~]# service mysqld restart

Backup:192.168.164.153

~]# cat /etc/my.cnf
    [mysqld]
    datadir = /opt/mysql/data
    basedir = /opt/mysql
    socket = /tmp/mysql.sock
    symbolic-links = 0
 
    skip-name-resolve
 
    slave_skip_errors = 1062
 
    server_id = 2
    log-bin = /opt/mysql/log/binary-log
    relay-log = /opt/mysql/log/relay-log
    binlog_format = mixed
    sync_binlog = 1
    auto-increment-increment = 2
    auto-increment-offset = 2
 
    binlog-do-db = zabbix
    binlog-ignore-db = mysql
    binlog-ignore-db = sys 
    binlog-ignore-db = performance_schema
    binlog-ignore-db = information_schema
 
    replicate-do-db = zabbix
    replicate-ignore-db = mysql
    replicate-ignore-db = sys 
    replicate-ignore-db = performance_schema
    replicate-ignore-db = information_schema
 
    gtid_mode=on
    enforce_gtid_consistency=on
 
    [mysqld_safe]
    log-error=/opt/mysql/log/error.log
    pid-file=/opt/mysql/data/mysql.pid
 
    !includedir /etc/my.cnf.d

mysql> grant replication slave on *.* to 'zabbixB'@'192.168.164.152' identified by 'zabbixB';    #给master同步backup创建用户
mysql> flush privileges;

~]# service mysqld restart

实现同步:

mysql> change master to                        #master连接backup
            master_host='192.168.164.153',
            master_port=3306,
            master_auto_position=1;
mysql> start slave user='zabbixB' password='zabbixB';    #启动同步  

mysql> show slave status\G;  	#看到IO与SQL线程起来就OK                              
	   ...
	   Slave_IO_Running: Yes
       Slave_SQL_Running: Yes
	   ...
mysql> show processlist;

mysql> change master to                        #backup连接master
       master_host='192.168.164.152',
       master_port='3306',
       master_auto_position=1;
 
mysql> start slave user='zabbixM' password='zabbixM';    #开启同步

2.实现MySQL故障转移

Master:192.168.164.152

~]# yum install -y keepalived
~]# cat /etc/keepalived/keepalived.conf                     #给mysql做高可用
      ! Configuration File for keepalived
 
      global_defs {
         router_id mnode1
      }
 
      vrrp_script check_mysql {
          script "/etc/keepalived/check_mysql.sh"
          interval 2
          weight -15
          fall 2
          rise 2
      }
 
      vrrp_instance mysql {
          state MASTER
          interface ens32
          virtual_router_id 66
          priority 100
          advert_int 1
          authentication {
              auth_type PASS
              auth_pass mysql.com
          }
 
          virtual_ipaddress {
              192.168.164.200
          }
 
          track_script {
              check_mysql
          }
      } 

~]# cat /etc/keepalived/check_mysql.sh              #mysql检测脚本
      #!/bin/bash
      # check mysql is alived
      # author by chuan
 
      STATUS_1=`ps -C mysqld --no-header | wc -l`
 
      if [ $STATUS_1 -eq 0 ];then
          service mysqld start
          sleep 10
 
          STATUS_2=`ps -C mysqld --no-header | wc -l`
          if [ $STATUS_2 -eq 0 ];then
 
                exit 1
          fi
      fi
     
~]# systemctl restart keepalived

Backup:192.168.164.153

~]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
         
    global_defs {
       router_id mnode2
    }
         
    vrrp_script check_mysql {
        script "/etc/keepalived/check_mysql.sh"
        interval 2
        weight -15
        fall 2
        rise 2
    }
         
    vrrp_instance mysql {
        state BACKUP
        interface ens32
        virtual_router_id 66
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass mysql.com
        }
         
        virtual_ipaddress {
            192.168.164.200
        }
         
        track_script {
            check_mysql
        }
    }

~]# systemctl restart keepalived

至此,我们这套环境就已经搭建成功了!!

你可能感兴趣的:(Zabbix)