blob: 67bf85ac9703d9ac381ef3bcf4e558e93a92ffaa [file] [log] [blame]
Dean Troyer6d04fd72012-12-21 11:03:37 -06001# lib/databases/mysql
2# Functions to control the configuration and operation of the **MySQL** database backend
Terry Wilson428af5a2012-11-01 16:12:39 -04003
4# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# - DATABASE_{HOST,USER,PASSWORD} must be defined
Terry Wilson428af5a2012-11-01 16:12:39 -04007
8# Save trace setting
Dean Troyer41bf4522013-01-28 14:04:39 -06009MY_XTRACE=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -040010set +o xtrace
11
Dean Troyercc6b4432013-04-08 15:38:03 -050012
Terry Wilson428af5a2012-11-01 16:12:39 -040013register_database mysql
14
Dean Troyercc6b4432013-04-08 15:38:03 -050015
16# Functions
17# ---------
18
Dean Troyer995eb922013-03-07 16:11:40 -060019# Get rid of everything enough to cleanly change database backends
20function cleanup_database_mysql {
21 if is_ubuntu; then
22 # Get ruthless with mysql
23 stop_service $MYSQL
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010024 apt_get purge -y mysql*
Dean Troyer995eb922013-03-07 16:11:40 -060025 sudo rm -rf /var/lib/mysql
Tiago Mello4376ae02014-03-14 10:48:56 -030026 sudo rm -rf /etc/mysql
Dean Troyer995eb922013-03-07 16:11:40 -060027 return
28 elif is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +010029 if [[ $DISTRO =~ (rhel7) ]]; then
30 MYSQL=mariadb
31 else
32 MYSQL=mysqld
33 fi
Dean Troyer995eb922013-03-07 16:11:40 -060034 elif is_suse; then
35 MYSQL=mysql
36 else
37 return
38 fi
39 stop_service $MYSQL
40}
41
Terry Wilson428af5a2012-11-01 16:12:39 -040042function recreate_database_mysql {
43 local db=$1
44 local charset=$2
zhhuabj2832f282013-05-08 18:43:26 +080045 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
46 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET $charset;"
Terry Wilson428af5a2012-11-01 16:12:39 -040047}
48
49function configure_database_mysql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050050 local my_conf mysql slow_log
Terry Wilson428af5a2012-11-01 16:12:39 -040051 echo_summary "Configuring and starting MySQL"
52
Vincent Untzc18b9652012-12-04 12:36:34 +010053 if is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050054 my_conf=/etc/mysql/my.cnf
55 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010056 elif is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +010057 if [[ $DISTRO =~ (rhel7) ]]; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050058 mysql=mariadb
Attila Fazekas2d650592014-02-20 15:49:13 +010059 else
Dean Troyer3ef23bc2014-07-25 14:56:22 -050060 mysql=mysqld
Attila Fazekas2d650592014-02-20 15:49:13 +010061 fi
Dean Troyer3ef23bc2014-07-25 14:56:22 -050062 my_conf=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010063 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050064 my_conf=/etc/my.cnf
65 mysql=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010066 else
67 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040068 fi
69
70 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010071 if is_fedora || is_suse; then
72 # service is not started by default
Dean Troyer3ef23bc2014-07-25 14:56:22 -050073 start_service $mysql
Vincent Untz00011c02012-12-06 09:56:32 +010074 fi
75
76 # Set the root password - only works the first time. For Ubuntu, we already
77 # did that with debconf before installing the package.
78 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040079 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
80 fi
Vincent Untz00011c02012-12-06 09:56:32 +010081
Terry Wilson428af5a2012-11-01 16:12:39 -040082 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
83 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
84
85 # Now update ``my.cnf`` for some local needs and restart the mysql service
86
Ralf Haferkamp0526bb82014-04-03 08:27:33 +020087 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0) and
88 # set default db type to InnoDB
89 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -050090 iniset $my_conf mysqld bind-address 0.0.0.0 && \
91 iniset $my_conf mysqld sql_mode STRICT_ALL_TABLES && \
92 iniset $my_conf mysqld default-storage-engine InnoDB"
Terry Wilson428af5a2012-11-01 16:12:39 -040093
Terry Wilson428af5a2012-11-01 16:12:39 -040094
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000095 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
96 echo_summary "Enabling MySQL query logging"
Attila Fazekas3b53aeb2014-04-30 11:57:22 +020097 if is_fedora && ! [[ $DISTRO =~ (rhel6) ]]; then
98 slow_log=/var/log/mariadb/mariadb-slow.log
99 else
100 slow_log=/var/log/mysql/mysql-slow.log
101 fi
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200102 sudo sed -e '/log.slow.queries/d' \
103 -e '/long.query.time/d' \
104 -e '/log.queries.not.using.indexes/d' \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500105 -i $my_conf
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000106
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200107 # Turn on slow query log, log all queries (any query taking longer than
108 # 0 seconds) and log all non-indexed queries
109 sudo bash -c "source $TOP_DIR/functions && \
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500110 iniset $my_conf mysqld slow-query-log 1 && \
111 iniset $my_conf mysqld slow-query-log-file $slow_log && \
112 iniset $my_conf mysqld long-query-time 0 && \
113 iniset $my_conf mysqld log-queries-not-using-indexes 1"
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000114
115 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000116
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500117 restart_service $mysql
Terry Wilson428af5a2012-11-01 16:12:39 -0400118}
119
120function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100121 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400122 # Seed configuration with mysql password so that apt-get install doesn't
123 # prompt us for a password upon install.
124 cat <<MYSQL_PRESEED | sudo debconf-set-selections
125mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
126mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
127mysql-server-5.1 mysql-server/start_on_boot boolean true
128MYSQL_PRESEED
129 fi
130
131 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
132 # as it allows you to access the mysql databases via ``mysql nova`` instead
133 # of having to specify the username/password each time.
134 if [[ ! -e $HOME/.my.cnf ]]; then
135 cat <<EOF >$HOME/.my.cnf
136[client]
137user=$DATABASE_USER
138password=$DATABASE_PASSWORD
139host=$DATABASE_HOST
140EOF
141 chmod 0600 $HOME/.my.cnf
142 fi
143 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100144 if is_ubuntu || is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +0100145 if [[ $DISTRO =~ (rhel7) ]]; then
146 install_package mariadb-server
147 else
148 install_package mysql-server
149 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100150 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200151 if ! is_package_installed mariadb; then
152 install_package mysql-community-server
153 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100154 else
Vincent Untz00011c02012-12-06 09:56:32 +0100155 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100156 fi
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: