Prep Instructions:
master:192.168.10.100
slave:192.168.10.101
1. Configure GTID parameters
The configuration files are all /etc/my.cnf
Master parameter configuration:
gtid-mode = ON enforce-gtid-consistency = ON server-id =100 binlog_format = ROW log-bin = /data/mysql/data/mysql-bin
Parameter configuration on Slave:
gtid-mode = ON enforce-gtid-consistency = ON server-id =200 binlog_format = ROW log-bin = /data/mysql/mysql3306/logs/mysql-bin log_slave_updates = ON skip-slave-start = 1
Second, configure the synchronization account:
mysql> grant replication slave on *.* to 'repl'@'192.168.10.%' identified by '123456'; mysql> flush privileges;
192.168.10.% is the host IP for synchronization. It is recommended to use % at the end, so that slaves can be directly synchronized in the intranet in the future.
3. Back up the main database data
mysqldump -uroot -p --master-data=2 --single-transaction -R --triggers --events -A > master.sql
For new libraries, this operation can be omitted, and the synchronization can be configured directly.
Description of backup parameters:
-h, --host= name The host of the target database to be exported, the default is localhost -u, --user= name The database user name of the link target database -p, --password[= name] The database password of the link target database -P, --port= # port to link to the target database --add-drop-database Add drop database command before every create database command when using --databases or --all- databases parameter --add -drop- table Add drop before every create table command The table command --default-character-set= name specifies the default character set, which is UTF8 by default -- replace Use this command to insert data instead of using the insert command --set- charset Write the set names default_character_set command to the export backup file , the default is the open state --dump-slave[=#] means to export the backup from the replicated slave slave library, and it contains the change master pass statement. If the value parameter is not written or =- 1, the change master to statement will be written to the dump file. If set to 2, it will also be written to the dump file, but it will be commented out --master-data[=#] means Export the backup from the replicated master. The value parameter is the same as --dump-slave. Using this parameter will automatically open the lock-all-table parameter, unless the --single- transaction parameter - T, --tab= name is used at the same time to generate the backup file as a text file, and specify the storage file path, each table Two files will be generated, one is the .sql file to save the table structure, the other is the .txt file to save the table data information -A, --all-databases export all tables in all databases -B, -- databases export one or more specified databases --ignore-table= name means to ignore the export of a specified table during the export process, if multiple tables are to be ignored, this parameter Use multiple times -d, --no- data means to export only the table structure -R, -- routines means to also export stored procedures and functions when exporting -- triggers means to export triggers when exporting - w , --where= name means exporting eligible data -x, --lock-all- table means adding a read-only lock to each table in each database during the export process --no-autocommit means adding a read-only lock to each table The data export content is wrapped with two statements set autocommit= 0 and commit --single-transaction means to set the transaction isolation level to repeatable read and start a new transaction when the export starts to execute start transaction, and it will not be executed during the dump execution process. Block any read and write operations
Fourth, configure synchronization from the library
1. Import the data backed up by the Master
mysql -uroot -p < master.sql
2. Configuration synchronization
mysql> CHANGE MASTER TO MASTER_HOST='192.168.10.100', MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION=1;
3. Turn on synchronization and check the synchronization status
mysql> start slave; mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.100 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 918 Relay_Log_File: Client-relay-bin.000004 Relay_Log_Pos: 454 Relay_Master_Log_File: mysql-bin.000001 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: 918 Relay_Log_Space: 962 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: 1003306 Master_UUID: b3f31135-4851-11e8-b758-000c29148b03 Master_Info_File: /data/mysql/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: b3f31135-4851-11e8-b758-000c29148b03:1-4 Executed_Gtid_Set: 25d36cbf-485a-11e8-a621-000c292c6f36:1-3, b3f31135-4851-11e8-b758-000c29148b03:1-4 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
Parameter Description:
Retrieved_Gtid_Set: b3f31135- 4851 -11e8-b758-000c29148b03: 1 - 4 # acknowledged receipt of the transaction number Executed_Gtid_Set: 25d36cbf -485a-11E8-a621-000c292c6f36: 1 - 3 , b3f31135 - 4851 -11e8-b758-000c29148b03: 1 - 4 #represents the number of transactions executed
When a master-slave failure occurs, you can observe which transaction is specifically stuck from here.
5. Introduction to GTID parameters
GTID has the following parameter configurations:
mysql> show variables like '%GTID%'; +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ 8 rows in set (0.01 sec)
Relevant parameter description:
gtid_mode: - ON: generate GTID, slave only accepts transactions with GTID - ON_PERMISSIVE: generate GTID, slave accepts transactions without GTID and transactions with GTID - OFF: does not generate GTID, slave only accepts transactions without GTID - OFF_PERMISSIVE: Does not generate GTID, slave accepts transactions without GTID and transactions with GTID enforce -gtid- consistency - ON: When a statement/ transaction is found to not support GTID, return an error message - WARN: When a statement/ transaction is found to be unsupported, return a warning and log a warning message - OFF: Do not check for GTID Unsupported statement/ transaction
gtid_executed_compression_period #This parameter is the control table compression rate
gtid_next:automatic #gtid is the way to obtain the next transaction, automatic is automatic acquisition, when a slave failure occurs and a transaction needs to be skipped, the ID of the transaction can be specified here
gtid_owned: #Represents the gtid of the transaction being executed and the corresponding thread ID
gtid_purged: #The transaction of binlog that has been deleted, it is a subset of GTID_EXECUTED, starting from MySQL 5.6.9, this variable cannot be set
session_track_gtids: