blob: 9c9401edf655bf41f38e0355a966316f1c10df66 [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
Dean Troyer41bf4522013-01-28 14:04:39 -060011MY_XTRACE=$(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}
Sean Dague37421992015-05-20 06:37:11 -070015# Force over to pymysql driver by default if we are using it.
16if is_service_enabled mysql; then
17 if [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
18 SQLALCHEMY_DATABASE_DRIVER=${SQLALCHEMY_DATABASE_DRIVER:-"pymysql"}
19 fi
20fi
Dean Troyercc6b4432013-04-08 15:38:03 -050021
Terry Wilson428af5a2012-11-01 16:12:39 -040022register_database mysql
23
Sean Dague53753292014-12-04 19:38:15 -050024# Linux distros, thank you for being incredibly consistent
25MYSQL=mysql
Wiekus Beukesec47bc12015-03-19 08:20:38 -070026if is_fedora && ! is_oraclelinux; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010027 MYSQL=mariadb
Sean Dague53753292014-12-04 19:38:15 -050028fi
Dean Troyercc6b4432013-04-08 15:38:03 -050029
30# Functions
31# ---------
32
Dean Troyer995eb922013-03-07 16:11:40 -060033# Get rid of everything enough to cleanly change database backends
34function cleanup_database_mysql {
Sean Dague53753292014-12-04 19:38:15 -050035 stop_service $MYSQL
Dean Troyer995eb922013-03-07 16:11:40 -060036 if is_ubuntu; then
37 # Get ruthless with mysql
Sean Dague8f90f762015-01-14 10:36:48 -050038 apt_get purge -y mysql* mariadb*
Dean Troyer995eb922013-03-07 16:11:40 -060039 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030040 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060041 return
Wiekus Beukesec47bc12015-03-19 08:20:38 -070042 elif is_suse || is_oraclelinux; then
43 uninstall_package mysql-community-server
44 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060045 elif is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010046 uninstall_package mariadb-server
47 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060048 else
49 return
50 fi
Dean Troyer995eb922013-03-07 16:11:40 -060051}
52
Terry Wilson428af5a2012-11-01 16:12:39 -040053function recreate_database_mysql {
54 local db=$1
zhhuabj2832f282013-05-08 18:43:26 +080055 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020056 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
Terry Wilson428af5a2012-11-01 16:12:39 -040057}
58
59function configure_database_mysql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050060 local my_conf mysql slow_log
Terry Wilson428af5a2012-11-01 16:12:39 -040061 echo_summary "Configuring and starting MySQL"
62
Vincent Untzc18b9652012-12-04 12:36:34 +010063 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050064 my_conf=/etc/mysql/my.cnf
65 mysql=mysql
Wiekus Beukesec47bc12015-03-19 08:20:38 -070066 elif is_suse || is_oraclelinux; then
67 my_conf=/etc/my.cnf
68 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010069 elif is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010070 mysql=mariadb
Dean Troyer3ef23bc2014-07-25 14:56:22 -050071 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010072 else
73 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040074 fi
75
76 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010077 if is_fedora || is_suse; then
78 # service is not started by default
Dean Troyer3ef23bc2014-07-25 14:56:22 -050079 start_service $mysql
Vincent Untz00011c02012-12-06 09:56:32 +010080 fi
81
82 # Set the root password - only works the first time. For Ubuntu, we already
83 # did that with debconf before installing the package.
84 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040085 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
86 fi
Vincent Untz00011c02012-12-06 09:56:32 +010087
Terry Wilson428af5a2012-11-01 16:12:39 -040088 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
89 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
90
91 # Now update ``my.cnf`` for some local needs and restart the mysql service
92
Brian Haley180f5eb2015-06-16 13:14:31 -040093 # Change ‘bind-address’ from localhost (127.0.0.1) to any (::) and
Ralf Haferkamp0526bb82014-04-03 08:27:33 +020094 # set default db type to InnoDB
95 sudo bash -c "source $TOP_DIR/functions && \
Brian Haley180f5eb2015-06-16 13:14:31 -040096 iniset $my_conf mysqld bind-address "$SERVICE_LISTEN_ADDRESS" && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -050097 iniset $my_conf mysqld sql_mode STRICT_ALL_TABLES && \
Clint Byrumd16bfa42015-06-18 13:22:35 -070098 iniset $my_conf mysqld default-storage-engine InnoDB \
99 iniset $my_conf mysqld max_connections 1024 \
100 iniset $my_conf mysqld query_cache_type OFF \
101 iniset $my_conf mysqld query_cache_size 0"
Terry Wilson428af5a2012-11-01 16:12:39 -0400102
Terry Wilson428af5a2012-11-01 16:12:39 -0400103
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000104 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
105 echo_summary "Enabling MySQL query logging"
Attila Fazekas1f316be2015-01-26 16:39:57 +0100106 if is_fedora; then
Attila Fazekas3b53aeb2014-04-30 11:57:22 +0200107 slow_log=/var/log/mariadb/mariadb-slow.log
108 else
109 slow_log=/var/log/mysql/mysql-slow.log
110 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200111 sudo sed -e '/log.slow.queries/d' \
112 -e '/long.query.time/d' \
113 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500114 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000115
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200116 # Turn on slow query log, log all queries (any query taking longer than
117 # 0 seconds) and log all non-indexed queries
118 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500119 iniset $my_conf mysqld slow-query-log 1 && \
120 iniset $my_conf mysqld slow-query-log-file $slow_log && \
121 iniset $my_conf mysqld long-query-time 0 && \
122 iniset $my_conf mysqld log-queries-not-using-indexes 1"
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000123
124 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000125
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500126 restart_service $mysql
Terry Wilson428af5a2012-11-01 16:12:39 -0400127}
128
129function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100130 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400131 # Seed configuration with mysql password so that apt-get install doesn't
132 # prompt us for a password upon install.
Bob Ball90333432015-01-19 10:56:42 +0000133 sudo debconf-set-selections <<MYSQL_PRESEED
134mysql-server mysql-server/root_password password $DATABASE_PASSWORD
135mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD
136mysql-server mysql-server/start_on_boot boolean true
Terry Wilson428af5a2012-11-01 16:12:39 -0400137MYSQL_PRESEED
138 fi
139
140 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
141 # as it allows you to access the mysql databases via ``mysql nova`` instead
142 # of having to specify the username/password each time.
143 if [[ ! -e $HOME/.my.cnf ]]; then
144 cat <<EOF >$HOME/.my.cnf
145[client]
146user=$DATABASE_USER
147password=$DATABASE_PASSWORD
148host=$DATABASE_HOST
149EOF
150 chmod 0600 $HOME/.my.cnf
151 fi
152 # Install mysql-server
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700153 if is_suse || is_oraclelinux; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200154 if ! is_package_installed mariadb; then
155 install_package mysql-community-server
156 fi
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700157 elif is_fedora; then
158 install_package mariadb-server
159 elif is_ubuntu; then
160 install_package mysql-server
Vincent Untzca5c4712012-11-21 17:45:49 +0100161 else
Vincent Untz00011c02012-12-06 09:56:32 +0100162 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100163 fi
Dean Troyer5686dbc2015-03-09 14:27:51 -0500164}
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600165
Dean Troyer5686dbc2015-03-09 14:27:51 -0500166function install_database_python_mysql {
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600167 # Install Python client module
Sean Dague37421992015-05-20 06:37:11 -0700168 pip_install_gr $MYSQL_DRIVER
169 if [[ "$MYSQL_DRIVER" == "MySQL-python" ]]; then
170 ADDITIONAL_VENV_PACKAGES+=",MySQL-python"
Julien Danjou0f63eb32015-06-12 09:05:12 +0200171 elif [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
172 ADDITIONAL_VENV_PACKAGES+=",PyMySQL"
Sean Dague37421992015-05-20 06:37:11 -0700173 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400174}
175
176function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100177 local db=$1
178 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400179}
180
Dean Troyercc6b4432013-04-08 15:38:03 -0500181
Terry Wilson428af5a2012-11-01 16:12:39 -0400182# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600183$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400184
185# Local variables:
186# mode: shell-script
187# End: