blob: 0eb8fdd7a27e8cc5789c8ae8d2806d74dced726d [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
Monty Taylor7c73e8d2013-01-07 08:17:01 +000090 # Turn on slow query log
91 sudo sed -i '/log.slow.queries/d' $MY_CONF
92 sudo sed -i -e "/^\[mysqld\]/ a \
93log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF
94
Joe Gordon767cd632013-01-18 17:15:44 -050095 # Log all queries (any query taking longer than 0 seconds)
Monty Taylor7c73e8d2013-01-07 08:17:01 +000096 sudo sed -i '/long.query.time/d' $MY_CONF
97 sudo sed -i -e "/^\[mysqld\]/ a \
Joe Gordon767cd632013-01-18 17:15:44 -050098long-query-time = 0" $MY_CONF
Monty Taylor7c73e8d2013-01-07 08:17:01 +000099
100 # Log all non-indexed queries
101 sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF
102 sudo sed -i -e "/^\[mysqld\]/ a \
103log-queries-not-using-indexes" $MY_CONF
104
Terry Wilson428af5a2012-11-01 16:12:39 -0400105 restart_service $MYSQL
106}
107
108function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100109 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400110 # Seed configuration with mysql password so that apt-get install doesn't
111 # prompt us for a password upon install.
112 cat <<MYSQL_PRESEED | sudo debconf-set-selections
113mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
114mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
115mysql-server-5.1 mysql-server/start_on_boot boolean true
116MYSQL_PRESEED
117 fi
118
119 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
120 # as it allows you to access the mysql databases via ``mysql nova`` instead
121 # of having to specify the username/password each time.
122 if [[ ! -e $HOME/.my.cnf ]]; then
123 cat <<EOF >$HOME/.my.cnf
124[client]
125user=$DATABASE_USER
126password=$DATABASE_PASSWORD
127host=$DATABASE_HOST
128EOF
129 chmod 0600 $HOME/.my.cnf
130 fi
131 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100132 if is_ubuntu || is_fedora; then
133 install_package mysql-server
134 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200135 if ! is_package_installed mariadb; then
136 install_package mysql-community-server
137 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100138 else
Vincent Untz00011c02012-12-06 09:56:32 +0100139 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100140 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400141}
142
143function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100144 local db=$1
145 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400146}
147
Dean Troyercc6b4432013-04-08 15:38:03 -0500148
Terry Wilson428af5a2012-11-01 16:12:39 -0400149# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600150$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400151
152# Local variables:
153# mode: shell-script
154# End: