Mysql之主从异步
数据库创建完后主从数据库数据保持同步
- 主数据库
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000002 | 4062 | | | 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-17 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> show global variables like '%gtid%';
+----------------------------------+-------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-17 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-------------------------------------------+
8 rows in set (0.00 sec)
# mysqlbinlog mysql-bin.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#240320 12:50:13 server id 3225616916 end_log_pos 123 CRC32 0x3eeed942 Start: binlog v 4, server v 5.7.39-log created 240320 12:50:13 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
BWv6ZQ8UAkPAdwAAAHsAAAABAAQANS43LjM5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAFa/plEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AULZ7j4=
'/*!*/;
# at 123
#240320 12:50:13 server id 3225616916 end_log_pos 154 CRC32 0xaa49709d Previous-GTIDs
# [empty]
# at 154
#240320 12:50:15 server id 3225616916 end_log_pos 219 CRC32 0xcc611e3e GTID last_committed=0 sequence_number=1 rbr_only=no
SET @@SESSION.GTID_NEXT= '53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1'/*!*/;
...
...
...
# at 3200
#240320 12:50:15 server id 3225616916 end_log_pos 3434 CRC32 0xeb895228 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1710910215/*!*/;
GRANT SELECT, INSERT, CREATE, RELOAD, PROCESS, SUPER, LOCK TABLES, REPLICATION SLAVE, REPLICATION CLIENT, CREATE TABLESPACE ON *.* TO 'bkpuser'@'%'
/*!*/;
# at 3434
#240320 12:50:15 server id 3225616916 end_log_pos 3499 CRC32 0x8ef83223 GTID last_committed=14 sequence_number=15 rbr_only=no
SET @@SESSION.GTID_NEXT= '53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:15'/*!*/;
# at 3499
#240320 12:50:15 server id 3225616916 end_log_pos 3689 CRC32 0xdca92acb Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1710910215/*!*/;
CREATE USER 'proxy_sql_user'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*31C6AAAA2E813546EF9E161596C267B66629F00E'
/*!*/;
# at 3689
#240320 12:50:15 server id 3225616916 end_log_pos 3754 CRC32 0xb97d3937 GTID last_committed=15 sequence_number=16 rbr_only=no
SET @@SESSION.GTID_NEXT= '53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:16'/*!*/;
# at 3754
#240320 12:50:15 server id 3225616916 end_log_pos 3910 CRC32 0x7280f9cc Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1710910215/*!*/;
GRANT ALL PRIVILEGES ON *.* TO 'proxy_sql_user'@'%' WITH GRANT OPTION
/*!*/;
# at 3910
#240320 12:50:15 server id 3225616916 end_log_pos 3975 CRC32 0xf79946ef GTID last_committed=16 sequence_number=17 rbr_only=no
SET @@SESSION.GTID_NEXT= '53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:17'/*!*/;
# at 3975
#240320 12:50:15 server id 3225616916 end_log_pos 4062 CRC32 0x15c4ea73 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1710910215/*!*/;
SET @@session.time_zone='SYSTEM'/*!*/;
flush privileges
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
- 从数据库
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.67.2.20
Master_User: cdbsync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 4062
Relay_Log_File: mysqlseca208g-mysql-master-2-11c17-0-relay-bin.000002
Relay_Log_Pos: 4275
Relay_Master_Log_File: mysql-bin.000002
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: 4062
Relay_Log_Space: 4513
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: 3225616916
Master_UUID: 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8
Master_Info_File: mysql.slave_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: 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-17
Executed_Gtid_Set: 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-17
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> show global variables like '%gtid%';
+----------------------------------+-------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-17 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-------------------------------------------+
8 rows in set (0.00 sec)
参数 | 主库 | 从库 |
---|---|---|
Executed_Gtid_Set | 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-17 | 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-17 |
binlog文件名称 | mysql-bin.000002 | mysql-bin.000002( Relay_Master_Log_File) |
Position | 4062 | 4062(Read_Master_Log_Pos) |
Exec_Master_Log_Pos | 无 | 4062 |
主库创建一个TEST库,查看从库同步状态
- 主库
mysql> CREATE DATABASE TEST;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| TEST |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000002 | 4221 | | | 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-18 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> show global variables like '%gtid%';
+----------------------------------+-------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-18 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-------------------------------------------+
8 rows in set (0.00 sec)
- 从库
从库的Retrieved_Gtid_Set、Executed_Gtid_Set均是53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-18,和主库一致。
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.67.2.20
Master_User: cdbsync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 4221
Relay_Log_File: mysqlseca208g-mysql-master-2-11c17-0-relay-bin.000002
Relay_Log_Pos: 4434
Relay_Master_Log_File: mysql-bin.000002
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: 4221
Relay_Log_Space: 4672
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: 3225616916
Master_UUID: 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8
Master_Info_File: mysql.slave_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: 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-18
Executed_Gtid_Set: 53e34b1a-e675-11ee-bd7e-fa163e6a9cd8:1-18
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)