Debian Codegraph @

packaging/deb-in/extra/mysql-helpers

raw
1 # Copyright (c) 2017, 2026, Oracle and/or its affiliates.
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License, version 2.0,
5 # as published by the Free Software Foundation.
6 #
7 # This program is designed to work with certain software (including
8 # but not limited to OpenSSL) that is licensed under separate terms,
9 # as designated in a particular file or component or in included license
10 # documentation. The authors of MySQL hereby grant you an additional
11 # permission to link the program and your derivative works with the
12 # separately licensed software that they have either included with
13 # the program or referenced in the documentation.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License, version 2.0, for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24 # Wait for server to start up (this requires the client package)
25 pinger () {
26 while /bin/true ; do
27 sleep 1
28 mysqladmin ping >/dev/null 2>&1 && break
29 done
30 }
31
32 # To avoid having hardcoded paths in the script, we do a search on the path, as suggested at:
33 # https://www.debian.org/doc/manuals/developers-reference/ch06.en.html#bpp-debian-maint-scripts
34 pathfind() {
35 OLDIFS="$IFS"
36 IFS=:
37 for p in $PATH; do
38 if [ -x "$p/$*" ]; then
39 IFS="$OLDIFS"
40 return 0
41 fi
42 done
43 IFS="$OLDIFS"
44 return 1
45 }
46
47 # Fetch value from config files
48 # Usage: get_mysql_option [section] [option] [default value]
49 get_mysql_option() {
50 if pathfind my_print_defaults; then
51 RESULT=$(my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1)
52 fi
53 if [ -z "$RESULT" ]; then
54 RESULT="$3"
55 fi
56 echo $RESULT
57 }
58
59 # Check if server is running
60 get_running () {
61 MYSQLDATA=$(get_mysql_option mysqld datadir "/var/lib/mysql")
62 PIDFILE=$(get_mysql_option mysqld_safe pid-file "")
63 if [ -z "$PIDFILE" ]; then
64 PIDFILE=$(get_mysql_option mysqld pid-file "$MYSQLDATA/$(hostname).pid")
65 fi
66 if [ -e "$PIDFILE" ] && [ -d "/proc/$(cat "$PIDFILE")" ]; then
67 echo 1
68 else
69 echo 0
70 fi
71 }
72
73 # Runs an arbitrary init sql file supplied in $1. Does not require login access
74 run_init_sql() {
75 tmpdir=$(mktemp -d)
76 chown mysql:mysql "$tmpdir"
77 mysqld --defaults-group-suffix="$2" --user=mysql --init-file="$1" --socket="$tmpdir/mysqld.sock" --pid-file="$tmpdir/mysqld.pid" > /dev/null 2>&1
78 result=$?
79 rm -rf "$tmpdir"
80 return $result
81 }
82
83 # Verify that everything the server needs to run is set up
84 verify_ready() {
85 # For multi-instance support with the mysql@ service
86 INSTANCE="mysqld${1}"
87 MYSQLDATA=$(get_mysql_option ${INSTANCE} datadir "/var/lib/mysql")
88 MYSQLFILES=$(get_mysql_option ${INSTANCE} secure-file-priv "/var/lib/mysql-files")
89 MYSQLKEYRING=$(dirname $(get_mysql_option ${INSTANCE} keyring-file-data "/var/lib/mysql-keyring/keyring"))
90 MYSQLLOG=$(dirname $(get_mysql_option ${INSTANCE} log-error "/var/log/mysql/error.log"))
91 MYSQLRUN=/var/run/mysqld
92
93 if ! getent group mysql >/dev/null; then
94 addgroup --system mysql >/dev/null
95 fi
96
97 if ! getent passwd mysql >/dev/null; then
98 adduser --ingroup mysql --system --disabled-login --no-create-home --home ${MYSQLDATA} --shell /bin/false --gecos "MySQL Server" mysql >/dev/null
99 fi
100 ERROR_FLAG=0
101 if [ ! -d ${MYSQLDATA} -a ! -L ${MYSQLDATA} ]; then
102 if [ "$(dirname "${MYSQLDATA}")" = "/var/lib" ]; then
103 install -d -m0750 -omysql -gmysql ${MYSQLDATA}
104 else
105 echo "Error: Datadir ${MYSQLDATA} does not exist. For security reasons the service will not automatically create directories outside /var/lib.."
106 ERROR_FLAG=1
107 fi
108 fi
109
110 # Ensures that the mysql_upgrade_info file is owned by the database
111 if [ -O "${MYSQLDATA}/mysql_upgrade_info" ]; then
112 chown --reference="${MYSQLDATA}" "${MYSQLDATA}/mysql_upgrade_info"
113 fi
114
115 if [ "${MYSQLFILES}" != "NULL" -a ! -d "${MYSQLFILES}" -a ! -L "${MYSQLFILES}" ]; then
116 if [ "$(dirname ${MYSQLFILES})" = "/var/lib" ]; then
117 install -d -m0770 -omysql -gmysql "${MYSQLFILES}"
118 else
119 echo "Error: Secure-file-priv dir ${MYSQLFILES} does not exist. For security reasons the service will not automatically create directories outside /var/lib."
120 ERROR_FLAG=1
121 fi
122 fi
123
124 if [ ! -d ${MYSQLKEYRING} -a ! -L ${MYSQLKEYRING} ]; then
125 if [ "$(dirname "${MYSQLKEYRING}")" = "/var/lib" ]; then
126 install -d -m0750 -omysql -gmysql ${MYSQLKEYRING}
127 else
128 echo "Warning: Keyring dir ${MYSQLKEYRING} does not exist. For security reasons the service will not automatically create directories outside /var/lib. The server may not start correctly."
129 fi
130 fi
131
132 if [ ! -d ${MYSQLLOG} -a ! -L ${MYSQLLOG} ]; then
133 if [ "$(dirname "${MYSQLLOG}")" = "/var/log" ]; then
134 install -d -m0750 -omysql -gadm ${MYSQLLOG}
135 install /dev/null -m0640 -omysql -gadm ${MYSQLLOG}/error.log
136 else
137 echo "Error: Log dir ${MYSQLLOG} does not exist. For security reasons the service will not automatically create directories outside /var/log."
138 ERROR_FLAG=1
139 fi
140 fi
141
142 if [ ${ERROR_FLAG} = 1 ]; then
143 echo "Errors found. Aborting."
144 exit 1
145 fi
146
147 if [ ! -d ${MYSQLRUN} -a ! -L ${MYSQLRUN} ]; then
148 install -d -m0755 -omysql -gmysql ${MYSQLRUN}
149 fi
150 }
151
152 # Verify the database exists and is ssl ready
153 verify_database() {
154 # For multi-instance support with the mysql@ service
155 INSTANCE="mysqld${1}"
156 MYSQLDATA=$(get_mysql_option ${INSTANCE} datadir "/var/lib/mysql")
157 MYSQLFILES=$(get_mysql_option ${INSTANCE} secure-file-priv "/var/lib/mysql-files")
158
159 if [ ! -d "${MYSQLDATA}/mysql" ] && [ -d "${MYSQLFILES}" ]; then
160 su - mysql -s /bin/bash -c "/usr/sbin/mysqld --defaults-group-suffix="$1" --initialize-insecure > /dev/null"
161 SQL=$(mktemp -u ${MYSQLFILES}/XXXXXXXXXX)
162 install /dev/null -m0600 -omysql -gmysql "${SQL}"
163 cat << EOF > ${SQL}
164 SET @@SESSION.SQL_LOG_BIN=0;
165 USE mysql;
166 INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';
167 ALTER USER 'root'@'localhost' IDENTIFIED WITH 'auth_socket';
168 SHUTDOWN;
169 EOF
170 run_init_sql "$SQL" "$1"
171 rm -f "$SQL"
172 fi
173
174 if [ -x /usr/bin/mysql_ssl_rsa_setup -a ! -e "${MYSQLDATA}/server-key.pem" ]; then
175 mysql_ssl_rsa_setup --datadir="${MYSQLDATA}" --uid=mysql >/dev/null 2>&1
176 fi
177 }
178
179 verify_server () {
180 TIMEOUT=0
181 if [ "${1}" = "start" ]; then
182 TIMEOUT=${STARTTIMEOUT}
183 elif [ "${1}" = "stop" ]; then
184 TIMEOUT=${STOPTIMEOUT}
185 fi
186
187 COUNT=0
188 while [ ${COUNT} -lt ${TIMEOUT} ];
189 do
190 COUNT=$(( COUNT+1 ))
191 echo -n .
192 if [ "${1}" = "start" ] && [ "$(get_running)" = 1 ]; then
193 if [ -z ${2} ]; then
194 echo
195 fi
196 return 0
197 fi
198 if [ "${1}" = "stop" ] && [ "$(get_running)" = 0 ]; then
199 if [ -z ${2} ]; then
200 echo
201 fi
202 return 0
203 fi
204 sleep 1
205 done
206 return 1
207 }
208