blob: e069e128e9f68cd13be4ade3a9f5fee9d22f4538 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyer6d04fd72012-12-21 11:03:37 -06003# lib/databases/mysql
4# Functions to control the configuration and operation of the **MySQL** database backend
Terry Wilson428af5a2012-11-01 16:12:39 -04005
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - DATABASE_{HOST,USER,PASSWORD} must be defined
Terry Wilson428af5a2012-11-01 16:12:39 -04009
10# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110011_XTRACE_DB_MYSQL=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -040012set +o xtrace
13
armando-migliacciob3d88222015-06-12 07:54:03 -070014MYSQL_DRIVER=${MYSQL_DRIVER:-PyMySQL}
Carlos Camachocc6e20b2022-01-07 15:30:56 +010015INSTALL_DATABASE_SERVER_PACKAGES=$(trueorfalse True INSTALL_DATABASE_SERVER_PACKAGES)
Dean Troyercc6b4432013-04-08 15:38:03 -050016
Terry Wilson428af5a2012-11-01 16:12:39 -040017register_database mysql
18
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +020019if [[ -z "$MYSQL_SERVICE_NAME" ]]; then
20 MYSQL_SERVICE_NAME=mysql
21 if is_fedora && ! is_oraclelinux; then
22 MYSQL_SERVICE_NAME=mariadb
Dr. Jens Harbott0b79f6f2023-07-04 07:18:01 +020023 elif [[ "$DISTRO" =~ bookworm|bullseye ]]; then
Jens Harbott959a7c22021-05-02 09:29:15 +020024 MYSQL_SERVICE_NAME=mariadb
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +020025 fi
Sean Dague53753292014-12-04 19:38:15 -050026fi
Dean Troyercc6b4432013-04-08 15:38:03 -050027
28# Functions
29# ---------
30
Julien Danjou0eec4f82015-09-08 10:45:06 +000031function get_database_type_mysql {
32 if [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
33 echo mysql+pymysql
34 else
35 echo mysql
36 fi
37}
38
Dean Troyer995eb922013-03-07 16:11:40 -060039# Get rid of everything enough to cleanly change database backends
40function cleanup_database_mysql {
Dirk Mueller1d968d72017-09-23 14:45:42 +020041 stop_service $MYSQL_SERVICE_NAME
Dean Troyer995eb922013-03-07 16:11:40 -060042 if is_ubuntu; then
43 # Get ruthless with mysql
Sean Dague8f90f762015-01-14 10:36:48 -050044 apt_get purge -y mysql* mariadb*
Dean Troyer995eb922013-03-07 16:11:40 -060045 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030046 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060047 return
Dirk Mueller1d968d72017-09-23 14:45:42 +020048 elif is_oraclelinux; then
Wiekus Beukesec47bc12015-03-19 08:20:38 -070049 uninstall_package mysql-community-server
50 sudo rm -rf /var/lib/mysql
Martin Kopecec07b342023-01-24 17:38:45 +010051 elif is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010052 uninstall_package mariadb-server
53 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060054 else
55 return
56 fi
Dean Troyer995eb922013-03-07 16:11:40 -060057}
58
Terry Wilson428af5a2012-11-01 16:12:39 -040059function recreate_database_mysql {
60 local db=$1
zhhuabj2832f282013-05-08 18:43:26 +080061 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020062 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
Terry Wilson428af5a2012-11-01 16:12:39 -040063}
64
65function configure_database_mysql {
Ade Lee15b2e422023-01-24 14:44:13 +010066 local my_conf mysql slow_log my_client_conf
Terry Wilson428af5a2012-11-01 16:12:39 -040067 echo_summary "Configuring and starting MySQL"
68
Vincent Untzc18b9652012-12-04 12:36:34 +010069 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050070 my_conf=/etc/mysql/my.cnf
Martin Kopecec07b342023-01-24 17:38:45 +010071 elif is_oraclelinux; then
Wiekus Beukesec47bc12015-03-19 08:20:38 -070072 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010073 elif is_fedora; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050074 my_conf=/etc/my.cnf
Yuval Brik13e81ad2017-06-23 10:32:16 +030075 local cracklib_conf=/etc/my.cnf.d/cracklib_password_check.cnf
76 if [ -f "$cracklib_conf" ]; then
77 inicomment -sudo "$cracklib_conf" "mariadb" "plugin-load-add"
78 fi
Vincent Untz00011c02012-12-06 09:56:32 +010079 else
80 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040081 fi
82
Ade Lee15b2e422023-01-24 14:44:13 +010083 # Set fips mode on
84 if is_ubuntu; then
85 if is_fips_enabled; then
86 my_client_conf=/etc/mysql/mysql.conf.d/mysql.cnf
87 iniset -sudo $my_client_conf mysql ssl-fips-mode "on"
88 iniset -sudo $my_conf mysqld ssl-fips-mode "on"
89 fi
90 fi
91
Brian Haley7943a922022-03-14 13:53:41 -040092 # Change bind-address from localhost (127.0.0.1) to any (::)
93 iniset -sudo $my_conf mysqld bind-address "$(ipv6_unquote $SERVICE_LISTEN_ADDRESS)"
94
95 # (Re)Start mysql-server
Martin Kopecec07b342023-01-24 17:38:45 +010096 if is_fedora; then
Vincent Untz00011c02012-12-06 09:56:32 +010097 # service is not started by default
Dirk Mueller1d968d72017-09-23 14:45:42 +020098 start_service $MYSQL_SERVICE_NAME
Brian Haley7943a922022-03-14 13:53:41 -040099 elif is_ubuntu; then
100 # required since bind-address could have changed above
101 restart_service $MYSQL_SERVICE_NAME
Vincent Untz00011c02012-12-06 09:56:32 +0100102 fi
103
104 # Set the root password - only works the first time. For Ubuntu, we already
Jens Rosenboom9abb26d2016-12-07 21:12:55 +0100105 # did that with debconf before installing the package, but we still try,
Miguel Lavalle0a406482022-12-07 16:51:28 -0600106 # because the package might have been installed already. We don't do this
107 # for Ubuntu 22.04 (jammy) because the authorization model change in
108 # version 10.4 of mariadb. See
109 # https://mariadb.org/authentication-in-mariadb-10-4/
110 if ! (is_ubuntu && [[ "$DISTRO" == "jammy" ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]); then
111 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
112 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100113
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +0200114 # In case of Mariadb, giving hostname in arguments causes permission
115 # problems as it expects connection through socket
116 if is_ubuntu && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
117 local cmd_args="-uroot -p$DATABASE_PASSWORD "
118 else
Brian Haley7943a922022-03-14 13:53:41 -0400119 local cmd_args="-uroot -p$DATABASE_PASSWORD -h$SERVICE_LOCAL_HOST "
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +0200120 fi
121
122 # In mariadb e.g. on Ubuntu socket plugin is used for authentication
123 # as root so it works only as sudo. To restore old "mysql like" behaviour,
124 # we need to change auth plugin for root user
Dr. Jens Harbott0b79f6f2023-07-04 07:18:01 +0200125 # TODO(frickler): simplify this logic
126 if is_ubuntu && [[ ! "$DISTRO" =~ bookworm|bullseye ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
Miguel Lavalle0a406482022-12-07 16:51:28 -0600127 if [[ "$DISTRO" == "jammy" ]]; then
128 # For Ubuntu 22.04 (jammy) we follow the model outlined in
129 # https://mariadb.org/authentication-in-mariadb-10-4/
130 sudo mysql -e "ALTER USER $DATABASE_USER@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD('$DATABASE_PASSWORD');"
131 else
132 sudo mysql $cmd_args -e "UPDATE mysql.user SET plugin='' WHERE user='$DATABASE_USER' AND host='localhost';"
133 sudo mysql $cmd_args -e "FLUSH PRIVILEGES;"
134 fi
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +0200135 fi
Miguel Lavalle0a406482022-12-07 16:51:28 -0600136 if ! (is_ubuntu && [[ "$DISTRO" == "jammy" ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]); then
137 # Create DB user if it does not already exist
138 sudo mysql $cmd_args -e "CREATE USER IF NOT EXISTS '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
139 # Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
140 sudo mysql $cmd_args -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%';"
141 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400142
143 # Now update ``my.cnf`` for some local needs and restart the mysql service
144
Brian Haley7943a922022-03-14 13:53:41 -0400145 # Set default db type to InnoDB
Roman Podoliaka88b84092017-02-07 13:34:12 +0200146 iniset -sudo $my_conf mysqld sql_mode TRADITIONAL
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000147 iniset -sudo $my_conf mysqld default-storage-engine InnoDB
Jens Rosenboom4b59fbb2017-03-15 21:58:48 +0000148 iniset -sudo $my_conf mysqld max_connections 1024
Terry Wilson428af5a2012-11-01 16:12:39 -0400149
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000150 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
151 echo_summary "Enabling MySQL query logging"
Attila Fazekas1f316be2015-01-26 16:39:57 +0100152 if is_fedora; then
Attila Fazekas3b53aeb2014-04-30 11:57:22 +0200153 slow_log=/var/log/mariadb/mariadb-slow.log
154 else
155 slow_log=/var/log/mysql/mysql-slow.log
156 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200157 sudo sed -e '/log.slow.queries/d' \
158 -e '/long.query.time/d' \
159 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500160 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000161
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200162 # Turn on slow query log, log all queries (any query taking longer than
163 # 0 seconds) and log all non-indexed queries
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000164 iniset -sudo $my_conf mysqld slow-query-log 1
165 iniset -sudo $my_conf mysqld slow-query-log-file $slow_log
166 iniset -sudo $my_conf mysqld long-query-time 0
167 iniset -sudo $my_conf mysqld log-queries-not-using-indexes 1
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000168 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000169
Dan Smithc2772c22022-04-08 08:48:49 -0700170 if [[ "$MYSQL_GATHER_PERFORMANCE" == "True" ]]; then
Dan Smithfe52d7f2022-04-28 12:34:38 -0700171 echo "enabling MySQL performance counting"
172
173 # Install our sqlalchemy plugin
174 pip_install ${TOP_DIR}/tools/dbcounter
175
176 # Create our stats database for accounting
177 recreate_database stats
178 mysql -u $DATABASE_USER -p$DATABASE_PASSWORD -h $MYSQL_HOST -e \
179 "CREATE TABLE queries (db VARCHAR(32), op VARCHAR(32),
180 count INT, PRIMARY KEY (db, op)) ENGINE MEMORY" stats
Dan Smithc2772c22022-04-08 08:48:49 -0700181 fi
182
Dan Smith75673592023-02-13 14:41:40 +0000183 if [[ "$MYSQL_REDUCE_MEMORY" == "True" ]]; then
184 iniset -sudo $my_conf mysqld read_buffer_size 64K
185 iniset -sudo $my_conf mysqld innodb_buffer_pool_size 16M
186 iniset -sudo $my_conf mysqld thread_stack 192K
187 iniset -sudo $my_conf mysqld thread_cache_size 8
188 iniset -sudo $my_conf mysqld tmp_table_size 8M
189 iniset -sudo $my_conf mysqld sort_buffer_size 8M
190 iniset -sudo $my_conf mysqld max_allowed_packet 8M
191 fi
192
Dirk Mueller1d968d72017-09-23 14:45:42 +0200193 restart_service $MYSQL_SERVICE_NAME
Terry Wilson428af5a2012-11-01 16:12:39 -0400194}
195
196function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100197 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400198 # Seed configuration with mysql password so that apt-get install doesn't
199 # prompt us for a password upon install.
Bob Ball90333432015-01-19 10:56:42 +0000200 sudo debconf-set-selections <<MYSQL_PRESEED
201mysql-server mysql-server/root_password password $DATABASE_PASSWORD
202mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD
203mysql-server mysql-server/start_on_boot boolean true
Terry Wilson428af5a2012-11-01 16:12:39 -0400204MYSQL_PRESEED
205 fi
206
207 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
208 # as it allows you to access the mysql databases via ``mysql nova`` instead
209 # of having to specify the username/password each time.
210 if [[ ! -e $HOME/.my.cnf ]]; then
211 cat <<EOF >$HOME/.my.cnf
212[client]
213user=$DATABASE_USER
214password=$DATABASE_PASSWORD
Terry Wilson428af5a2012-11-01 16:12:39 -0400215EOF
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +0200216
217 if ! is_ubuntu || [ "$MYSQL_SERVICE_NAME" != "mariadb" ]; then
218 echo "host=$MYSQL_HOST" >> $HOME/.my.cnf
219 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400220 chmod 0600 $HOME/.my.cnf
221 fi
222 # Install mysql-server
Carlos Camachocc6e20b2022-01-07 15:30:56 +0100223 if [[ "$INSTALL_DATABASE_SERVER_PACKAGES" == "True" ]]; then
224 if is_oraclelinux; then
225 install_package mysql-community-server
226 elif is_fedora; then
227 install_package mariadb-server mariadb-devel mariadb
228 sudo systemctl enable $MYSQL_SERVICE_NAME
Carlos Camachocc6e20b2022-01-07 15:30:56 +0100229 elif is_ubuntu; then
230 install_package $MYSQL_SERVICE_NAME-server
231 else
232 exit_distro_not_supported "mysql installation"
233 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100234 fi
Dean Troyer5686dbc2015-03-09 14:27:51 -0500235}
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600236
Dean Troyer5686dbc2015-03-09 14:27:51 -0500237function install_database_python_mysql {
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600238 # Install Python client module
Sean Dague37421992015-05-20 06:37:11 -0700239 pip_install_gr $MYSQL_DRIVER
240 if [[ "$MYSQL_DRIVER" == "MySQL-python" ]]; then
241 ADDITIONAL_VENV_PACKAGES+=",MySQL-python"
Julien Danjou0f63eb32015-06-12 09:05:12 +0200242 elif [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
243 ADDITIONAL_VENV_PACKAGES+=",PyMySQL"
Sean Dague37421992015-05-20 06:37:11 -0700244 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400245}
246
247function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100248 local db=$1
Dan Smithfe52d7f2022-04-28 12:34:38 -0700249 local plugin
250
251 # NOTE(danms): We don't enable perf on subnodes yet because the
252 # plugin is not installed there
253 if [[ "$MYSQL_GATHER_PERFORMANCE" == "True" ]]; then
254 if is_service_enabled mysql; then
255 plugin="&plugin=dbcounter"
256 fi
257 fi
258
259 echo "$BASE_SQL_CONN/$db?charset=utf8$plugin"
Terry Wilson428af5a2012-11-01 16:12:39 -0400260}
261
Dean Troyercc6b4432013-04-08 15:38:03 -0500262
Terry Wilson428af5a2012-11-01 16:12:39 -0400263# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100264$_XTRACE_DB_MYSQL
Sean Dague584d90e2013-03-29 14:34:53 -0400265
266# Local variables:
267# mode: shell-script
268# End: