blob: d4969d713c7d5303bc88f0a0790805ae78a7c3b4 [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}
Dean Troyercc6b4432013-04-08 15:38:03 -050015
Terry Wilson428af5a2012-11-01 16:12:39 -040016register_database mysql
17
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +020018if [[ -z "$MYSQL_SERVICE_NAME" ]]; then
19 MYSQL_SERVICE_NAME=mysql
20 if is_fedora && ! is_oraclelinux; then
21 MYSQL_SERVICE_NAME=mariadb
22 elif is_suse && systemctl list-unit-files | grep -q 'mariadb\.service'; then
23 # Older mariadb packages on SLES 12 provided mysql.service. The
24 # newer ones on SLES 12 and 15 use mariadb.service; they also
25 # provide a mysql.service symlink for backwards-compatibility, but
26 # let's not rely on that.
27 MYSQL_SERVICE_NAME=mariadb
28 fi
Sean Dague53753292014-12-04 19:38:15 -050029fi
Dean Troyercc6b4432013-04-08 15:38:03 -050030
31# Functions
32# ---------
33
Julien Danjou0eec4f82015-09-08 10:45:06 +000034function get_database_type_mysql {
35 if [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
36 echo mysql+pymysql
37 else
38 echo mysql
39 fi
40}
41
Dean Troyer995eb922013-03-07 16:11:40 -060042# Get rid of everything enough to cleanly change database backends
43function cleanup_database_mysql {
Dirk Mueller1d968d72017-09-23 14:45:42 +020044 stop_service $MYSQL_SERVICE_NAME
Dean Troyer995eb922013-03-07 16:11:40 -060045 if is_ubuntu; then
46 # Get ruthless with mysql
Sean Dague8f90f762015-01-14 10:36:48 -050047 apt_get purge -y mysql* mariadb*
Dean Troyer995eb922013-03-07 16:11:40 -060048 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030049 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060050 return
Dirk Mueller1d968d72017-09-23 14:45:42 +020051 elif is_oraclelinux; then
Wiekus Beukesec47bc12015-03-19 08:20:38 -070052 uninstall_package mysql-community-server
53 sudo rm -rf /var/lib/mysql
Dirk Mueller1d968d72017-09-23 14:45:42 +020054 elif is_suse || is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010055 uninstall_package mariadb-server
56 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060057 else
58 return
59 fi
Dean Troyer995eb922013-03-07 16:11:40 -060060}
61
Terry Wilson428af5a2012-11-01 16:12:39 -040062function recreate_database_mysql {
63 local db=$1
zhhuabj2832f282013-05-08 18:43:26 +080064 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020065 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
Terry Wilson428af5a2012-11-01 16:12:39 -040066}
67
68function configure_database_mysql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050069 local my_conf mysql slow_log
Terry Wilson428af5a2012-11-01 16:12:39 -040070 echo_summary "Configuring and starting MySQL"
71
Vincent Untzc18b9652012-12-04 12:36:34 +010072 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050073 my_conf=/etc/mysql/my.cnf
Wiekus Beukesec47bc12015-03-19 08:20:38 -070074 elif is_suse || is_oraclelinux; then
75 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010076 elif is_fedora; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050077 my_conf=/etc/my.cnf
Yuval Brik13e81ad2017-06-23 10:32:16 +030078 local cracklib_conf=/etc/my.cnf.d/cracklib_password_check.cnf
79 if [ -f "$cracklib_conf" ]; then
80 inicomment -sudo "$cracklib_conf" "mariadb" "plugin-load-add"
81 fi
Vincent Untz00011c02012-12-06 09:56:32 +010082 else
83 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040084 fi
85
86 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010087 if is_fedora || is_suse; then
88 # service is not started by default
Dirk Mueller1d968d72017-09-23 14:45:42 +020089 start_service $MYSQL_SERVICE_NAME
Vincent Untz00011c02012-12-06 09:56:32 +010090 fi
91
92 # Set the root password - only works the first time. For Ubuntu, we already
Jens Rosenboom9abb26d2016-12-07 21:12:55 +010093 # did that with debconf before installing the package, but we still try,
94 # because the package might have been installed already.
95 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
Vincent Untz00011c02012-12-06 09:56:32 +010096
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +020097 # In case of Mariadb, giving hostname in arguments causes permission
98 # problems as it expects connection through socket
99 if is_ubuntu && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
100 local cmd_args="-uroot -p$DATABASE_PASSWORD "
101 else
102 local cmd_args="-uroot -p$DATABASE_PASSWORD -h127.0.0.1 "
103 fi
104
105 # In mariadb e.g. on Ubuntu socket plugin is used for authentication
106 # as root so it works only as sudo. To restore old "mysql like" behaviour,
107 # we need to change auth plugin for root user
YAMAMOTO Takashiede8b122019-12-09 14:21:21 +0900108 if is_ubuntu && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +0200109 sudo mysql $cmd_args -e "UPDATE mysql.user SET plugin='' WHERE user='$DATABASE_USER' AND host='localhost';"
110 sudo mysql $cmd_args -e "FLUSH PRIVILEGES;"
111 fi
Dr. Jens Harbott08d84bc2020-02-12 10:07:36 +0000112 # Create DB user if it does not already exist
113 sudo mysql $cmd_args -e "CREATE USER IF NOT EXISTS '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
Marian Horbanea21eb42015-08-18 06:57:18 -0400114 # Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
Dr. Jens Harbott08d84bc2020-02-12 10:07:36 +0000115 sudo mysql $cmd_args -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%';"
Terry Wilson428af5a2012-11-01 16:12:39 -0400116
117 # Now update ``my.cnf`` for some local needs and restart the mysql service
118
Marian Horbanea21eb42015-08-18 06:57:18 -0400119 # Change bind-address from localhost (127.0.0.1) to any (::) and
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200120 # set default db type to InnoDB
Jens Harbottdc7b4292017-09-19 10:52:32 +0000121 iniset -sudo $my_conf mysqld bind-address "$(ipv6_unquote $SERVICE_LISTEN_ADDRESS)"
Roman Podoliaka88b84092017-02-07 13:34:12 +0200122 iniset -sudo $my_conf mysqld sql_mode TRADITIONAL
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000123 iniset -sudo $my_conf mysqld default-storage-engine InnoDB
Jens Rosenboom4b59fbb2017-03-15 21:58:48 +0000124 iniset -sudo $my_conf mysqld max_connections 1024
Terry Wilson428af5a2012-11-01 16:12:39 -0400125
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000126 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
127 echo_summary "Enabling MySQL query logging"
Attila Fazekas1f316be2015-01-26 16:39:57 +0100128 if is_fedora; then
Attila Fazekas3b53aeb2014-04-30 11:57:22 +0200129 slow_log=/var/log/mariadb/mariadb-slow.log
130 else
131 slow_log=/var/log/mysql/mysql-slow.log
132 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200133 sudo sed -e '/log.slow.queries/d' \
134 -e '/long.query.time/d' \
135 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500136 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000137
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200138 # Turn on slow query log, log all queries (any query taking longer than
139 # 0 seconds) and log all non-indexed queries
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000140 iniset -sudo $my_conf mysqld slow-query-log 1
141 iniset -sudo $my_conf mysqld slow-query-log-file $slow_log
142 iniset -sudo $my_conf mysqld long-query-time 0
143 iniset -sudo $my_conf mysqld log-queries-not-using-indexes 1
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000144 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000145
Dirk Mueller1d968d72017-09-23 14:45:42 +0200146 restart_service $MYSQL_SERVICE_NAME
Terry Wilson428af5a2012-11-01 16:12:39 -0400147}
148
149function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100150 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400151 # Seed configuration with mysql password so that apt-get install doesn't
152 # prompt us for a password upon install.
Bob Ball90333432015-01-19 10:56:42 +0000153 sudo debconf-set-selections <<MYSQL_PRESEED
154mysql-server mysql-server/root_password password $DATABASE_PASSWORD
155mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD
156mysql-server mysql-server/start_on_boot boolean true
Terry Wilson428af5a2012-11-01 16:12:39 -0400157MYSQL_PRESEED
158 fi
159
160 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
161 # as it allows you to access the mysql databases via ``mysql nova`` instead
162 # of having to specify the username/password each time.
163 if [[ ! -e $HOME/.my.cnf ]]; then
164 cat <<EOF >$HOME/.my.cnf
165[client]
166user=$DATABASE_USER
167password=$DATABASE_PASSWORD
Terry Wilson428af5a2012-11-01 16:12:39 -0400168EOF
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +0200169
170 if ! is_ubuntu || [ "$MYSQL_SERVICE_NAME" != "mariadb" ]; then
171 echo "host=$MYSQL_HOST" >> $HOME/.my.cnf
172 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400173 chmod 0600 $HOME/.my.cnf
174 fi
175 # Install mysql-server
Dirk Mueller1d968d72017-09-23 14:45:42 +0200176 if is_oraclelinux; then
177 install_package mysql-community-server
Ian Wienand812e7842020-04-17 09:25:22 +1000178 elif is_fedora; then
179 install_package mariadb-server mariadb-devel
180 sudo systemctl enable $MYSQL_SERVICE_NAME
181 elif is_suse; then
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700182 install_package mariadb-server
Dirk Mueller1d968d72017-09-23 14:45:42 +0200183 sudo systemctl enable $MYSQL_SERVICE_NAME
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700184 elif is_ubuntu; then
Slawek Kaplonskid54a1c62019-09-10 12:05:06 +0200185 install_package $MYSQL_SERVICE_NAME-server
Vincent Untzca5c4712012-11-21 17:45:49 +0100186 else
Vincent Untz00011c02012-12-06 09:56:32 +0100187 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100188 fi
Dean Troyer5686dbc2015-03-09 14:27:51 -0500189}
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600190
Dean Troyer5686dbc2015-03-09 14:27:51 -0500191function install_database_python_mysql {
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600192 # Install Python client module
Sean Dague37421992015-05-20 06:37:11 -0700193 pip_install_gr $MYSQL_DRIVER
194 if [[ "$MYSQL_DRIVER" == "MySQL-python" ]]; then
195 ADDITIONAL_VENV_PACKAGES+=",MySQL-python"
Julien Danjou0f63eb32015-06-12 09:05:12 +0200196 elif [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
197 ADDITIONAL_VENV_PACKAGES+=",PyMySQL"
Sean Dague37421992015-05-20 06:37:11 -0700198 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400199}
200
201function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100202 local db=$1
203 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400204}
205
Dean Troyercc6b4432013-04-08 15:38:03 -0500206
Terry Wilson428af5a2012-11-01 16:12:39 -0400207# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100208$_XTRACE_DB_MYSQL
Sean Dague584d90e2013-03-29 14:34:53 -0400209
210# Local variables:
211# mode: shell-script
212# End: