MySQL 8.4.2
Source Code Documentation
Stopping and Restarting a Server During a Test

If a server dies during execution of a test case, this will be interpreted as a failure.

However, there may be cases where you actually want to stop and possibly restart a server intentionally. It is possible to let the system know you expect a server to terminate, and to either wait or have it restarted immediately:

Before you initiate the action that will stop the server, the test case should write either restart or wait to the file $MYSQLTEST_VARDIR/tmp/mysqld.1.expect. The easiest way to do this is using the exec echo construct like in the following example.

If you write restart into this file, the server will be immediately restarted. If you write wait instead, the server will remain down, but can be restarted at a later time by writing restart to the file.

If you write restart: or restart_abort: (please note the colon) followed by some command line parameters, those will be appended to the command line arguments of the server on restart. The former should be used when the server is expected to restart successfully while the latter comes with an implicit follow up wait and can be used when the server is expected to exit due to the provided arguments.

--exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--shutdown_server 10
--source include/wait_until_disconnected.inc
# Do something while server is down
--exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--source include/wait_until_connected_again.inc

The file name to write the command to will be mysqld.2.expect for the slave server in replication tests. Note that you have to use $MYSQLTEST_VARDIR/tmp for the directory here; if you use $MYSQL_TMP_DIR instead, it will not work when running tests in parallel.

For your convenience, there are files which can be used to do what's shown in the example.

  • restart_mysqld.inc

    include/restart_mysqld.inc can be sourced in your own test case, to shutdown and start the server but the server is immediately started up again.

    # Restart the server
    --source include/restart_mysqld.inc

    It is also possible to provide additional command line options to the restarted server, by writing a line with restart: (with a colon) followed by one or more options into the expect file. These extra options will be dropped if the same server is restarted again, unless they are repeated. A value for shutdown timeout can also be set with $shutdown_server_timeout, when this file is used.

    # Restart server with extra parameters
    --let $shutdown_server_timeout= 10
    --let $restart_parameters= "restart: --innodb_autoinc_lock_mode=1"
    --source include/restart_mysqld.inc

  • kill_and_restart_mysqld.inc

    Similar to include/restart_mysqld.inc, include/kill_and_restart_mysqld.inc can be used to restart the server, but setting $shutdown_server_timeout will have no effect.

    # Restart server with extra parameters
    --let $restart_paramters= "restart: --innodb_autoinc_lock_mode=0"
    --source include/kill_and_restart_mysqld.inc

  • shutdown_mysqld.inc and start_mysqld.inc

    If there is something to be done between the server shutdown and startup, there is a file include/shutdown_mysqld.inc which can be used to shutdown the server. A shutdown timeout value can be set and passed to include/shutdown_mysqld.inc. include/start_mysqld.inc can be used to start the server again after it has been shutdown.

    # Timeout of 10 seconds
    --let $shutdown_server_timeout= 10
    --source include/shutdown_mysqld.inc
    # Do something while the server is down
    --source include/start_mysqld.inc

  • kill_mysqld.inc

    To the kill the server, include/kill_mysqld.inc can be sourced, which internally calls –shutdown_server 0. Therefore, setting a value for shutdown timeout will have no effect.

    --source include/kill_mysqld.inc
    # Do something while the server is down
    --source include/start_mysqld.inc

  • wait_until_disconnected.inc

    It is recommended to use include/wait_until_disconnected in tandem with –shutdown_server. It ensures that all connections to the server are dropped before proceeding with the test. This file is sourced internally in include/shutdown_mysqld.inc, include/kill_mysqld.inc, include/kill_and_restart_mysqld.inc, and include/restart_mysqld.inc.

  • wait_until_connected_again.inc

    include/wait_until_connected_again.inc is used after the server is started again, to ensure that the client connection is restored. It tries to establish the connection till a timeout occurs. This file is sourced internally in include/start_mysqld.inc, include/restart_mysqld.inc, and include/kill_and_restart_mysqld.inc.