blob: 673d867d8faea906cd21200c9bffd5b5e364eb00 [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
Dean Troyercc6b4432013-04-08 15:38:03 -050017
18# Functions
19# ---------
20
Dean Troyer995eb922013-03-07 16:11:40 -060021# Get rid of everything enough to cleanly change database backends
22function cleanup_database_mysql {
23 if is_ubuntu; then
24 # Get ruthless with mysql
25 stop_service $MYSQL
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010026 apt_get purge -y mysql*
Dean Troyer995eb922013-03-07 16:11:40 -060027 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030028 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060029 return
30 elif is_fedora; then
Pavel Sedlák6d20f092014-10-22 15:34:46 +020031 if [[ $DISTRO =~ (rhel6) ]]; then
Attila Fazekas2d650592014-02-20 15:49:13 +010032 MYSQL=mysqld
Pavel Sedlák6d20f092014-10-22 15:34:46 +020033 else
34 MYSQL=mariadb
Attila Fazekas2d650592014-02-20 15:49:13 +010035 fi
Dean Troyer995eb922013-03-07 16:11:40 -060036 elif is_suse; then
37 MYSQL=mysql
38 else
39 return
40 fi
41 stop_service $MYSQL
42}
43
Terry Wilson428af5a2012-11-01 16:12:39 -040044function recreate_database_mysql {
45 local db=$1
zhhuabj2832f282013-05-08 18:43:26 +080046 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020047 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
Terry Wilson428af5a2012-11-01 16:12:39 -040048}
49
50function configure_database_mysql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050051 local my_conf mysql slow_log
Terry Wilson428af5a2012-11-01 16:12:39 -040052 echo_summary "Configuring and starting MySQL"
53
Vincent Untzc18b9652012-12-04 12:36:34 +010054 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050055 my_conf=/etc/mysql/my.cnf
56 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010057 elif is_fedora; then
Pavel Sedlák6d20f092014-10-22 15:34:46 +020058 if [[ $DISTRO =~ (rhel6) ]]; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050059 mysql=mysqld
Pavel Sedlák6d20f092014-10-22 15:34:46 +020060 else
61 mysql=mariadb
Attila Fazekas2d650592014-02-20 15:49:13 +010062 fi
Dean Troyer3ef23bc2014-07-25 14:56:22 -050063 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010064 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050065 my_conf=/etc/my.cnf
66 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010067 else
68 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040069 fi
70
71 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010072 if is_fedora || is_suse; then
73 # service is not started by default
Dean Troyer3ef23bc2014-07-25 14:56:22 -050074 start_service $mysql
Vincent Untz00011c02012-12-06 09:56:32 +010075 fi
76
77 # Set the root password - only works the first time. For Ubuntu, we already
78 # did that with debconf before installing the package.
79 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040080 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
81 fi
Vincent Untz00011c02012-12-06 09:56:32 +010082
Terry Wilson428af5a2012-11-01 16:12:39 -040083 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
84 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
85
86 # Now update ``my.cnf`` for some local needs and restart the mysql service
87
Ralf Haferkamp0526bb82014-04-03 08:27:33 +020088 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0) and
89 # set default db type to InnoDB
90 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -050091 iniset $my_conf mysqld bind-address 0.0.0.0 && \
92 iniset $my_conf mysqld sql_mode STRICT_ALL_TABLES && \
93 iniset $my_conf mysqld default-storage-engine InnoDB"
Terry Wilson428af5a2012-11-01 16:12:39 -040094
Terry Wilson428af5a2012-11-01 16:12:39 -040095
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000096 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
97 echo_summary "Enabling MySQL query logging"
Attila Fazekas3b53aeb2014-04-30 11:57:22 +020098 if is_fedora && ! [[ $DISTRO =~ (rhel6) ]]; then
99 slow_log=/var/log/mariadb/mariadb-slow.log
100 else
101 slow_log=/var/log/mysql/mysql-slow.log
102 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200103 sudo sed -e '/log.slow.queries/d' \
104 -e '/long.query.time/d' \
105 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500106 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000107
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200108 # Turn on slow query log, log all queries (any query taking longer than
109 # 0 seconds) and log all non-indexed queries
110 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500111 iniset $my_conf mysqld slow-query-log 1 && \
112 iniset $my_conf mysqld slow-query-log-file $slow_log && \
113 iniset $my_conf mysqld long-query-time 0 && \
114 iniset $my_conf mysqld log-queries-not-using-indexes 1"
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000115
116 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000117
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500118 restart_service $mysql
Terry Wilson428af5a2012-11-01 16:12:39 -0400119}
120
121function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100122 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400123 # Seed configuration with mysql password so that apt-get install doesn't
124 # prompt us for a password upon install.
125 cat <<MYSQL_PRESEED | sudo debconf-set-selections
126mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
127mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
128mysql-server-5.1 mysql-server/start_on_boot boolean true
129MYSQL_PRESEED
130 fi
131
132 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
133 # as it allows you to access the mysql databases via ``mysql nova`` instead
134 # of having to specify the username/password each time.
135 if [[ ! -e $HOME/.my.cnf ]]; then
136 cat <<EOF >$HOME/.my.cnf
137[client]
138user=$DATABASE_USER
139password=$DATABASE_PASSWORD
140host=$DATABASE_HOST
141EOF
142 chmod 0600 $HOME/.my.cnf
143 fi
144 # Install mysql-server
pcrews6de7dba2014-11-18 20:50:00 -0800145 if is_fedora; then
146 if [[ $DISTRO =~ (rhel6) ]]; then
Attila Fazekas2d650592014-02-20 15:49:13 +0100147 install_package mysql-server
Pavel Sedlák6d20f092014-10-22 15:34:46 +0200148 else
149 install_package mariadb-server
Attila Fazekas2d650592014-02-20 15:49:13 +0100150 fi
pcrews6de7dba2014-11-18 20:50:00 -0800151 elif is_ubuntu; then
152 install_package mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100153 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200154 if ! is_package_installed mariadb; then
155 install_package mysql-community-server
156 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100157 else
Vincent Untz00011c02012-12-06 09:56:32 +0100158 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100159 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400160}
161
162function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100163 local db=$1
164 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400165}
166
Dean Troyercc6b4432013-04-08 15:38:03 -0500167
Terry Wilson428af5a2012-11-01 16:12:39 -0400168# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600169$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400170
171# Local variables:
172# mode: shell-script
173# End: