blob: 0a96cf8af4f26b625623ea9cfdda614974df96fb [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 {
50 echo_summary "Configuring and starting MySQL"
51
Vincent Untzc18b9652012-12-04 12:36:34 +010052 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040053 MY_CONF=/etc/mysql/my.cnf
54 MYSQL=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010055 elif is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +010056 if [[ $DISTRO =~ (rhel7) ]]; then
57 MYSQL=mariadb
58 else
59 MYSQL=mysqld
60 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040061 MY_CONF=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010062 elif is_suse; then
63 MY_CONF=/etc/my.cnf
64 MYSQL=mysql
65 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
Terry Wilson428af5a2012-11-01 16:12:39 -040072 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 && \
89 iniset $MY_CONF mysqld bind-address 0.0.0.0 && \
90 iniset $MY_CONF mysqld default-storage-engine InnoDB"
Terry Wilson428af5a2012-11-01 16:12:39 -040091
Terry Wilson428af5a2012-11-01 16:12:39 -040092
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000093 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
94 echo_summary "Enabling MySQL query logging"
Monty Taylor7c73e8d2013-01-07 08:17:01 +000095
Ralf Haferkamp0526bb82014-04-03 08:27:33 +020096 sudo sed -e '/log.slow.queries/d' \
97 -e '/long.query.time/d' \
98 -e '/log.queries.not.using.indexes/d' \
99 -i $MY_CONF
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000100
Ralf Haferkamp0526bb82014-04-03 08:27:33 +0200101 # Turn on slow query log, log all queries (any query taking longer than
102 # 0 seconds) and log all non-indexed queries
103 sudo bash -c "source $TOP_DIR/functions && \
104 iniset $MY_CONF mysqld slow-query-log 1 && \
105 iniset $MY_CONF mysqld slow-query-log-file /var/log/mysql/mysql-slow.log && \
106 iniset $MY_CONF mysqld long-query-time 0 && \
107 iniset $MY_CONF mysqld log-queries-not-using-indexes 1"
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000108
109 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000110
Terry Wilson428af5a2012-11-01 16:12:39 -0400111 restart_service $MYSQL
112}
113
114function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100115 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400116 # Seed configuration with mysql password so that apt-get install doesn't
117 # prompt us for a password upon install.
118 cat <<MYSQL_PRESEED | sudo debconf-set-selections
119mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
120mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
121mysql-server-5.1 mysql-server/start_on_boot boolean true
122MYSQL_PRESEED
123 fi
124
125 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
126 # as it allows you to access the mysql databases via ``mysql nova`` instead
127 # of having to specify the username/password each time.
128 if [[ ! -e $HOME/.my.cnf ]]; then
129 cat <<EOF >$HOME/.my.cnf
130[client]
131user=$DATABASE_USER
132password=$DATABASE_PASSWORD
133host=$DATABASE_HOST
134EOF
135 chmod 0600 $HOME/.my.cnf
136 fi
137 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100138 if is_ubuntu || is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +0100139 if [[ $DISTRO =~ (rhel7) ]]; then
140 install_package mariadb-server
141 else
142 install_package mysql-server
143 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100144 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200145 if ! is_package_installed mariadb; then
146 install_package mysql-community-server
147 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100148 else
Vincent Untz00011c02012-12-06 09:56:32 +0100149 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100150 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400151}
152
153function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100154 local db=$1
155 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400156}
157
Dean Troyercc6b4432013-04-08 15:38:03 -0500158
Terry Wilson428af5a2012-11-01 16:12:39 -0400159# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600160$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400161
162# Local variables:
163# mode: shell-script
164# End: