Skip to content

Commit e78942a

Browse files
WL#9809 - RESTART
The patch implements the RESTART SQL which meets the following specifications: 1. RESTART is supported on *nix OS which support systemd. 2. For other *nix OS not supporting systemd, RESTART will be supported through mysqld_safe. 3. Windows support RESTART both as windows service and standalone. 4. RESTART logs system level note to the error log indicating the user who executed RESTART. 5. To execute RESTART, the user should have SHUTDOWN privilege. 6. RESTART shall fail with ER_RESTRT_FAILED when mysqld is not managed by supervisor (systemd or mysqld_safe etc.) 7. RESTART sql initiates a restart of the server for the client. It shutdowns the system with a special exit value (16) and the monitor or supervisor process. The OK status is sent to client as restart is initiated and it is up to client to determine any failure in the subsequent process.
1 parent bd97e2e commit e78942a

26 files changed

+1719
-98
lines changed

include/my_sqlcommand.h

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ enum enum_sql_command {
192192
SQLCOM_CLONE,
193193
SQLCOM_LOCK_INSTANCE,
194194
SQLCOM_UNLOCK_INSTANCE,
195+
SQLCOM_RESTART_SERVER,
195196
/* This should be the last !!! */
196197
SQLCOM_END
197198
};

include/mysql/plugin_audit.h.pp

+1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@
332332
SQLCOM_CLONE,
333333
SQLCOM_LOCK_INSTANCE,
334334
SQLCOM_UNLOCK_INSTANCE,
335+
SQLCOM_RESTART_SERVER,
335336
SQLCOM_END
336337
};
337338
typedef enum

mysql-test/r/mysqld--help-notwin.result

+1-1
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ performance-schema-max-socket-classes 10
15611561
performance-schema-max-socket-instances -1
15621562
performance-schema-max-sql-text-length 1024
15631563
performance-schema-max-stage-classes 150
1564-
performance-schema-max-statement-classes 209
1564+
performance-schema-max-statement-classes 210
15651565
performance-schema-max-statement-stack 10
15661566
performance-schema-max-table-handles -1
15671567
performance-schema-max-table-instances -1

mysql-test/r/mysqld--help-win.result

+1-1
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ performance-schema-max-socket-classes 10
15561556
performance-schema-max-socket-instances -1
15571557
performance-schema-max-sql-text-length 1024
15581558
performance-schema-max-stage-classes 150
1559-
performance-schema-max-statement-classes 209
1559+
performance-schema-max-statement-classes 210
15601560
performance-schema-max-statement-stack 10
15611561
performance-schema-max-table-handles -1
15621562
performance-schema-max-table-instances -1

mysql-test/r/restart_server.result

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
RESTART;
2+
ERROR HY000: Restart server failed (mysqld is not managed by supervisor process).
3+
# Verifying RESTART using mysqld_safe as supervisor process.
4+
# Executing a sql command after RESTART.
5+
SELECT 1;
6+
1
7+
1
8+
CREATE USER u1@localhost;
9+
GRANT SHUTDOWN ON *.* TO u1@localhost;
10+
RESTART;
11+
REVOKE SHUTDOWN ON *.* FROM u1@localhost;
12+
FLUSH PRIVILEGES;
13+
RESTART;
14+
ERROR 42000: Access denied; you need (at least one of) the SHUTDOWN privilege(s) for this operation
15+
DROP USER u1@localhost;
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RESTART;
2+
# Executing a sql command after RESTART.
3+
SELECT 1;
4+
1
5+
1
6+
SHUTDOWN;

mysql-test/t/mysqld_safe.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ EOF
3535
--let $_server_id= `SELECT @@server_id`
3636
--let $_expect_file_name= $MYSQLTEST_VARDIR/tmp/mysqld.$_server_id.expect
3737
--exec echo "wait" > $_expect_file_name
38-
--shutdown_server
38+
--shutdown_server
3939
--source include/wait_until_disconnected.inc
4040

4141

mysql-test/t/restart_server.test

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
--source include/have_mysqld_safe.inc
2+
--source include/not_windows.inc
3+
4+
5+
# Ensure when RESTART when mysqld is managed with no SUPERVISOR returns error.
6+
--error ER_RESTART_SERVER_FAILED
7+
RESTART;
8+
9+
--echo # Verifying RESTART using mysqld_safe as supervisor process.
10+
11+
# 1) Set valiables to be used in parameters of mysqld_safe.
12+
let $MYSQLD_DATADIR= `SELECT @@datadir`;
13+
let $MYSQL_BASEDIR= `SELECT @@basedir`;
14+
let $MYSQL_SOCKET= `SELECT @@socket`;
15+
let $MYSQL_TIMEZONE= `SELECT @@time_zone`;
16+
let $MYSQL_PIDFILE= `SELECT @@pid_file`;
17+
let $MYSQL_PORT= `SELECT @@port`;
18+
let $MYSQL_MESSAGESDIR= `SELECT @@lc_messages_dir`;
19+
let $start_page_size= `select @@innodb_page_size`;
20+
let $other_page_size_k= `SELECT $start_page_size DIV 1024`;
21+
let $other_page_size_nk= `SELECT CONCAT($other_page_size_k,'k')`;
22+
23+
# mysqld_path to be passed to --ledir
24+
# use test;
25+
perl;
26+
my $dir = $ENV{'MYSQLTEST_VARDIR'};
27+
open ( OUTPUT, ">$dir/tmp/mysqld_path_file.inc") ;
28+
my $path = $ENV{MYSQLD};
29+
$path =~ /^(.*)\/([^\/]*)$/;
30+
print OUTPUT "let \$mysqld_path = $1;\n";
31+
print OUTPUT "let \$mysqld_bin = $2;\n";
32+
close (OUTPUT);
33+
EOF
34+
35+
#Get the value of the variable from to MTR, from perl
36+
--source $MYSQLTEST_VARDIR/tmp/mysqld_path_file.inc
37+
38+
#Remove the temp file
39+
--remove_file $MYSQLTEST_VARDIR/tmp/mysqld_path_file.inc
40+
41+
# 2) Shutdown mysqld which is started by mtr.
42+
--let $_server_id= `SELECT @@server_id`
43+
--let $_expect_file_name= $MYSQLTEST_VARDIR/tmp/mysqld.$_server_id.expect
44+
--exec echo "wait" > $_expect_file_name
45+
--shutdown_server
46+
--source include/wait_until_disconnected.inc
47+
48+
# 3) Run the mysqld_safe script with exec.
49+
--exec sh $MYSQLD_SAFE --defaults-file=$MYSQLTEST_VARDIR/my.cnf --log-error=$MYSQLTEST_VARDIR/log/err.log --basedir=$MYSQL_BASEDIR --ledir=$mysqld_path --mysqld=$mysqld_bin --datadir=$MYSQLD_DATADIR --socket=$MYSQL_SOCKET --pid-file=$MYSQL_PIDFILE --port=$MYSQL_PORT --timezone=SYSTEM --log-output=file --loose-debug-sync-timeout=600 --default-storage-engine=InnoDB --default-tmp-storage-engine=InnoDB --secure-file-priv="" --loose-skip-log-bin --core-file --lc-messages-dir=$MYSQL_MESSAGESDIR --innodb-page-size=$other_page_size_nk < /dev/null > /dev/null 2>&1 &
50+
# mysqld_safe takes some time to start mysqld
51+
--enable_reconnect
52+
--source include/wait_until_connected_again.inc
53+
--disable_reconnect
54+
55+
# 4) Reconnect to mysqld again
56+
connection default;
57+
58+
# 5) Execute the RESTART sql command.
59+
--exec $MYSQL -h localhost -S $MYSQL_SOCKET -P $MYSQL_PORT -u root -e "RESTART" 2>&1
60+
--source include/wait_until_disconnected.inc
61+
# mysqld_safe takes some time to restart mysqld
62+
--enable_reconnect
63+
--source include/wait_until_connected_again.inc
64+
--echo # Executing a sql command after RESTART.
65+
SELECT 1;
66+
67+
# 5.1) Create user and GRANT shutdown privilege on the user, execute RESTART.
68+
# First check check user can't execute RESTART without SHUTDOWN privilege.
69+
CREATE USER u1@localhost;
70+
GRANT SHUTDOWN ON *.* TO u1@localhost;
71+
--connect(con1,localhost,u1,'',,)
72+
RESTART;
73+
--source include/wait_until_disconnected.inc
74+
--enable_reconnect
75+
--source include/wait_until_connected_again.inc
76+
--connect(root_con,localhost,root,'',,)
77+
REVOKE SHUTDOWN ON *.* FROM u1@localhost;
78+
FLUSH PRIVILEGES;
79+
disconnect con1;
80+
--connect(con1,localhost,u1,'',,)
81+
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
82+
RESTART;
83+
--connection root_con
84+
DROP USER u1@localhost;
85+
--disable_reconnect
86+
87+
# 7) Shutdown mysqld with mysqladmin
88+
--exec $MYSQLADMIN -h localhost -S $MYSQL_SOCKET -P $MYSQL_PORT -u root shutdown 2>&1
89+
90+
# Delay introduced - mysqld_safe takes some time to restart mysqld
91+
--source include/wait_until_disconnected.inc
92+
93+
# 8) Restart mysqld of mtr
94+
--exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
95+
--enable_reconnect
96+
--source include/wait_until_connected_again.inc

mysql-test/t/restart_server_win.test

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--source include/windows.inc
2+
3+
# Check RESTART of standalone server under windows.
4+
5+
RESTART;
6+
# Wait until server comes up.
7+
--enable_reconnect
8+
--source include/wait_until_connected_again.inc
9+
--echo # Executing a sql command after RESTART.
10+
SELECT 1;
11+
--disable_reconnect
12+
13+
# Shutdown and restart mysqld of mtr.
14+
SHUTDOWN;
15+
# Wait for mysqld to come up.
16+
--source include/wait_until_disconnected.inc
17+
18+
--exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
19+
--enable_reconnect
20+
--source include/wait_until_connected_again.inc

scripts/mysqld_safe.sh

+19-10
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ eval_log_error () {
195195
esac
196196

197197
#echo "Running mysqld: [$cmd]"
198+
cmd="env MYSQLD_PARENT_PID=$$ $cmd"
198199
eval "$cmd"
199200
}
200201

@@ -901,12 +902,18 @@ fast_restart=0
901902
max_fast_restarts=5
902903
# flag whether a usable sleep command exists
903904
have_sleep=1
904-
905905
while true
906906
do
907907
start_time=`date +%M%S`
908+
echo $cmd
908909

909910
eval_log_error "$cmd"
911+
if [ $? -eq 16 ] ; then
912+
dont_restart_mysqld=false
913+
echo "Restarting mysqld..."
914+
else
915+
dont_restart_mysqld=true
916+
fi
910917

911918
# hypothetical: log was renamed but not
912919
# flushed yet. we'd recreate it with
@@ -936,15 +943,17 @@ do
936943

937944
end_time=`date +%M%S`
938945

939-
if test ! -f "$pid_file" # This is removed if normal shutdown
940-
then
941-
break
942-
else # self's mysqld crashed or other's mysqld running
943-
PID=`cat "$pid_file"`
944-
if @CHECK_PID@
945-
then # true when above pid belongs to a running mysqld process
946-
log_error "A mysqld process with pid=$PID is already running. Aborting!!"
947-
exit 1
946+
if $dont_restart_mysqld; then
947+
if test ! -f "$pid_file" # This is removed if normal shutdown
948+
then
949+
break
950+
else # self's mysqld crashed or other's mysqld running
951+
PID=`cat "$pid_file"`
952+
if @CHECK_PID@
953+
then # true when above pid belongs to a running mysqld process
954+
log_error "A mysqld process with pid=$PID is already running. Aborting!!"
955+
exit 1
956+
fi
948957
fi
949958
fi
950959

scripts/systemd/mysqld.service.in

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -56,4 +56,10 @@ Restart=on-failure
5656

5757
RestartPreventExitStatus=1
5858

59+
# Always restart when mysqld exits with exit code of 3.
60+
RestartForceExitStatus=3
61+
62+
# Set enviroment variable MYSQLD_MONITOR_PROCESS. This is required for restart.
63+
Environment=MYSQLD_PARENT_PID=1
64+
5965
PrivateTmp=false

share/errmsg-utf8.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -10985,7 +10985,6 @@ ER_ONLY_IMPLEMENTED_FOR_SRID_0_AND_4326 22S00
1098510985

1098610986
ER_EXPIRE_LOGS_DAYS_IGNORED
1098710987
eng "The option expire_logs_days and binlog_expire_logs_seconds cannot be used together. Please use binlog_expire_logs_seconds to set the expire time (expire_logs_days is deprecated)"
10988-
1098910988
#
1099010989
# Regular expression errors. None of these should take any arguments.
1099110990
# We strive to keep this list in the same order as in UErrorCode
@@ -11509,6 +11508,12 @@ ER_KEYRING_MIGRATION_FAILED
1150911508
ER_KEYRING_MIGRATION_SUCCESSFUL
1151011509
eng "Keyring migration successful."
1151111510

11511+
ER_RESTART_SERVER_FAILED
11512+
eng "Restart server failed (%s)."
11513+
11514+
ER_RESTART_RECEIVED_INFO
11515+
eng "Received RESTART from user %s. Restarting mysqld (Version: %s)."
11516+
1151211517
#
1151311518
# End of 8.0 error messages.
1151411519
#

sql/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ SET(SQL_SHARED_SOURCES
424424
sql_reload.cc
425425
sql_rename.cc
426426
sql_resolver.cc
427+
sql_restart_server.cc
427428
sql_rewrite.cc
428429
sql_select.cc
429430
sql_servers.cc
@@ -546,9 +547,10 @@ ENDIF()
546547

547548
IF(WIN32)
548549
LIST(APPEND SQL_SOURCE
549-
named_pipe.cc
550550
conn_handler/named_pipe_connection.cc
551551
conn_handler/shared_memory_connection.cc
552+
named_pipe.cc
553+
restart_monitor_win.cc
552554
)
553555
ENDIF()
554556

sql/lex.h

+1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ static const SYMBOL symbols[] = {
544544
{ SYM("RESPECT", RESPECT_SYM)},
545545
{ SYM("RESIGNAL", RESIGNAL_SYM)},
546546
{ SYM("RESOURCE", RESOURCE_SYM)},
547+
{ SYM("RESTART", RESTART_SYM)},
547548
{ SYM("RESTORE", RESTORE_SYM)},
548549
{ SYM("RESTRICT", RESTRICT)},
549550
{ SYM("RESUME", RESUME_SYM)},

0 commit comments

Comments
 (0)