blob: 42004c5dcda1be987c5f9acdd7aa6d02acfef01b [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
20 if [[ $DISTRO =~ (rhel6) ]]; then
21 MYSQL=mysqld
22 else
23 MYSQL=mariadb
24 fi
25fi
Dean Troyercc6b4432013-04-08 15:38:03 -050026
27# Functions
28# ---------
29
Dean Troyer995eb922013-03-07 16:11:40 -060030# Get rid of everything enough to cleanly change database backends
31function cleanup_database_mysql {
Sean Dague53753292014-12-04 19:38:15 -050032 stop_service $MYSQL
Dean Troyer995eb922013-03-07 16:11:40 -060033 if is_ubuntu; then
34 # Get ruthless with mysql
35 stop_service $MYSQL
Moshe Levid97d2cb2015-01-12 22:47:29 +020036 uninstall_package mysql-common mariadb-common
Dean Troyer995eb922013-03-07 16:11:40 -060037 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030038 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060039 return
40 elif is_fedora; then
Pavel Sedlák6d20f092014-10-22 15:34:46 +020041 if [[ $DISTRO =~ (rhel6) ]]; then
Sean Dague1cbb5d32014-12-15 14:57:15 -050042 stop_service mysqld
43 uninstall_package mysql-server
44 sudo rm -rf /var/lib/mysql
Pavel Sedlák6d20f092014-10-22 15:34:46 +020045 else
Sean Dague1cbb5d32014-12-15 14:57:15 -050046 stop_service mariadb
47 uninstall_package mariadb-server
48 sudo rm -rf /var/lib/mysql
Attila Fazekas2d650592014-02-20 15:49:13 +010049 fi
Dean Troyer995eb922013-03-07 16:11:40 -060050 elif is_suse; then
Sean Dague1cbb5d32014-12-15 14:57:15 -050051 stop_service mysql
52 uninstall_package mysql-community-server
53 sudo rm -rf /var/lib/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060054 else
55 return
56 fi
Dean Troyer995eb922013-03-07 16:11:40 -060057}
58
Terry Wilson428af5a2012-11-01 16:12:39 -040059function recreate_database_mysql {
60 local db=$1
61 local charset=$2
zhhuabj2832f282013-05-08 18:43:26 +080062 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
63 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET $charset;"
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
72 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010073 elif is_fedora; then
Pavel Sedlák6d20f092014-10-22 15:34:46 +020074 if [[ $DISTRO =~ (rhel6) ]]; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050075 mysql=mysqld
Pavel Sedlák6d20f092014-10-22 15:34:46 +020076 else
77 mysql=mariadb
Attila Fazekas2d650592014-02-20 15:49:13 +010078 fi
Dean Troyer3ef23bc2014-07-25 14:56:22 -050079 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010080 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050081 my_conf=/etc/my.cnf
82 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010083 else
84 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040085 fi
86
87 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010088 if is_fedora || is_suse; then
89 # service is not started by default
Dean Troyer3ef23bc2014-07-25 14:56:22 -050090 start_service $mysql
Vincent Untz00011c02012-12-06 09:56:32 +010091 fi
92
93 # Set the root password - only works the first time. For Ubuntu, we already
94 # did that with debconf before installing the package.
95 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040096 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
97 fi
Vincent Untz00011c02012-12-06 09:56:32 +010098
Terry Wilson428af5a2012-11-01 16:12:39 -040099 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
100 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
101
102 # Now update ``my.cnf`` for some local needs and restart the mysql service
103
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200104 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0) and
105 # set default db type to InnoDB
106 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500107 iniset $my_conf mysqld bind-address 0.0.0.0 && \
108 iniset $my_conf mysqld sql_mode STRICT_ALL_TABLES && \
109 iniset $my_conf mysqld default-storage-engine InnoDB"
Terry Wilson428af5a2012-11-01 16:12:39 -0400110
Terry Wilson428af5a2012-11-01 16:12:39 -0400111
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000112 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
113 echo_summary "Enabling MySQL query logging"
Attila Fazekas3b53aeb2014-04-30 11:57:22 +0200114 if is_fedora && ! [[ $DISTRO =~ (rhel6) ]]; then
115 slow_log=/var/log/mariadb/mariadb-slow.log
116 else
117 slow_log=/var/log/mysql/mysql-slow.log
118 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200119 sudo sed -e '/log.slow.queries/d' \
120 -e '/long.query.time/d' \
121 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500122 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000123
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200124 # Turn on slow query log, log all queries (any query taking longer than
125 # 0 seconds) and log all non-indexed queries
126 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500127 iniset $my_conf mysqld slow-query-log 1 && \
128 iniset $my_conf mysqld slow-query-log-file $slow_log && \
129 iniset $my_conf mysqld long-query-time 0 && \
130 iniset $my_conf mysqld log-queries-not-using-indexes 1"
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000131
132 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000133
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500134 restart_service $mysql
Terry Wilson428af5a2012-11-01 16:12:39 -0400135}
136
137function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100138 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400139 # Seed configuration with mysql password so that apt-get install doesn't
140 # prompt us for a password upon install.
141 cat <<MYSQL_PRESEED | sudo debconf-set-selections
142mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
143mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
144mysql-server-5.1 mysql-server/start_on_boot boolean true
145MYSQL_PRESEED
146 fi
147
148 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
149 # as it allows you to access the mysql databases via ``mysql nova`` instead
150 # of having to specify the username/password each time.
151 if [[ ! -e $HOME/.my.cnf ]]; then
152 cat <<EOF >$HOME/.my.cnf
153[client]
154user=$DATABASE_USER
155password=$DATABASE_PASSWORD
156host=$DATABASE_HOST
157EOF
158 chmod 0600 $HOME/.my.cnf
159 fi
160 # Install mysql-server
pcrews6de7dba2014-11-18 20:50:00 -0800161 if is_fedora; then
162 if [[ $DISTRO =~ (rhel6) ]]; then
Attila Fazekas2d650592014-02-20 15:49:13 +0100163 install_package mysql-server
Pavel Sedlák6d20f092014-10-22 15:34:46 +0200164 else
165 install_package mariadb-server
Attila Fazekas2d650592014-02-20 15:49:13 +0100166 fi
pcrews6de7dba2014-11-18 20:50:00 -0800167 elif is_ubuntu; then
168 install_package mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100169 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200170 if ! is_package_installed mariadb; then
171 install_package mysql-community-server
172 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100173 else
Vincent Untz00011c02012-12-06 09:56:32 +0100174 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100175 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
Dean Troyer41bf4522013-01-28 14:04:39 -0600185$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400186
187# Local variables:
188# mode: shell-script
189# End: