mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

The average small company database, using the master-slave replication to ensure high availability of the database, but once the primary database fails, take some time to switch from the library, thus leading to down time and can not resume business. Dual primary (master) with high availability architecture also keepalived this mysql replication of master-slave principle and built on. This is a simple and convenient solution for high availability in a clustered environment, keepalived use vip, use keepalived own service monitoring and automatically switches to implement custom scripts mysql fault.

1, mysql dual master copy Introduction

Dual master copy is copied from the mutual call the shots, each master is both a master and slave of another server. Thus, any changes made to a party, will apply to the other party by copying the database.
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
Relates to three replicate master thread from a master node to run (log dump thread), the remaining two (I / O thread, SQL thread ) running from the node, as follows:

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
i / o requests binlog thread to the main library, and writes the obtained log binlog relay log (relay log) file;

Primary library will generate a log dump thread, from a library used to binlog i / o threads pass;

SQL thread, reads the relay log log file, and parsed into specific operation to achieve consistent master-slave operation, and consistent final data;

Master-slave replication focus:

1) replication is master-slave sql statement level asynchronous copy logic

2) replication, the master library has a I / O thread, there are two threads from the library, i.e., I / O and SQL thread.

3) implement the necessary conditions from the need to open the main copy of the main library function binlog

4) As the server-id mysql all nodes can have the same replication

5) binlog file sql statement recorded only changes to the database, it does not record any query.

The logic flow diagram substantially as follows:
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

2 keepalived

Keepalived is a server-based vrrp protocol to achieve high availability solution, you can use its IP implementation avoids single points of failure, there is a similar tool heartbeat, corosync. But it will not be alone, but with the LVS, Nginx, HAproxy, collaborative work together to achieve the purpose of high availability

VRRP stands Vritual Router Redundancy Protocol, virtual routing redundancy protocol. By providing several routing function of the device to form a virtual routing equipment, the use of certain mechanisms to ensure high availability of virtual routing, so as to achieve to maintain business continuity and reliability.

In a virtual router this composition, there are points of the master and backup. master is the master node in a virtual router, only one master, but can have a plurality of backup; backup is a backup node, i.e. hang up when the master, backup master node to take over all the resources when there are a plurality of backup when a node, according to the size of the value of its priority (priority), who is selected as a replacement of the master. When the backup node priority value are the same, according to the size of its IP address, to decide.
FIG works
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
substantially workflow is as follows:
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

3 experiment configurations

3.1 basic environment

Experiments on the Community Edition myql8.0
172.31.208.123 A primary Master,
172.31.208.124 standby database B Master
VIP 172.31.208.125
architectural principle is as follows:
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

3.2 database configuration file

If the primary master replication implemented, is arranged on the basis of the master copy from each other mainly

Database primary master A, the configuration file as follows, / etc / my.cnf

server-id = 1                                                #任意自然n,只需要保证两台mysql主机不重复就可以
log-bin=mysql-bin                                        #开启二进制日志
auto_increment_increment=2                      #步进值auto_imcrement 。一般有n台主mysql就填n
auto_increment_offset=1                             #起始值,一般填写第n台主机mysql.此时为第一台主  mysql
#binlog-ignore=mysql                                  #忽略mysql库,可以不填写
#binlog-ignore=infomation_schema            #忽略information_schema库,一般不填写
replicate-do-db=test_db                              #指定同步的数据库,不填写则默认所有的数据库

Database primary master B, the configuration file as follows, / etc / my.cnf

server-id = 2
log-bin=mysql-bin
auto_increment_increment=2
auto_increment_offset=2
#binlog-ignore=mysql
#binlog-ignore=infomation_schema
replicate-do-db=test_db

Once configured, restart mysql

3.3 Main master-slave configuration library masterA

And then begin to create a user copy, repel, repel password
main library master A

create user 'repl'@'172.31.208.124' identified with mysql_native_password by 'mysqlP@ssw0rd';      #创建用户 mysql.0中密码需要填写mysql_native_password
grant replication slave on *.* to 'repl'@'172.31.208.124';                         #分配权限
flush privileges;                                                                                         #刷新权限

And then see whether the account is correct permissions assigned

show grants for 'repl'@'172.31.208.124';

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
View masterA state record binary file name and location

show master status;

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

Landing masterB database, perform a synchronization statement

mysql> change master to
    -> master_host='172.31.208.123',
    -> master_user='repl',
    -> master_password='mysqlP@ssw0rd',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=896;

And then start the slave synchronization process

mysql> start slave;

Then check the slave status

show slave status\G

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
If you do not see the configuration error message in error, the configuration was successful

3.4 Preparation of master-slave configuration library masterB

The basic configuration with the main library masterA way as
to create the user assign permissions repl

mysql> create user 'repl'@'172.31.208.123' identified with mysql_native_password by 'mysqlP@ssw0rd';
mysql> grant replication slave on *.* to 'repl'@'172.31.208.123';
mysql> flush privileges;

View masterB state
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
and then landing masterA, perform a synchronization statement

mysql> change master to 
    -> master_host='172.31.208.124',
    -> master_user='repl',
    -> master_password='mysqlP@ssw0rd',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=866;

And then start the slave synchronization, check the slave status ·

 mysql>start slave;
 mysql>show slave status\G

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
Check the above information, indicating that the configuration is correct

3.5 Test

Creating test_db the main library masterA, and creates a table product

mysql> create table product(
    ->        product_id int(10) not NULL,
    ->        product_name varchar(100) not NULL,
    ->        product_tyep varchar(32) not NULL,
    ->        sale_price int(10) default 0,
    ->        input_price int(10) default 0,
    ->        regist_time date,
    ->        primary key (product_id)
    -> );

masterB on View

mysql> show databases;
mysql> use test_db;
mysql> show tables;
mysql> desc product;

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
See masterA in test_db library table product has been synchronized over

Then create a table product_new test_db library on library equipment masterB, then masterA on View

mysql> create table product_new(
    -> product_id int(10) not NULL,
    ->        product_name varchar(100) not NULL,
    ->        product_tyep varchar(32) not NULL,
    ->        sale_price int(10) default 0,
    ->        input_price int(10) default 0,
    ->        regist_time date,
    ->        primary key (product_id)
    -> );

masterA on View

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
So far proved successful dual-master configuration

Note
1, the master-master replication configuration file auto_increment_increment and auto_increment_offset primary key can only guarantee non-repetition, but can not guarantee the primary key order.

2, the configuration is completed Slave_IO_Running, Slave_SQL_Running insufficiency is YES, show slave status \ G error messages and information, can be corrected according to the error.

3, Slave_IO_Running, Slave_SQL_Running failure to YES, most of the problems are leading to incompatibility of data.
4, the two best database software and other hardware specifications must be configured.

Common Error point:

1, there are two database db database, and the first Taiwan MySQL db has tab1, a second MySQL db no tab1, it certainly can not succeed.

2, has acquired the binary log name and location data, and perform data manipulation, resulting in POS changed. When configuring CHANGE MASTER was used before the POS.

3, stop slave, data changes, then start slave. An error occurred.

The ultimate Dhamma more: re-run again CHANGE MASTER enough.

4, configuration keepalived

Two database, using the yum package installation of keepalived

yum install -y keepalived

Before configuration, make sure the same time on both sides, can be time synchronized with ntp chrony and other tools
at the same time and turn off the firewall configuration to ensure that both sides of the heart
and then confirm that the NIC Multicast support
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
is not turned on, then open as follows

ip link set multicast on dev eth0  

Keepalived after installation, check the version of
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
the configuration file path keepalived is: /etc/keepalived/keepalived.conf, there are a lot of useless configuration, you need to clean up, be reconfigured according to our need to
complete the modification emperor masterA configuration file as follows:

! Configuration File for keepalived

vrrp_script check_mysql_status {                                #定义vrrp脚本,检测mysql主从状态
script "keepalived_check_mysql.sh"                           #脚本名称

interval 10                                                                   #脚本检测执行间隔时间 10秒
}

vrrp_instance VI_1 {                                                  #实例名字V1_1,相同的实例备节点名字要和这个相同
    nopreempt                                                             #采取非抢占模式
    state BACKUP                                                    # 状态为BACKUP状态,避免发生裂脑、冲突现象
    interface ens192                                                  #定义通信接口为ens192,此参数备节点和主节点相同,根据网卡实际名称来
    virtual_router_id 51                                              #实例ID为51
    priority 100                                                          #优先级为90,备节点的优先级必须比此数字低
    advert_int 5                                                           #通信检查检查间隔时间为1秒
    authentication {
        auth_type PASS                                                #认证类型,此参数备节点设置和主节点设置相同
        auth_pass 1111                                                  #密码是1111,此参数备节点设置和主节点相同
    }
    virtual_ipaddress {                                                 #虚拟机IP,即VIP为172.31.208.95/24,绑定接口为ens192,别名为ens192:1,此参数备节点设置和主节点相同
        172.31.208.124/24 dev ens192 label ens192:1
    }
    track_script {
    check_mysql_status                                              #触发检查
    }
}

The following is masterB profile

script "keepalived_check_mysql.sh"

interval 10
}

vrrp_instance VI_1 {
    nopreempt
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 100
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.31.208.125/24 dev ens192 label ens192:1
    }
    track_script {
    check_mysql_status
    }
}

Description:

1, the overall configuration and other keepalived same configuration
2, using a dual primary and a backup state to be arranged, and a non-preemptive mode, priority is determined by the main library who avoid cracking trouble, conflicts

Note that, by default, keepalived software only when the other machine is down or turning off the keepalive will take over the business. Sometimes stop mysql service, but keepalived service is still working, then it will lead to a corresponding service vip user access can not be found, so the need to write scripts, mysql service state detection, when the service interruption, keepalive services are interrupted here keepalived_check_mysql.sh action script is like that. Script below, and stored in the main maserA standby masterB the / etc / keepalived / path

#!/bin/bash
#for centos7
mysqlstr=/usr/sbin/mysqld
user=user
password=ctbtP@ssw0rd

##mysql服务状态正常为1,否则为0
#mysql_status=1

####check mysql status######
$mysqlstr  -u $user -p$password -e "show status;" >/dev/null 2>&1  #执行成功,代表数据库服务正常
echo "mysql_status=1"
exit 0
else
systemctl stop mysqld
fi

Remember to add executable permissions

Once configured, respectively, start masterA and masterB of service keepalived

systemctl start keepalived
systemctl enable keepalvied

Note that when the configuration file has any errors, systemctl not be prompted keepalived failed to start, you need systemclt status checking service
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
can also view the log file to start case

tail -f /var/log/messages

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

When configured correctly there will be the main masterA vip
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived
and masterB not have vip
mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

keepalived总结

mysql learning -mysql8.0 dual-master replication to achieve high availability architecture + keepalived

Guess you like

Origin blog.51cto.com/11555417/2407532