Keepalived and MySQL mutual master-slave configuration is automatically switched

To solve the problem of single point Mysql database, MySQL database to achieve two mutual backup, bi-directional replication. When a problem occurs Master, will continue to work to switch to Master Slave.

Environment Description

System version: CentOS Linux release 7.6.1810 (Core)

MySQL version: mysql Ver 14.14 Distrib 5.7.27

keepalived版本:Keepalived v1.2.13

No. IP server use

1     192.168.158.10    Master
2     192.168.158.20    Slave
3     192.168.158.30    VIP

A, MySQL mutual master-slave configuration

1.> Two same version MySQL database.
2.> NTP clock synchronization standby machine
3> Duplex trust authentication densely arranged ssh Free
4> configuration database (Master and Slave Configuration Configuration server-id is not consistent , the other can be the same)
4.1> modify configuration files on the master host MySQL database, and then restart MySQL

#vim / ECT / the my.cnf 
[mysqld] 
log -bin = MySQL- bin 
Server -id = 100 
expire_logs_days = 10 
DATADIR = / var / lib / MySQL 
Socket = / var / lib / MySQL / mysql.sock 
symbolic -links = 0 
log -error = / var / log / mysqld.log 
pid -file = / var / RUN / mysqld / mysqld.pid 
validate_password = OFF # turn off password security policy 
default_password_lifetime = 0      # set password not expire
log_bin=/var/log/mysql/mysql-bin

4.2> modify the configuration file of the Slave MySQL database host, and then restart MySQL

#vim /ect/my.cnf

[mysqld]
log-bin=mysql-bin
server-id=101
expire_logs_days = 10
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password=off           
default_password_lifetime=0     
log_bin=/var/log/mysql/mysql-bin

5.> Start MySQL service

# systemctl start mysqld

6.> relevant state to Master host, for example, as follows

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

7.> Create a account setup and synchronization
7.1>, respectively, in the implementation of Master and Slave library library, create a copy of data synchronization account.

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO replication@'%' IDENTIFIED BY 'replication';
mysql> flush  privileges;

7.2> 7.2> Master on the host, perform synchronization operations (Note master_host each point standby machine parameter), as follows:

mysql> change master to master_host='192.168.158.20',master_port=3306,master_user='replication',master_password='replication',master_log_file='mysql-bin.000001',master_log_pos=154;
mysql> start slave;

7.3> Slave on the host, perform synchronization operations (Note master_host each point standby machine parameter), as follows:

mysql> change master to master_host='192.168.158.10',master_port=3306,master_user='replication',master_password='replication',master_log_file='mysql-bin.000001',master_log_pos=154;
mysql> start slave;

7.4> on Master, Slave host, query synchronization status "show slave status \ G", check the results Slave_IO_Running: Yes and Slave_SQL_Running: Yes, otherwise there is an exception.

8.> Cipher text commands access (two hosts are configured)
when using Mysql database such as mysql or mysqldump related commands, you need to enter a password on the command-line interface, when using a script, enter a password in the script is clearly not safe, so You may be provided Mysql ciphertext file.

# mysql_config_editor set --login-path=local --user=root --port=3306 --password
# mysql_config_editor print --all

9.> Create a toggle script

Switching script planning, as this switch is mysql, mysql therefore create a directory in the directory, all switching scripts into the / home / mysql directory, this time related to the script as follows:

Enter / home / mysql directory, the following documents:

Logs // store the log file directory

mybackup.sh // empty slave configuration, remote log file and retrieve Pos, and turn sync

mycheck.sh // check mysql running, if run properly, quit. If the run does not call normal pkill keepalived

mymaster.sh // first determine whether the synchronous copy execution is completed, and if not completed execution wait 1 minute, stopping the synchronization (stop slave;), and switching logs after recording and pos

.mysqlenv // script file runtime environment

mystop.sh // set parameters to ensure that data is not lost, the final check to see if there is a write operation, quit last minute

After syncposfile // every time you switch, Master File last value and Position values.

10. Environment File

10.1> Master host environment file

[MySQL the root @ localhost] # Vim .mysqlenv 

MYSQL Based = / usr / bin / MySQL 
MYSQL_CMD = " --login local-path = " 
the IP address of the remote host # 
REMOTE_IP = 192.168 . 158.20 
Export MySQL = " $ $ MYSQL Based MYSQL_CMD "

10.2> Slave host environment file

[MySQL the root @ localhost] # Vim .mysqlenv 
MYSQL Based = / usr / bin / MySQL 
MYSQL_CMD = " --login local-path = " 
the IP address of the remote host # 
REMOTE_IP = 192.168 . 158.10 
Export MySQL = " $ $ MYSQL Based MYSQL_CMD "

11.> Check Script Service

11.1> mycheck.sh

[root@localhost mysql]# vim mycheck.sh 
#!/bin/sh

##################################################
#File Name  : mycheck.sh
#Description: mysql is working MYSQL_OK is 1
#             mysql is down MYSQL_OK is 0
##################################################

BASEPATH=/home/mysql
LOGSPATH=$BASEPATH/logs
source $BASEPATH/.mysqlenv

CHECK_TIME=3
MYSQL_OK=1
##################################################################
function check_mysql_helth (){
  $mysql -e "show status;" >/dev/null 2>&1
  if [ $? == 0 ] 
  then 
    MYSQL_OK=1
  else
    MYSQL_OK=0
    #systemctl status keepalived
 fi
 return $MYSQL_OK
}

#check_mysql_helth
while [ $CHECK_TIME -ne 0 ] #不等于
do
    let "CHECK_TIME -= 1"
    check_mysql_helth
    if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    echo "$(date "+%Y-%m-%d %H:%M:%S") The scripts mycheck.sh is running ..." >> $LOGSPATH/mysql_switch.log
    exit 0
    fi
    if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ] #等于
    then
    systemctl stop keepalived
    echo "$(date "+%Y-%m-%d %H:%M:%S") The mycheck.sh, mysql is down, after switch..." >> $LOGSPATH/mysql_switch.log
    exit 1
    fi
    sleep 1  
done
[root@localhost mysql]# 

11.2> switch script

[root@localhost mysql]# vim mymaster.sh 
#!/bin/sh

##################################################
#File Name  : mymaster.sh
#Description: First determine whether synchronous
#             replication is performed, and if no
#             execution is completed, wait for 1
#             minutes. Log logs and POS after
#             switching, and record files synchronously.
##################################################

BASEPATH=/home/mysql
LOGSPATH=$BASEPATH/logs
source $BASEPATH/.mysqlenv

$mysql -e "show slave status\G" > $LOGSPATH/mysqlslave.states
Master_Log_File=`cat $LOGSPATH/mysqlslave.states | grep -w Master_Log_File | awk -F": " '{print $2}'`
Relay_Master_Log_File=`cat $LOGSPATH/mysqlslave.states | grep -w Relay_Master_Log_File | awk -F": " '{print $2}'`
Read_Master_Log_Pos=`cat $LOGSPATH/mysqlslave.states | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}'`
Exec_Master_Log_Pos=`cat $LOGSPATH/mysqlslave.states | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}'`
i=1

while true
do
    if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ];then
        echo "$(date "+%Y-%m-%d %H:%M:%S") The mymaster.sh, slave sync ok... " >> $LOGSPATH/mysql_switch.log
        break
    else
        sleep 1
        if [ $i -gt 60 ];then
            break
        fi
        continue
        let i++
    fi
done

$mysql -e "stop slave;"
$mysql -e "set global innodb_support_xa=0;"
$mysql -e "set global sync_binlog=0;"
$mysql -e "set global innodb_flush_log_at_trx_commit=0;"
$mysql -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'replication'@'%' IDENTIFIED BY 'replication';flush privileges;"
$mysql -e "show master status;" > $LOGSPATH/master_status_$(date "+%y%m%d-%H%M").txt

# sync pos file
/usr/bin/scp $LOGSPATH/master_status_$(date "+%y%m%d-%H%M").txt root@$REMOTE_IP:$BASEPATH/syncposfile/backup_master.status
echo "$(date "+%Y-%m-%d %H:%M:%S") The mymaster.sh, Sync pos file sucess." >> $LOGSPATH/mysql_switch.log
[root@localhost mysql]#

11.3> cut back script

[root@localhost mysql]# vim mybackup.sh 
#!/bin/sh

##################################################
#File Name  : mybackup.sh
#Description: Empty the slave configuration, retrieve
#             the remote log file and Pos, and open
#             the synchronization
##################################################

BASEPATH=/home/mysql
LOGSPATH=$BASEPATH/logs
source $BASEPATH/.mysqlenv

$mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'replication'@'%' IDENTIFIED BY 'replication';flush privileges;"
$mysql -e "set global innodb_support_xa=0;"
$mysql -e "set global sync_binlog=0;"
$mysql -e "set global innodb_flush_log_at_trx_commit=0;"
$mysql -e "flush logs;"
$mysql -e "reset slave all;"

if [ -f $BASEPATH/syncposfile/backup_master.status ];then
        New_ReM_File=`cat $BASEPATH/syncposfile/backup_master.status | grep -v File |awk '{print $1}'`
        New_ReM_Position=`cat $BASEPATH/syncposfile/backup_master.status | grep -v File |awk '{print $2}'`
        echo "$(date "+%Y-%m-%d %H:%M:%S") This mybackup.sh, New_ReM_File:$New_ReM_File,New_ReM_Position:$New_ReM_Position" >> $LOGSPATH/mysql_switch.log
        $mysql -e "change master to master_host='$REMOTE_IP',master_port=3306,master_user='replication',master_password='replication',master_log_file='$New_ReM_File',master_log_pos=$New_ReM_Position;"
        $mysql -e "start slave;"
        $mysql -e "show slave status\G;" > $LOGSPATH/slave_status_$(date "+%y%m%d-%H%M").txt
        cat $LOGSPATH/slave_status_$(date "+%y%m%d-%H%M").txt >> $LOGSPATH/mysql_switch.log
        rm -f $BASEPATH/syncposfile/backup_master.status
else
    echo "$(date "+%Y-%m-%d %H:%M:%S") The scripts mybackup.sh running error..." >> $LOGSPATH/mysql_switch.log
fi
[root@localhost mysql]# 

11.4> Stop Script

[root@localhost mysql]# vim mystop.sh 
#!/bin/sh

##################################################
#File Name  : mystop.sh
#Description: Set parameters to ensure that the data
#             is not lost, and finally check to see
#             if there are still write operations,
#             the last 1 minutes to exit

##################################################

BASEPATH=/home/mysql
LOGSPATH=$BASEPATH/logs
source $BASEPATH/.mysqlenv

$mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'replication'@'%' IDENTIFIED BY 'replication';flush privileges;"
$mysql -e "set global innodb_support_xa=1;"
$mysql -e "set global sync_binlog=1;"
$mysql -e "set global innodb_flush_log_at_trx_commit=1;"
$mysql -e "show master status\G" > $LOGSPATH/mysqlmaster0.states
M_File1=`cat $LOGSPATH/mysqlmaster0.states | awk -F': ' '/File/{print $2}'`
M_Position1=`cat $LOGSPATH/mysqlmaster0.states | awk -F': ' '/Position/{print $2}'`
sleep 2
$mysql -e "show master status\G" > $LOGSPATH/mysqlmaster1.states
M_File2=`cat $LOGSPATH/mysqlmaster1.states | awk -F': ' '/File/{print $2}'`
M_Position2=`cat $LOGSPATH/mysqlmaster1.states | awk -F': ' '/Position/{print $2}'`

i=1

while true
do
    if [ $M_File1 = $M_File2 ] && [ $M_Position1 -eq $M_Position2 ];then
        echo "$(date "+%Y-%m-%d %H:%M:%S") The mystop.sh, master sync ok.." >> $LOGSPATH/mysql_switch.log
        exit 0
    else
        sleep 1
        if [$i -gt 60 ];then
            break
        fi
        continue
        let i++
    fi
done
echo "$(date "+%Y-%m-%d %H:%M:%S") The mystop.sh, master sync exceed one minutes..." >> $LOGSPATH/mysql_switch.log
[root@localhost mysql]#

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

# mysql_config_editor set --login-path=local --user=root --port=3306 --password# mysql_config_editor print --all

Guess you like

Origin www.cnblogs.com/saneri/p/11423059.html