Keepalived achieved using dual master MySQL High Availability

MySQL dual-master configuration

Preparing the environment:

First, install the MySQL database.

Mysql installed on the master and backup, after the installation starts automatically, mysql root password is 123456

Second, modify the MySQL configuration file:

1.master end profile as follows:

1.master end profile as follows:

# Vim /etc/my.cnf # Add
server_id = 2 is set to the #backup. 1
log-bin = / Data / MySQL / MySQL-bin
log-bin-index = / Data / MySQL / My-bin.index
the binlog-the ignore -db = mysql, information_schema # binlog-written logs ignore
auto-increment-increment = 2 # incremental change field
auto-increment-offset = 1 # initial field ID. 1
Slave-All Skip-# = ignores all errors copy the generated error

# systemctl restart mysqld

2. backup end profile as follows:

master-side and backup-side configuration only server_id not the same, other are the same.

Third, create user data synchronization and review the logs and log bin pos position:

1.> Create mysql synchronization on the master account and view the log and the log bin pos position:

# mysql -uroot -p123456

mysql> GRANT  REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.%' IDENTIFIED  BY 'repl';

mysql> flush  privileges;

mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB        | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000001 |      618 |              | mysql,information_schema |                  |
+------------------+----------+--------------+--------------------------+-------------------+

master configuration is as follows:

# mysql -uroot -p123456

mysql> change master to
    -> master_host='192.168.1.20',                #这里填backup的IP
    -> master_user='repl',
    -> master_password='repl',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=618;

mysql> start slave;

2.> Create mysql synchronization account on the backup configuration is as follows:

# mysql -uroot -p123456

mysql> GRANT  REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.%' IDENTIFIED  BY 'repl';

mysql> flush  privileges;

mysql> change master to
    -> master_host='192.168.1.10',                #这里填master的IP
    -> master_user='repl',
    -> master_password='repl',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=618;

mysql> start slave;
---------------------

View synchronization status were:

master View:

mysql> show slave status\G;
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.20
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1082
              Relay_Log_File: test2-relay-bin.000002
                Relay_Log_Pos: 784
        Relay_Master_Log_File: mysql-bin.000001
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
---------------------

backup View:

mysql> show slave status\G;
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.10
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 618
              Relay_Log_File: test3-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
---------------------

Slave_IO and Slave_SQL is YES Description The Lord synchronization is successful.

 4, MySQL of the main primary synchronization test

master data insertion test:

mysql> create database testdb;

mysql> use testdb;

mysql> create table user (number INT(10),name VARCHAR(255));

mysql> insert into user values(01,'testid');

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user          |
+----------------+
---------------------

backup on View:

mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb              |
+--------------------+

mysql> use testdb;

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user          |
+----------------+

mysql> select number,name from user;
+--------+------+
| number | name |
+--------+------+
|      1 | testid  |
+--------+------+
---------------------

You can see the past has been successfully synchronized, the same is inserted into the user table in the backup data, sync, like in the past, dual-master configuration no problem.

5, configuration to achieve hot standby keepalived

1.master keepalived installation and configuration:

# yum install -y keepalived

# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
  notification_email {
    [email protected]
  }
  notification_email_from [email protected]
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id MYSQL_HA
}

vrrp_instance VI_1 {
    State the BACKUP
    interface eth0 # according to the actual network interface changes
    virtual_router_id 51 is
    priority 100 # priority, Master set 100
    advert_int. 1
    nopreempt # does not preempt resources, provided only on the Master
    authentication {
        AUTH_TYPE the PASS
        AUTH_PASS 1111
    }
    virtual_ipaddress {
        192.168 .1.30
    }
}

virtual_server 192.168.1.30 3306 {
    delay_loop 2
    #lb_algo rr
    #lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.10 3306 {# detect local mysql
        weight. 3
        notify_down /tmp/mysql.sh # mysql service when Down, execute this script, kill switch keepalived achieve
        TCP_CHECK {
            connect_timeout The. 3
            nb_get_retry. 3
            delay_before_retry. 3
        }
    }
}

keepalived backup installation and configuration:

# yum install -y keepalived

# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
  notification_email {
    [email protected]
  }
  notification_email_from [email protected]
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id MYSQL_HA
}

{VI_1 vrrp_instance
    State the BACKUP
    interface eth0 # network interface according to the actual changes
    virtual_router_id 51 is
    priority Priority # 90, backup set to 90
    advert_int. 1
    #nopreempt # preempt resources
    authentication {
        AUTH_TYPE the PASS
        AUTH_PASS 1111
    } 
    virtual_ipaddress {
        192.168.1.30
    } 

{192.168.1.30 3306 virtual_server
    delay_loop 2
    #lb_algo rr
    #lb_kind NAT
    persistence_timeout 50
    Protocol TCP
   
    real_server 192.168.1.20 # 3306 {detect local mysql
        weight 3
        notify_down /tmp/mysql.sh # when mysql service down when this script is executed, killed keepalived achieve switching
        TCP_CHECK {
            connect_timeout The. 3
            nb_get_retry. 3
            delay_before_retry. 3
        } 
    } 
}

master and backup on editing mysql.sh

# vim /tmp/mysql.sh

#!/bin/bash
pkill keepalived

# chmod +x !$
# systemctl start keepalived

Two mysql server license allows remote root login:

# mysql -uroot -p123456789

mysql> grant all on *.* to 'root'@'192.168.1.%' identified by '123456';

mysql> flush privileges;

Test availability
via VIP by mysql client to see if the connection is successful.
Here I use another machine with the network, the connection test:

# mysql -h192.168.1.30 -uroot -p123456

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from test.user;
+--------+------+
| number | name |
+--------+------+
|      1 | testid  |
+--------+------+
1 row in set (0.01 sec)
---------------------

We can see, the connection is successful, and query the data is no problem, stop the master mysql service, whether to switch to the normal backup, you can use ip addr command to view the server where the VIP.

Check the master if there is VIP, you can be seen on the master VIP

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
    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 group default qlen 1000
    link/ether 00:0c:29:cf:ab:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
    inet 192.168.1.30/32 scope global eth0
      valid_lft forever preferred_lft forever
    inet6 fe80::fe8e:3c2f:4d32:e9fd/64 scope link noprefixroute
      valid_lft forever preferred_lft forever
---------------------

Stopped on the master mysql service:

# systemctl stop mysqld

# ps axu |grep keepalived
root      11074  0.0  0.0 112708  988 pts/1    S+  15:28  0:00 grep --color=autokeepalived

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
    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 group default qlen 1000
    link/ether 00:0c:29:cf:ab:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
    inet6 fe80::fe8e:3c2f:4d32:e9fd/64 scope link noprefixroute
      valid_lft forever preferred_lft forever
---------------------

You can see, keepalived also stopped after the mysql service stopped, VIP is not on the master.

backup to see if there is a VIP, you can see on the backup VIP.

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
    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 group default qlen 1000
    link/ether 00:0c:29:33:80:d5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.20/24 brd 192.168.1.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
    inet 192.168.1.30/32 scope global eth0
      valid_lft forever preferred_lft forever
    inet6 fe80::4b20:2e16:a957:f9a1/64 scope link noprefixroute
      valid_lft forever preferred_lft forever
---------------------

View / var / log / messages logs, standby switching process can be seen:

Apr  8 15:27:16 hosts systemd: Stopping MySQL Server...
Apr  8 15:27:16 hosts Keepalived_healthcheckers[11048]: TCP connection to [192.168.1.10]:3306 failed.
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: TCP connection to [192.168.1.10]:3306 failed.
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: Check on service [192.168.1.10]:3306 failed after 1 retry.
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: Removing service [192.168.1.10]:3306 from VS [192.168.1.30]:3306
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: IPVS (cmd 1160, errno 2): No such destination
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: Executing [/tmp/mysql.sh] for service [192.168.1.10]:3306 in VS [192.168.1.30]:3306
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: Lost quorum 1-0=1 > 0 for VS [192.168.1.30]:3306
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: Remote SMTP server [127.0.0.1]:25 connected.
Apr  8 15:27:19 hosts Keepalived_vrrp[11049]: VRRP_Instance(VI_1) sent 0 priority
Apr  8 15:27:19 hosts Keepalived_vrrp[11049]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr  8 15:27:19 hosts Keepalived[11047]: Stopping
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: IPVS (cmd 1156, errno 2): No such file or directory
Apr  8 15:27:19 hosts Keepalived_healthcheckers[11048]: Stopped
Apr  8 15:27:20 hosts Keepalived_vrrp[11049]: Stopped
Apr  8 15:27:20 hosts Keepalived[11047]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Apr  8 15:27:27 hosts systemd: Stopped MySQL Server.
---------------------

Restore master server failure, to see if preempt resources, the active server.

Start mysql service and keepalived service on master:

# systemctl start mysqld

# systemctl start keepalived

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
    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 group default qlen 1000
    link/ether 00:0c:29:cf:ab:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
    inet6 fe80::fe8e:3c2f:4d32:e9fd/64 scope link noprefixroute
      valid_lft forever preferred_lft forever
---------------------

You can see that even master failover, nor seize resources, VIP is still on the backup, because the master has been configured previously non-preemptive mode (nopreempt).

But it notes that:

nopreempt this parameter can be used to state the case for the BACKUP, so take the time to configure the master state and the backup are set to BACKUP, this will achieve a non-preemptive mode keepalived!

In other words:

* When the state status as a MASTER, a time for the BACKUP, plus without nopreempt this parameter is the same effect. That is according to priority priority to determine who seize vip resources, it is preemptive mode!

* When the state status is set to BACKUP, if not nopreempt configuration parameters, then the priority is to see the priority to decide who seize vip resources, that is preemption mode.

* When the state status is set to BACKUP, if nopreempt configuration parameters, then it will not be a priority to consider priority, non preemptive mode! That is, only vip currently resides machine fails, another machine to take over the vip.
 Even if the high priority that the machine will not take the initiative to return to normal after the snatch vip, can only wait until the other party fails, vip will be cut back.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160164.htm