mysql master-slave (master-slave-slave) master-slave recovery data is not lost

Environment simulation: master-slave structure, master library failure, slave library upgrade to master library, new master writes data, then master-slave recovery, successfully import data written by new master during main library failure

Environment deployment

  • System version
    CentOS Linux release 7.5.1804 (Core)
  • Kernel version
    3.10.0-862.el7.x86_64
  • Firewall and selinux closed
  • Role assignment
    192.168.153.179 master master database
    192.168.153.178 slave slave database

Start simulation

First, the master-slave environment foundation

slave from:

Insert picture description here

master master:

Simulate the main library failure restart

vim /etc/my.cnf
systemctl restart mariadb

Insert picture description here

slave from:

vim /etc/my.cnf
systemctl restart mariadb

Insert picture description here

Close reset slave

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.01 sec)

Create library and insert data

MariaDB [(none)]> create database ceshi charset utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use ceshi;
Database changed
MariaDB [ceshi]> create table a(id int(4),name char(12));
Query OK, 0 rows affected (0.01 sec)

MariaDB [ceshi]> insert a values(1,'测试');
Query OK, 1 row affected (0.00 sec)

MariaDB [ceshi]> select * from a;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 测试   |
+------+--------+
1 row in set (0.00 sec)

View the starting position before inserting data and the position after inserting data

Insert picture description here

MariaDB [ceshi]> show master status ;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      633 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

Then restore the master and slave and check whether our master library has the data written when the new master took over

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

slave from:

Import the data into the sql file according to the "mysql-bin.000004" log file of our new master before and after inserting the data, and then transfer the file to our master master

Export data to sql file

mysqlbinlog mysql-bin.000004 --start-position=245 --stop-position=6
33 > /root/mysql-bin.sql

Transfer to the master server

Insert picture description here

master master:

Import data into the library

MariaDB [(none)]> source /root/mysql-bin.sql

Imported successfully

MariaDB [ceshi]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ceshi              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [ceshi]> use ceshi;
Database changed
MariaDB [ceshi]> select * from a;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 测试   |
+------+--------+
1 row in set (0.00 sec)

Successfully imported data

Guess you like

Origin blog.csdn.net/qq_49296785/article/details/109326517