prepare mysql safe shutdown

In Percona’s Managed Services, we start up and shut down MySQL servers all the time.  Over the years, we’ve seen various issues occur due to the way servers are shut down.  No matter if it is a situation where unexpected errors appear in the log or you’re stuck waiting for hours for a server to finishing shut down… Here are some steps we can take to minimize any trouble and prepare MySQL for shutdown.

This list of “super safe practices” was born from lessons we’ve learned in a wide variety of MySQL environments.

1. Stop Replication.

Under some (rare) circumstances, a slave may try to startup in the incorrect position. To help minimize this risk, stop the IO thread first so it’s not receiving new events.

Wait for the SQL thread to apply all events, then stop it too.

This puts both of the replication threads in a consistent position so that the relay log only contains executed events and the relay_log_info_repository positions are up-to-date.

For multi-threaded slaves, make sure to fill in the gaps when stopping replication.

2. Commit, Rollback, or Kill Long-Running Transactions.

A lot can happen in 1 minute and InnoDB must rollback uncommitted transactions during a shutdown. This is expensive and may take a long time. Any rolled back transaction may mean data loss, so ideally there are none open at shutdown.

If shutting down a read/write server, the write traffic should be pointed to another server beforehand (ex: from a failover). Or perhaps the server is a BI (Business Intelligence) slave replicating from a master but receiving writes to BI databases. In this case, coordinate a shutdown time with your internal teams to stop those writes.

If you must continue to shutdown a server receiving transactions, the below query will report transactions running > 60 seconds and return some metadata. Use these details to research further and decide how to proceed (commit, rollback, kill).

Note: The query will also report XA PREPARED transactions which likely should not be manually committed, rolled back, or killed.

3. Clean up the Processlist.

MySQL is going to shut down and terminate all connections. So in the spirit of teamwork, we can lend a hand!

Use pt-kill to review or kill both active and sleeping connections. Again, at this point, there should be no incoming write traffic. We are now handling read only connections.

This command will match all sleeping and active connections, but ignore connections from the percona and orchestrator user.

4. Configure InnoDB for Max Flushing.

You’ll often see a similar recommendation when shutting down MySQL for an upgrade.

Disabling innodb_fast_shutdown can add minutes or hours to the actual shutdown as you wait for a full undo log purge and change buffer merge.

To help speed up the shutdown time, set innodb_max_dirty_pages_pct to 0 and monitor the count with the below query. The desired result is 0 but it’s not always possible if there is still activity on MySQL. If it seems like there is no more gain (the count is not continually getting any lower), call it good and move onto the next step.

Also, we have stopped the innodb_change_buffering. If using PMM, you can review the size and activity with the “InnoDB Change Buffer” graphs.

5. Dump the Buffer Pool.

Most likely this server is going to be re-introduced to the cluster. Dumping and loading the buffer pool contents will greatly reduce the warm up time after startup.

Dump the buffer pool now, while MySQL is still running.

Monitor the status and wait for it to complete.

To make sure the buffer pool will get loaded, check that innodb_buffer_pool_load_at_startup is not being disabled in the CNF (this is a global read only variable that’s ON by default).

6. Flush the Logs.

We’ll flush logs for good measure.

MySQL is now prepared for shutdown!

Summary

Most of the time, DBAs just issue the stop command and MySQL goes down and comes back up just fine (this is true). However, the one time it does not go well…what happens? New procedures (similar to these steps) are put into place to help ensure a clean shutdown.

Don’t wait for that “one time” to happen. Create a procedure now and be patient with the extra steps!


Our solution brief “Get Up and Running with Percona Server for MySQL” outlines setting up a MySQL® database on-premises using Percona Server for MySQL. It includes failover and basic business continuity components.

Download PDF

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
MURALI KRISHNA

Along with that, how about setting the Master to READ ONLY if you want to stop a Master

SET GLOBAL READ_ONLY = ON;

and wait for couple of minutes until you can confirm that all slaves caught up.

3B Optic Instruments

should globals be reset to their old values after restart?