blob: 476b4b91b7cebdff11e26f8ec5a11b2b5196f155 [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
24 sudo aptitude purge -y ~nmysql-server
25 sudo rm -rf /var/lib/mysql
26 return
27 elif is_fedora; then
28 MYSQL=mysqld
29 elif is_suse; then
30 MYSQL=mysql
31 else
32 return
33 fi
34 stop_service $MYSQL
35}
36
Terry Wilson428af5a2012-11-01 16:12:39 -040037function recreate_database_mysql {
38 local db=$1
39 local charset=$2
zhhuabj2832f282013-05-08 18:43:26 +080040 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
41 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET $charset;"
Terry Wilson428af5a2012-11-01 16:12:39 -040042}
43
44function configure_database_mysql {
45 echo_summary "Configuring and starting MySQL"
46
Vincent Untzc18b9652012-12-04 12:36:34 +010047 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040048 MY_CONF=/etc/mysql/my.cnf
49 MYSQL=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010050 elif is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040051 MY_CONF=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010052 MYSQL=mysqld
53 elif is_suse; then
54 MY_CONF=/etc/my.cnf
55 MYSQL=mysql
56 else
57 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040058 fi
59
60 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010061 if is_fedora || is_suse; then
62 # service is not started by default
Terry Wilson428af5a2012-11-01 16:12:39 -040063 start_service $MYSQL
Vincent Untz00011c02012-12-06 09:56:32 +010064 fi
65
66 # Set the root password - only works the first time. For Ubuntu, we already
67 # did that with debconf before installing the package.
68 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040069 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
70 fi
Vincent Untz00011c02012-12-06 09:56:32 +010071
Terry Wilson428af5a2012-11-01 16:12:39 -040072 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
73 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
74
75 # Now update ``my.cnf`` for some local needs and restart the mysql service
76
77 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0)
78 sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF
79
80 # Set default db type to InnoDB
81 if sudo grep -q "default-storage-engine" $MY_CONF; then
82 # Change it
83 sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB"
84 else
85 # Add it
86 sudo sed -i -e "/^\[mysqld\]/ a \
87default-storage-engine = InnoDB" $MY_CONF
88 fi
89
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000090 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
91 echo_summary "Enabling MySQL query logging"
Monty Taylor7c73e8d2013-01-07 08:17:01 +000092
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000093 # Turn on slow query log
94 sudo sed -i '/log.slow.queries/d' $MY_CONF
95 sudo sed -i -e "/^\[mysqld\]/ a \
96 log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF
Monty Taylor7c73e8d2013-01-07 08:17:01 +000097
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000098 # Log all queries (any query taking longer than 0 seconds)
99 sudo sed -i '/long.query.time/d' $MY_CONF
100 sudo sed -i -e "/^\[mysqld\]/ a \
101 long-query-time = 0" $MY_CONF
102
103 # Log all non-indexed queries
104 sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF
105 sudo sed -i -e "/^\[mysqld\]/ a \
106 log-queries-not-using-indexes" $MY_CONF
107
108 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000109
Terry Wilson428af5a2012-11-01 16:12:39 -0400110 restart_service $MYSQL
111}
112
113function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100114 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400115 # Seed configuration with mysql password so that apt-get install doesn't
116 # prompt us for a password upon install.
117 cat <<MYSQL_PRESEED | sudo debconf-set-selections
118mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
119mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
120mysql-server-5.1 mysql-server/start_on_boot boolean true
121MYSQL_PRESEED
122 fi
123
124 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
125 # as it allows you to access the mysql databases via ``mysql nova`` instead
126 # of having to specify the username/password each time.
127 if [[ ! -e $HOME/.my.cnf ]]; then
128 cat <<EOF >$HOME/.my.cnf
129[client]
130user=$DATABASE_USER
131password=$DATABASE_PASSWORD
132host=$DATABASE_HOST
133EOF
134 chmod 0600 $HOME/.my.cnf
135 fi
136 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100137 if is_ubuntu || is_fedora; then
138 install_package mysql-server
139 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200140 if ! is_package_installed mariadb; then
141 install_package mysql-community-server
142 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100143 else
Vincent Untz00011c02012-12-06 09:56:32 +0100144 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100145 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400146}
147
148function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100149 local db=$1
150 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400151}
152
Dean Troyercc6b4432013-04-08 15:38:03 -0500153
Terry Wilson428af5a2012-11-01 16:12:39 -0400154# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600155$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400156
157# Local variables:
158# mode: shell-script
159# End: