For those who want to get started or beginners, intermediate MySQL database operation and maintenance personnel, it is very necessary to understand the impact of mysqldump on the mysql database. After the mysqldump command is executed, what is executed in the mysql background, let's take a look at it. Use general_log here. analysis
1. First open the general_log of the database, as shown below
[root@localhost] 17:30:41 [(none)]>show variables like 'general_log%';
+------------------+---------------------------------+
| Variable_name | Value |
+------------------+---------------------------------+
| general_log | OFF |
| general_log_file | /data/mysql/data/3306/mysql.log |
+------------------+---------------------------------+
2 rows in set (0.00 sec)
Open general_log
[root@localhost] 17:41:40 [(none)]>set global general_log=on;
Query OK, 0 rows affected (0.42 sec)
[root@localhost] 17:41:50 [(none)]>show variables like 'general_log%';
+------------------+---------------------------------+
| Variable_name | Value |
+------------------+---------------------------------+
| general_log | ON |
| general_log_file | /data/mysql/data/3306/mysql.log |
+------------------+---------------------------------+
2 rows in set (0.00 sec)
2. Execute the mysqldump backup command
#mysqldump --socket=/data/mysql/run/3306/mysql.sock --skip-tz-utc --all-databases --single-transaction -ER -u root -proot> db_script.sql
After execution, look at the contents of general_log
2020-08-20T17:44:58.473668+08:00 6 Connect root@localhost on using Socket
2020-08-20T17:44:58.473911+08:00 6 Query /*!40100 SET @@SQL_MODE='' */
2020-08-20T17:44:58.474234+08:00 6 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
2020-08-20T17:44:58.474325+08:00 6 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
2020-08-20T17:44:58.474514+08:00 6 Query SHOW VARIABLES LIKE 'gtid\_mode'
2020-08-20T17:44:58.478784+08:00 6 Query SELECT @@GLOBAL.GTID_EXECUTED
2020-08-20T17:44:58.478972+08:00 6 Query UNLOCK TABLES
The backup command first sets the isolation level of the session to RR, and then starts to set the transaction consistency snapshot. When you see this, some students will ask why FTWRL (flush table with read lock) is not executed in the background because it is not added during the backup – The master-data parameter, then see what is different after adding this parameter
#mysqldump --socket=/data/mysql/run/3306/mysql.sock --master-data=2 --skip-tz-utc --all-databases --single-transaction -ER -u root -proot> db_script.sql
Look again at the content of general_log
2020-08-20T17:52:33.612990+08:00 7 Connect root@localhost on using Socket
2020-08-20T17:52:33.613504+08:00 7 Query /*!40100 SET @@SQL_MODE='' */
2020-08-20T17:52:33.614493+08:00 7 Query FLUSH /*!40101 LOCAL */ TABLES
2020-08-20T17:52:33.617383+08:00 7 Query FLUSH TABLES WITH READ LOCK
2020-08-20T17:52:33.617559+08:00 7 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
2020-08-20T17:52:33.617628+08:00 7 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
2020-08-20T17:52:33.617804+08:00 7 Query SHOW VARIABLES LIKE 'gtid\_mode'
2020-08-20T17:52:33.625696+08:00 7 Query SELECT @@GLOBAL.GTID_EXECUTED
2020-08-20T17:52:33.625876+08:00 7 Query SHOW MASTER STATUS
2020-08-20T17:52:33.626051+08:00 7 Query UNLOCK TABLES
Here you will find that there are 2 more operations
FLUSH /*!40101 LOCAL */ TABLES
FLUSH TABLES WITH READ LOCK
This is because the addition of the –master-data parameter means that the backup is exported from the replicated master database. This backup file can be used to directly create another replication slave database. FLUSH TABLES WITH READ LOCK is mainly used for backup tools to obtain consistent backups (data Match with binlog site).
It is very necessary to understand FLUSH TABLES WITH READ LOCK, because the operation is not good, all DML, DDL, for Update will be blocked, causing the business to be unable to do any operations
FTWRL mainly includes 3 steps:
1. On the global read lock (lock_global_read_lock)
2. Clear the table cache (close_cached_tables)
3. On the global COMMIT lock (make_global_read_lock_block_commit)
In the first step, because it is a global read lock, it will block all DML, DDL, for Update operations, but this step is very fast and the business cannot feel it
In the second step, the table cache is cleaned up. You need to pay attention here. If there is a slow query, FTWRL will be blocked. Moreover, all operations on the involved tables, including select, will be blocked. Blocked, and now even if the mysqldump backup command is terminated, the blockage will not be removed, unless you kill the slow query statement, so students who do backups in the early morning should pay attention to
the third operation, will apply for the global commit lock, if At this time, if there is a big thing, if it is not submitted for a long time, FTWRL will be blocked
In summary, before executing the mysqldump command, first check whether there are slow sql and big things that have not been submitted for a long time in the database.