MYSQL Asynchronous Replication

Keywords: MySQL mysqldump mycat

Replication, replication is a highly available foundation, and the underlying layers of middleware such as MHA, mycat all rely on replication principles

Master master master instance slave Slave slave

Classification: default asynchronous replication, semi-synchronous replication after version 5.5, new GTID replication at version 5.6, multi-source replication at version 5.7, parallel replication based on group submission, and enhanced semi-synchronous replication

Replication methods: 1. Traditional methods: binlog-based replication 2.GTID: transaction-based replication

Bilogs can be in different formats: statement-based, row-based, mixed (row data replication is the default)

Set up the regular asynchronous replication below

Requirements: server_id is different between master and slave; the master library opens binlog, and it is recommended that the slave library also opens convenient schema extensions

First edit my.cnf to open binlog and set server_id

mysql> show variables like '%log_bin%';
+---------------------------------+---------------------------------------+
| Variable_name                   | Value                                 |
+---------------------------------+---------------------------------------+
| log_bin                         | ON                                    |
| log_bin_basename                | /usr/local/mysql/data/mysql-bin       |
| log_bin_index                   | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF                                   |
| log_bin_use_v1_row_events       | OFF                                   |
| sql_log_bin                     | ON                                    |
+---------------------------------+---------------------------------------+
6 rows in set (0.00 sec)

mysql> ^DBye
[root@localhost ~]$ cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
log_bin=mysql-bin
server_id=1
[root@localhost ~]$

Then create a master-slave replicated user, which was used when the previous experiment created a scott user (since the password is stored in master.info in slave, you should actually create a separate user with only replication privileges)

mysql> grant replication slave on *.* to 'scott'@'%';
Query OK, 0 rows affected (0.00 sec)

Table Lock Stop Modification

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

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

mysql> drop table tmp;
ERROR 122

Then pass a full backup of the primary library to the standby, either directly packaged in the data directory (not recommended by innodb) or directly mysqldump

[root@localhost ~]$ mysqldump --all-databases -uroot -pmysql --master-data > dbdump.db
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]$ ls
dbdump.db
[root@localhost ~]$ scp dbdump.db 192.0.1.11:~
The authenticity of host '192.0.1.11 (192.0.1.11)' can't be established.
ECDSA key fingerprint is SHA256:pmk8Q9EnT+TugRZ5rb2bc0GP20ZV3LkeuXP/Jrw5tbs.
ECDSA key fingerprint is MD5:13:d1:4a:14:3a:4d:fd:33:56:15:f9:1f:2f:44:87:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.0.1.11' (ECDSA) to the list of known hosts.
root@192.0.1.11's password: 
dbdump.db                                                                                                    100%  775KB  47.6MB/s   00:00

Then apply it in the backup

mysql> source dbdump.db

Configuring primary library information from libraries

mysql> change master to
    -> master_host='192.0.1.10',
    -> master_user='scott',
    -> master_password='tiger',
    -> master_port=3306,
    -> master_log_file='mysql-bin.000003',
    ->  master_log_pos=556;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>

Turn on synchronization

mysql> start slave ;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.0.1.10
                  Master_User: scott
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 556
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 556
              Relay_Log_Space: 531
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 531fa6d1-627f-11e9-8dc7-000c297887a1
             Master_Info_File: /data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql>

Release Main Library Lock

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

Once validated, records are inserted into the main library and immediately available from the library

Posted by Gast on Wed, 14 Aug 2019 22:04:55 -0700