blob: 4d0f5f3e452b5c40a060245070c5ded9bcd9a05e [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
Dirk Mueller1d968d72017-09-23 14:45:42 +020018MYSQL_SERVICE_NAME=mysql
Adam Spiersbc2a88d2019-01-24 18:57:33 +000019if is_fedora && ! is_oraclelinux; then
20 MYSQL_SERVICE_NAME=mariadb
21elif is_suse && systemctl list-unit-files | grep -q 'mariadb\.service'; then
22 # Older mariadb packages on SLES 12 provided mysql.service. The
23 # newer ones on SLES 12 and 15 use mariadb.service; they also
24 # provide a mysql.service symlink for backwards-compatibility, but
25 # let's not rely on that.
Dirk Mueller1d968d72017-09-23 14:45:42 +020026 MYSQL_SERVICE_NAME=mariadb
Sean Dague53753292014-12-04 19:38:15 -050027fi
Dean Troyercc6b4432013-04-08 15:38:03 -050028
29# Functions
30# ---------
31
Julien Danjou0eec4f82015-09-08 10:45:06 +000032function get_database_type_mysql {
33 if [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
34 echo mysql+pymysql
35 else
36 echo mysql
37 fi
38}
39
Dean Troyer995eb922013-03-07 16:11:40 -060040# Get rid of everything enough to cleanly change database backends
41function cleanup_database_mysql {
Dirk Mueller1d968d72017-09-23 14:45:42 +020042 stop_service $MYSQL_SERVICE_NAME
Dean Troyer995eb922013-03-07 16:11:40 -060043 if is_ubuntu; then
44 # Get ruthless with mysql
Sean Dague8f90f762015-01-14 10:36:48 -050045 apt_get purge -y mysql* mariadb*
Dean Troyer995eb922013-03-07 16:11:40 -060046 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030047 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060048 return
Dirk Mueller1d968d72017-09-23 14:45:42 +020049 elif is_oraclelinux; then
Wiekus Beukesec47bc12015-03-19 08:20:38 -070050 uninstall_package mysql-community-server
51 sudo rm -rf /var/lib/mysql
Dirk Mueller1d968d72017-09-23 14:45:42 +020052 elif is_suse || is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010053 uninstall_package mariadb-server
54 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060055 else
56 return
57 fi
Dean Troyer995eb922013-03-07 16:11:40 -060058}
59
Terry Wilson428af5a2012-11-01 16:12:39 -040060function recreate_database_mysql {
61 local db=$1
zhhuabj2832f282013-05-08 18:43:26 +080062 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020063 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
Terry Wilson428af5a2012-11-01 16:12:39 -040064}
65
66function configure_database_mysql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050067 local my_conf mysql slow_log
Terry Wilson428af5a2012-11-01 16:12:39 -040068 echo_summary "Configuring and starting MySQL"
69
Vincent Untzc18b9652012-12-04 12:36:34 +010070 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050071 my_conf=/etc/mysql/my.cnf
Wiekus Beukesec47bc12015-03-19 08:20:38 -070072 elif is_suse || is_oraclelinux; then
73 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010074 elif is_fedora; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050075 my_conf=/etc/my.cnf
Yuval Brik13e81ad2017-06-23 10:32:16 +030076 local cracklib_conf=/etc/my.cnf.d/cracklib_password_check.cnf
77 if [ -f "$cracklib_conf" ]; then
78 inicomment -sudo "$cracklib_conf" "mariadb" "plugin-load-add"
79 fi
Vincent Untz00011c02012-12-06 09:56:32 +010080 else
81 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040082 fi
83
84 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010085 if is_fedora || is_suse; then
86 # service is not started by default
Dirk Mueller1d968d72017-09-23 14:45:42 +020087 start_service $MYSQL_SERVICE_NAME
Vincent Untz00011c02012-12-06 09:56:32 +010088 fi
89
90 # Set the root password - only works the first time. For Ubuntu, we already
Jens Rosenboom9abb26d2016-12-07 21:12:55 +010091 # did that with debconf before installing the package, but we still try,
92 # because the package might have been installed already.
93 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
Vincent Untz00011c02012-12-06 09:56:32 +010094
Marian Horbanea21eb42015-08-18 06:57:18 -040095 # Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
Terry Wilson428af5a2012-11-01 16:12:39 -040096 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
97
98 # Now update ``my.cnf`` for some local needs and restart the mysql service
99
Marian Horbanea21eb42015-08-18 06:57:18 -0400100 # Change bind-address from localhost (127.0.0.1) to any (::) and
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200101 # set default db type to InnoDB
Jens Harbottdc7b4292017-09-19 10:52:32 +0000102 iniset -sudo $my_conf mysqld bind-address "$(ipv6_unquote $SERVICE_LISTEN_ADDRESS)"
Roman Podoliaka88b84092017-02-07 13:34:12 +0200103 iniset -sudo $my_conf mysqld sql_mode TRADITIONAL
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000104 iniset -sudo $my_conf mysqld default-storage-engine InnoDB
Jens Rosenboom4b59fbb2017-03-15 21:58:48 +0000105 iniset -sudo $my_conf mysqld max_connections 1024
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000106 iniset -sudo $my_conf mysqld query_cache_type OFF
107 iniset -sudo $my_conf mysqld query_cache_size 0
Terry Wilson428af5a2012-11-01 16:12:39 -0400108
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000109 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
110 echo_summary "Enabling MySQL query logging"
Attila Fazekas1f316be2015-01-26 16:39:57 +0100111 if is_fedora; then
Attila Fazekas3b53aeb2014-04-30 11:57:22 +0200112 slow_log=/var/log/mariadb/mariadb-slow.log
113 else
114 slow_log=/var/log/mysql/mysql-slow.log
115 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200116 sudo sed -e '/log.slow.queries/d' \
117 -e '/long.query.time/d' \
118 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500119 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000120
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200121 # Turn on slow query log, log all queries (any query taking longer than
122 # 0 seconds) and log all non-indexed queries
Ian Wienand9c0b9f32015-07-22 06:08:09 +1000123 iniset -sudo $my_conf mysqld slow-query-log 1
124 iniset -sudo $my_conf mysqld slow-query-log-file $slow_log
125 iniset -sudo $my_conf mysqld long-query-time 0
126 iniset -sudo $my_conf mysqld log-queries-not-using-indexes 1
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000127 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000128
Dirk Mueller1d968d72017-09-23 14:45:42 +0200129 restart_service $MYSQL_SERVICE_NAME
Terry Wilson428af5a2012-11-01 16:12:39 -0400130}
131
132function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100133 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400134 # Seed configuration with mysql password so that apt-get install doesn't
135 # prompt us for a password upon install.
Bob Ball90333432015-01-19 10:56:42 +0000136 sudo debconf-set-selections <<MYSQL_PRESEED
137mysql-server mysql-server/root_password password $DATABASE_PASSWORD
138mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD
139mysql-server mysql-server/start_on_boot boolean true
Terry Wilson428af5a2012-11-01 16:12:39 -0400140MYSQL_PRESEED
141 fi
142
143 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
144 # as it allows you to access the mysql databases via ``mysql nova`` instead
145 # of having to specify the username/password each time.
146 if [[ ! -e $HOME/.my.cnf ]]; then
147 cat <<EOF >$HOME/.my.cnf
148[client]
149user=$DATABASE_USER
150password=$DATABASE_PASSWORD
Johan Pas199d8572015-11-17 00:56:25 +0100151host=$MYSQL_HOST
Terry Wilson428af5a2012-11-01 16:12:39 -0400152EOF
153 chmod 0600 $HOME/.my.cnf
154 fi
155 # Install mysql-server
Dirk Mueller1d968d72017-09-23 14:45:42 +0200156 if is_oraclelinux; then
157 install_package mysql-community-server
158 elif is_fedora || is_suse; then
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700159 install_package mariadb-server
Dirk Mueller1d968d72017-09-23 14:45:42 +0200160 sudo systemctl enable $MYSQL_SERVICE_NAME
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700161 elif is_ubuntu; then
162 install_package mysql-server
Vincent Untzca5c4712012-11-21 17:45:49 +0100163 else
Vincent Untz00011c02012-12-06 09:56:32 +0100164 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100165 fi
Dean Troyer5686dbc2015-03-09 14:27:51 -0500166}
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600167
Dean Troyer5686dbc2015-03-09 14:27:51 -0500168function install_database_python_mysql {
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600169 # Install Python client module
Sean Dague37421992015-05-20 06:37:11 -0700170 pip_install_gr $MYSQL_DRIVER
171 if [[ "$MYSQL_DRIVER" == "MySQL-python" ]]; then
172 ADDITIONAL_VENV_PACKAGES+=",MySQL-python"
Julien Danjou0f63eb32015-06-12 09:05:12 +0200173 elif [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
174 ADDITIONAL_VENV_PACKAGES+=",PyMySQL"
Sean Dague37421992015-05-20 06:37:11 -0700175 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400176}
177
178function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100179 local db=$1
180 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400181}
182
Dean Troyercc6b4432013-04-08 15:38:03 -0500183
Terry Wilson428af5a2012-11-01 16:12:39 -0400184# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100185$_XTRACE_DB_MYSQL
Sean Dague584d90e2013-03-29 14:34:53 -0400186
187# Local variables:
188# mode: shell-script
189# End: