linux mysql MM (double master) and build keepalived

First, the standby machine IP and VIP plan:
Master1 10.1.1.14 10.1.1.16 VIP
Master2 VIP 10.1.1.15 10.1.1.16

二、mysql MM配置
1.修改master1的my.cnf
# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql/
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/mysqld.log
port = 3306
socket=/usr/local/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid

expire-logs-days=10

#binlog-do-db=db1
#binlog-ignore-db=db2

server-id = 1
log-bin = binlog
relay_log = relay-bin
log_slave_updates =1
auto_increment_increment=2
auto_increment_offset=1

2.修改master2的my.cnf
# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql/
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/mysqld.log
port = 3306
socket=/usr/local/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid

expire-logs-days=10

#binlog-do-db=db1
#binlog-ignore-db=db2

server-id=2
relay_log=relay-bin
log_bin =binlog
log_slave_updates =1
auto_increment_increment=2
auto_increment_offset=2


3.创建master1复制账号
  grant replication slave,replication client on *.* to 'repl'@'10.1.1.15' identified by 'repl';
  
4.创建master2复制账号
  grant replication slave,replication client on *.* to 'repl'@'10.1.1.14' identified by 'repl';
  
5.为master1配置master
  show master status;
  change master to master_host='10.1.1.15',master_user='repl',master_password='repl',master_log_file='binlog.000005',master_log_pos=154;


6.为master2配置master
  show master status;
  change master to master_host='10.1.1.14',master_user='repl',master_password='repl',master_log_file='binlog.000001',master_log_pos=154;
  
7.启动slave
  master1:
  start slave;
  master2:
  start slave;

Three, keepalived Configuration
1. Edit master1 of keepalived profile
  #vi /etc/keepalived/keepalived.conf

! The Configuration File for keepalived
global_defs {
    # configure alarm notification mailbox, you can configure multiple
   notification_email {
    root @ localhost
   }
   # configure mail destination address
   notification_email_from [email protected]
   # Configure smtp server address, it must exist
   smtp_server 10.1.1.11
   # Configure smtp server connection timeout
   smtp_connect_timeout 30
   # Keepalived instance identifier is provided to run, which will be displayed in the message header
   the router_id of the mysql_ha
}
# monitor script
vrrp_script chk_mysql {
    script "/etc/keepalived/check_mysql.sh"
    interval the 2
    weight 2
}
# configure VRRP example, instance naming any
vrrp_instance MySQL-HA {
    # Keepalived role configuration, the mASTER to the host bACKUP preparing machine, where two are set to bACKUP
    the BACKUP State 
    # keepalived configured to monitor a network interface
    interface eth0
    # virtual routing identifier, which is a (1-255) numbers, a master VRRP example of the ID must be the same
    virtual_router_id 66
    # server priority, the larger the number the more priority high, one example primary server and secondary servers take precedence over
    priority 50  
    times disposed between the primary and secondary servers # synchronization check interval (in seconds)
    advert_int. 1
    # preemption mode configuration server, the configuration where the non-preemptive mode (only for Master2 ( preparation machine) to configure)
    #nopreempt
    # configure password authentication type and
    authentication {
        # Two types of authentication the PASS {|} the HA
        AUTH_TYPE the PASS
        # authentication password specified, one example of the primary and the password to the server as
        AUTH_PASS CentOS
    }
    track_script {
    # designated to perform monitoring service
        chk_mysql
    }
    # configure virtual IP, you can specify multiple, line each
    virtual_ipaddress {
    10.1.1.16
    }
}

2. Edit master1 heartbeat script:
#vi /etc/keepalived/check_mysql.sh 

#!/bin/bash
#This scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "3311"|wc -l)
if [ "${counter}" -eq 0 ]; then
    service keepalived stop
    killall keepalived
fi
ping 10.1.1.14 -w1 -c1 &>/dev/null
if [ $? -ne 0 ]
then
    systemctl stop keepalived
    killall keepalived
fi

3. Edit master2 of keepalived configuration file
# vim /etc/keepalived/keepalived.conf 

! The Configuration File for keepalived
global_defs {
   # configure alarm notification mailbox, you can configure multiple
   notification_email {
    root @ localhost
   }
   # configure mail destination address
   notification_email_from [email protected]
   # Configure smtp server address, it must exist
   smtp_server 10.1.1.11
   # Configure smtp server connection timeout
   smtp_connect_timeout 30
   # Keepalived instance identifier is provided to run, which will be displayed in the message header
   the router_id of the mysql_ha
}
# MCS present
vrrp_script chk_mysql {
    Script "/etc/keepalived/check_mysql.sh"
    interval the 2
    weight 2
}
# Configuring VRRP instance, the instance naming any
vrrp_instance MySQL-HA {
    # Keepalived role configuration, the mASTER to the host bACKUP preparing machine, where two are set to bACKUP
    the BACKUP State
    # keepalived configured to monitor a network interface
    interface eth0
    # virtual routing identifier, which is a (1-255) numbers, a master VRRP example of the ID must be the same
    virtual_router_id 66
    # server priority, the larger the number the more priority high, one example primary server and secondary servers take precedence over
    priority 49
    times disposed between the primary and secondary servers # synchronization check interval (in seconds)
    advert_int. 1
    # preemption mode configuration server, the configuration where the non-preemptive mode (only for Master2 ( preparation machine) to configure)
    nopreempt
    # configure password authentication type and
    authentication {
        # Two types of authentication the PASS {|} the HA
        AUTH_TYPE the PASS
        # authentication password specified, one example of the primary and the password to the server as
        AUTH_PASS CentOS
    }
    track_script {
    # specify execution monitoring service
        chk_mysql  
    }
    # configure virtual IP, you can specify multiple, line each
    virtual_ipaddress {
    10.1.1.16
    }
}

4. Edit master2 detection script
# vim /etc/keepalived/check_mysql.sh 

#!/bin/bash
#This scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    systemctl stop keepalived
    killall keepalived
fi
ping 10.1.1.15 -w1 -c1 &>/dev/null
if [ $? -ne 0 ]
then
    service keepalived stop
    killall keepalived
fi

5.vip drift detection
1) while turning keepalived and mysql on master1 and Master2
  #service keepalived Start
  #service mysqld Start
2) View on master1 ip address
  ip addr
3) mysql on the login 10.1.1.16
  mysql -Uusername -Ppassword -h10. -P3311 1.1.16
4) stopped the mysql service on master1
  service mysqld STOP
5) observed ip address on master1 and Master2
  ip addr
6) continued 3) in the session to run the mysql command, and see what happens
  mysql> use information_schema;
  

 

Published 171 original articles · won praise 46 · views 190 000 +

Guess you like

Origin blog.csdn.net/LHDZ_BJ/article/details/95489986