mysql 5.7 master slave synchronization gtid

Keywords: MySQL Database SQL CentOS

Environment: 1. linux centOS 7 64 bit
2. (slave) linux centOS 7 64 bit
3. (mysql) it's better to have the same version. It's recommended that the slave database be no higher than the master database. 5.7 is recommended

centos 7 silently installs mariadb. For mysql 5.7 installation, please refer to the following link documents:
centos 7 installs mysql 5.7: https://juejin.im/post/5c088b066fb9a049d4419985 (Reprint)
I. main database configuration
1. Change the configuration file of the main database / etc/my.cnf

vim /etc/my.cnf

[mysqld]
lower_case_table_names=1
max_connections=7000
group_concat_max_len = 202400
max_allowed_packet = 128M
#open gtid function
gtid-mode=on
enforce-gtid-consistency=1
#Set up server_id,Generally set to IP,Be unique
server_id=840
Replication filtering: that is, to specify which database is not synchronized( mysql Library is generally not synchronized)
binlog-ignore-db=mysql
#Specify which database to synchronize. Only synchronize this time newerp library
binlog-do-db=newerp
#Enable the binary log function, which can be accessed at will. The best meaningful key is here
log-bin=edu-mysql-bin
For each session Allocated memory used to store the cache of binary logs during transactions
binlog_cache_size=1M
Format of master-slave replication mixed,statement,row,The default format is statement
binlog_format=mixed
Binary log auto delete/Number of days overdue. The default value is 0, which means it will not be deleted automatically.
expire_logs_days=7
Skip all errors encountered in master-slave replication or errors of the specified type to avoid slave End replication interrupted.
For example, 1062 error means that some primary keys are duplicate, and 1032 error is caused by inconsistency between master and slave database data
slave_skip_errors=1062

2. Restart mysql database

systemctl restart mysqld

3. Create synchronous account and view master information

[tomcat@iZ2zeij9pa0qnzjt5wcr4kZ ~]$ msyql -uroot -p
Enter password:
mysql>GRANT REPLICATION SLAVE ON . to ' slave_account '@'%' identified by '123456';
mysql>FLUSH PRIVILEGES;
mysql> SHOW MASTER STATUS\G;
1. row
File: edu-mysql-bin.000031
Position: 1210791
Binlog_Do_DB: newerp
Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 2abeaffc-6158-11e7-8222-00163e03196b:1-16151
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

Note: 'slave account' creates a synchronization user for the main database, '123456' is the user password of the database slave account.
Note: record File: edu-mysql-bin.000031 and Position: 1210791, which will be used later from the library.

4. mysqldump exports new ERP database to slave database

mysqldump -uroot -p123456 -hlocalhost --single-transaction --master-data=2 newerp > /data/newerp_back.sql

Wrote a script for reference

  1. > DB_USER="root"
  2. > DB_PASS="123456!"
  3. > DB_HOST="localhost"
  4. > DB_NAME="newerp"
  5. > BIN_DIR="/usr/bin"
  6. > BCK_DIR="/data"
  7. > DATE=date +%Y-%m-%d_%H-%M-%S
  8. > $BIN_DIR/mysqldump -u$DB_USER -p$DB_PASS -h$DB_HOST --single-transaction --master-data=2 $DB_NAME > $BCK_DIR/$DB_NAME.$DATE.sql

5. Transfer the backup file scp to the slave database

scp -P 22312 /data/newerp_back.sql root@Slave Library IP:/root/
Password:

The configuration of the master database is completed, and the configuration of the slave database is started.

II. Slave configuration
1. Configure from the library / etc/my.cnf

[mysqld]

gtid-mode=on #Turn on gtid, after 5.6

enforce-gtid-consistency=1

server_id=3026 #id is required, not the same as the main library id

replicate-ignore-db=mysql #Indicates that the mysql database is not synchronized. You can write multiple

replicate-do-db=newerp #It means that only the new ERP library can be synchronized, and more than one can be written

2. Restart mysqld service after changing

systemctl restart mysqld

3. Import new ERP back.sql data

[root@localhost ~]# mysql -uroot -p < /root/newerp_back.sql

4. Enter mysql

mysql> change master to master_host='10.175.18.40',master_user='slave_account',master_password='123456',master_log_file='edu-mysql-bin.000031',master_log_pos=1210791;

mysql> start slave; #Start synchronization, stop slave stop synchronization, reset master reset main database information

mysql> `show slave status\G;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 37
Current database: NONE

1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 10.175.18.40
Master_User: slave_account
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: edu-mysql-bin.000032
Read_Master_Log_Pos: 1031964
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 1032185
Relay_Master_Log_File: edu-mysql-bin.000032
Slave_IO_Running: Yes #All two are YES Indicates that synchronization is successfully turned on
Slave_SQL_Running: Yes #YES for both indicates successful synchronization
Replicate_Do_DB: newerp
Replicate_Ignore_DB: mysql
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: 1031964
Relay_Log_Space: 1032606
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: 840
Master_UUID: 2abeaffc-6158-11e7-8222-00163e03196b
Master_Info_File: /var/lib/mysql/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: 2abeaffc-6158-11e7-8222-00163e03196b:10626-16016
Executed_Gtid_Set: 2abeaffc-6158-11e7-8222-00163e03196b:1-16016
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

Note: Master  host  10.175.18.40  is the main library IP, master  user  slave  account  is the main library to create a synchronous user, master  password  123456  is the database  slave  account  user password  123456  is the database user password, master  log  file. Execute the sql command from the main database: SHOW MASTER STATUS;

5, test
Create a new table in the main library or insert new data to see if the slave library is synchronized.

Posted by suavebum on Wed, 13 Nov 2019 10:43:46 -0800