blob: 70073c4c6f5230c79f19eedcf47bd39fd170758a [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
Dean Troyercc6b4432013-04-08 15:38:03 -050014
Terry Wilson428af5a2012-11-01 16:12:39 -040015register_database mysql
16
Sean Dague53753292014-12-04 19:38:15 -050017# Linux distros, thank you for being incredibly consistent
18MYSQL=mysql
19if is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010020 MYSQL=mariadb
Sean Dague53753292014-12-04 19:38:15 -050021fi
Dean Troyercc6b4432013-04-08 15:38:03 -050022
23# Functions
24# ---------
25
Dean Troyer995eb922013-03-07 16:11:40 -060026# Get rid of everything enough to cleanly change database backends
27function cleanup_database_mysql {
Sean Dague53753292014-12-04 19:38:15 -050028 stop_service $MYSQL
Dean Troyer995eb922013-03-07 16:11:40 -060029 if is_ubuntu; then
30 # Get ruthless with mysql
Sean Dague8f90f762015-01-14 10:36:48 -050031 apt_get purge -y mysql* mariadb*
Dean Troyer995eb922013-03-07 16:11:40 -060032 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030033 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060034 return
35 elif is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010036 uninstall_package mariadb-server
37 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060038 elif is_suse; then
Sean Dague1cbb5d32014-12-15 14:57:15 -050039 uninstall_package mysql-community-server
40 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060041 else
42 return
43 fi
Dean Troyer995eb922013-03-07 16:11:40 -060044}
45
Terry Wilson428af5a2012-11-01 16:12:39 -040046function recreate_database_mysql {
47 local db=$1
zhhuabj2832f282013-05-08 18:43:26 +080048 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020049 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
Terry Wilson428af5a2012-11-01 16:12:39 -040050}
51
52function configure_database_mysql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050053 local my_conf mysql slow_log
Terry Wilson428af5a2012-11-01 16:12:39 -040054 echo_summary "Configuring and starting MySQL"
55
Vincent Untzc18b9652012-12-04 12:36:34 +010056 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050057 my_conf=/etc/mysql/my.cnf
58 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010059 elif is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010060 mysql=mariadb
Dean Troyer3ef23bc2014-07-25 14:56:22 -050061 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010062 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050063 my_conf=/etc/my.cnf
64 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010065 else
66 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040067 fi
68
69 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010070 if is_fedora || is_suse; then
71 # service is not started by default
Dean Troyer3ef23bc2014-07-25 14:56:22 -050072 start_service $mysql
Vincent Untz00011c02012-12-06 09:56:32 +010073 fi
74
75 # Set the root password - only works the first time. For Ubuntu, we already
76 # did that with debconf before installing the package.
77 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040078 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
79 fi
Vincent Untz00011c02012-12-06 09:56:32 +010080
Terry Wilson428af5a2012-11-01 16:12:39 -040081 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
82 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
83
84 # Now update ``my.cnf`` for some local needs and restart the mysql service
85
Ralf Haferkamp0526bb82014-04-03 08:27:33 +020086 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0) and
87 # set default db type to InnoDB
88 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -050089 iniset $my_conf mysqld bind-address 0.0.0.0 && \
90 iniset $my_conf mysqld sql_mode STRICT_ALL_TABLES && \
91 iniset $my_conf mysqld default-storage-engine InnoDB"
Terry Wilson428af5a2012-11-01 16:12:39 -040092
Terry Wilson428af5a2012-11-01 16:12:39 -040093
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000094 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
95 echo_summary "Enabling MySQL query logging"
Attila Fazekas1f316be2015-01-26 16:39:57 +010096 if is_fedora; then
Attila Fazekas3b53aeb2014-04-30 11:57:22 +020097 slow_log=/var/log/mariadb/mariadb-slow.log
98 else
99 slow_log=/var/log/mysql/mysql-slow.log
100 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200101 sudo sed -e '/log.slow.queries/d' \
102 -e '/long.query.time/d' \
103 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500104 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000105
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200106 # Turn on slow query log, log all queries (any query taking longer than
107 # 0 seconds) and log all non-indexed queries
108 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500109 iniset $my_conf mysqld slow-query-log 1 && \
110 iniset $my_conf mysqld slow-query-log-file $slow_log && \
111 iniset $my_conf mysqld long-query-time 0 && \
112 iniset $my_conf mysqld log-queries-not-using-indexes 1"
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000113
114 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000115
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500116 restart_service $mysql
Terry Wilson428af5a2012-11-01 16:12:39 -0400117}
118
119function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100120 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400121 # Seed configuration with mysql password so that apt-get install doesn't
122 # prompt us for a password upon install.
Bob Ball90333432015-01-19 10:56:42 +0000123 sudo debconf-set-selections <<MYSQL_PRESEED
124mysql-server mysql-server/root_password password $DATABASE_PASSWORD
125mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD
126mysql-server mysql-server/start_on_boot boolean true
Terry Wilson428af5a2012-11-01 16:12:39 -0400127MYSQL_PRESEED
128 fi
129
130 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
131 # as it allows you to access the mysql databases via ``mysql nova`` instead
132 # of having to specify the username/password each time.
133 if [[ ! -e $HOME/.my.cnf ]]; then
134 cat <<EOF >$HOME/.my.cnf
135[client]
136user=$DATABASE_USER
137password=$DATABASE_PASSWORD
138host=$DATABASE_HOST
139EOF
140 chmod 0600 $HOME/.my.cnf
141 fi
142 # Install mysql-server
pcrews6de7dba2014-11-18 20:50:00 -0800143 if is_fedora; then
Attila Fazekas1f316be2015-01-26 16:39:57 +0100144 install_package mariadb-server
pcrews6de7dba2014-11-18 20:50:00 -0800145 elif is_ubuntu; then
146 install_package mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100147 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200148 if ! is_package_installed mariadb; then
149 install_package mysql-community-server
150 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100151 else
Vincent Untz00011c02012-12-06 09:56:32 +0100152 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100153 fi
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600154
155 # Install Python client module
156 pip_install MySQL-python
Terry Wilson428af5a2012-11-01 16:12:39 -0400157}
158
159function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100160 local db=$1
161 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400162}
163
Dean Troyercc6b4432013-04-08 15:38:03 -0500164
Terry Wilson428af5a2012-11-01 16:12:39 -0400165# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600166$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400167
168# Local variables:
169# mode: shell-script
170# End: