How to safely shut down MySQL

How to safely shut down MySQL

When shutting down the MySQL server, various problems may occur depending on the way it is shut down. The following steps can help reduce the occurrence of problems.

1. Stop copying

Under some special circumstances, the slave node may try to start from the wrong position. To reduce this risk, stop the io thread first so that no new event information is received.

mysql> stop slave io_thread;

After the sql thread has applied all events, the sql thread is also stopped.

mysql> show slave status\G
mysql> stop slave sql_thread;

In this way, the io thread and sql thread can be in a consistent position, the relay log only contains executed events, and the position information in the relay_log_info_repository is also the latest.

For slaves with multithreaded replication enabled, make sure that the gaps are filled before shutting down replication.

mysql> stop slave;
mysql> start slave until sql_after_mts_gaps; #Apply the gap in the relay log
mysql> show slave status\G #Make sure that sql_thread has been stopped before
mysql> stop slave;

2. Commit, rollback, or kill long-running transactions

A lot can happen in 1 minute, and on shutdown, InnoDB must roll back uncommitted transactions. Transaction rollbacks are very expensive and can take a long time. Any transaction rollback could mean data loss, so ideally there are no transactions open when closing.

If the read-write database is closed, write operations should be routed to other nodes in advance. If you must shut down a database that is still receiving transactions, the following query will output information about sessions that have run for more than 60 seconds. Based on this information, decide on the next step:

mysql> SELECT trx_id, trx_started, (NOW() - trx_started) trx_duration_seconds, id processlist_id, user, IF(LEFT(HOST, (LOCATE(':', host) - 1)) = '', host, LEFT(HOST, (LOCATE(':', host) - 1))) host, command, time, REPLACE(SUBSTRING(info,1,25),'\n','') info_25 FROM information_schema.innodb_trx JOIN information_schema.processlist ON innodb_trx.trx_mysql_thread_id = processlist.id WHERE (NOW() - trx_started) > 60 ORDER BY trx_started;
+--------+---------------------+----------------------+----------------+------+-----------+----------+------+---------------------------+
| trx_id | trx_started | trx_duration_seconds | processlist_id | user | host | command | time | info_25 |
+--------+---------------------+----------------------+----------------+------+-----------+----------+------+---------------------------+
| 511239 | 2020-04-22 16:52:23 | 2754 | 3515 | dba | localhost | Sleep | 1101 | NULL |
| 511240 | 2020-04-22 16:53:44 | 74 | 3553 | root | localhost | Query | 38 | update t1 set name="test" |
+--------+---------------------+----------------------+----------------+------+-----------+----------+------+---------------------------+
2 rows in set (0.00 sec)

3. Clear processlist

mysql is about to disconnect and shut down. We can manually help mysql.

Use pt-kill to view and kill active and sleeping connections. There should be no new write connections coming in at this time. We only handle read connections.

pt-kill --host="localhost" --victims="all" --interval=10 --ignore-user="pmm|orchestrator" --busy-time=1 --idle-time=1 --print [--kill]

Here you can selectively exclude connections established by certain users.

4. Configure innodb to complete the maximum flush

SET GLOBAL innodb_fast_shutdown=0;
SET GLOBAL innodb_max_dirty_pages_pct=0;
SET GLOBAL innodb_change_buffering='none';

Disabling innodb_fast_shutdown may cause the shutdown process to take several minutes or even hours because it is necessary to wait for the undo log to be purged and the changebuffer to be merged.

To speed up shutdown, set innodb_max_dirty_pages_pct=0 and monitor the results of the following query. The expected value is 0, but this is not always guaranteed if there is activity in MySQL. Then, if the result does not continue to get smaller, you can proceed to the next step:

SHOW GLOBAL STATUS LIKE '%dirty%';

If you use pmm monitoring, you can view the diagram of "innodb change buffer".

5. Dump the contents of the buffer pool

SET GLOBAL innodb_buffer_pool_dump_pct=75;
SET GLOBAL innodb_buffer_pool_dump_now=ON;
mysql> SHOW STATUS LIKE 'Innodb_buffer_pool_dump_status';
+--------------------------------+--------------------------------------------------+
| Variable_name | Value |
+--------------------------------+--------------------------------------------------+
| Innodb_buffer_pool_dump_status | Buffer pool(s) dump completed at 200429 14:04:47 |
+--------------------------------+--------------------------------------------------+
1 row in set (0.01 sec)

To load the dumped content at startup, check the configuration of the parameter innodb_buffer_pool_load_at_startup.

6. Brush log

FLUSH LOGS;

Now, you can shut down mysql.

Most of the time, we just execute the stop command, and it is normal for MySQL to shut down and restart. Occasionally there are problems.

The above are the details on how to safely shut down MySQL. For more information on safely shutting down MySQL, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • It's the end of the year, is your MySQL password safe?
  • MySQL database rename fast and safe method (3 kinds)
  • How to gracefully and safely shut down the MySQL process
  • Some suggestions for ensuring MySQL data security
  • How to safely shut down a MySQL instance
  • mysql security management details

<<:  Native JS realizes the special effect of spreading love by mouse sliding

>>:  Understanding innerHTML

Recommend

Detailed explanation of Linux redirection usage

I believe that everyone needs to copy and paste d...

Reasons for the sudden drop in MySQL performance

Sometimes you may encounter a situation where a S...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

Detailed analysis of binlog_format mode and configuration in MySQL

There are three main ways of MySQL replication: S...

ElementUI implements cascading selector

This article example shares the specific code of ...

How to use lodop print control in Vue to achieve browser compatible printing

Preface This control will have a watermark at the...

Example of downloading files with vue+django

Table of contents 1. Overview 2. Django Project 3...

Node.js makes a simple crawler case tutorial

Preparation First, you need to download nodejs, w...

Install Docker for Windows on Windows 10 Home Edition

0. Background Hardware: Xiaomi Notebook Air 13/In...

Example of using CASE WHEN in MySQL sorting

Preface In a previous project, the CASE WHEN sort...

Detailed explanation of MySQL from getting started to giving up - installation

What you will learn 1. Software installation and ...

Implementation steps for installing java environment in docker

This article is based on Linux centos8 to install...

How to use a field in one table to update a field in another table in MySQL

1. Modify 1 column update student s, city c set s...

Details on using bimface in vue

Table of contents 1. Install Vue scaffolding 2. C...

How to use nodejs to write a data table entity class generation tool for C#

Although Microsoft provides T4 templates, I find ...