blob: 0633ab046e3ad2055956987c186d80d740e03b4b [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:
5# DATABASE_{HOST,USER,PASSWORD} must be defined
6
7# Save trace setting
Dean Troyer41bf4522013-01-28 14:04:39 -06008MY_XTRACE=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -04009set +o xtrace
10
11register_database mysql
12
Dean Troyer995eb922013-03-07 16:11:40 -060013# Get rid of everything enough to cleanly change database backends
14function cleanup_database_mysql {
15 if is_ubuntu; then
16 # Get ruthless with mysql
17 stop_service $MYSQL
18 sudo aptitude purge -y ~nmysql-server
19 sudo rm -rf /var/lib/mysql
20 return
21 elif is_fedora; then
22 MYSQL=mysqld
23 elif is_suse; then
24 MYSQL=mysql
25 else
26 return
27 fi
28 stop_service $MYSQL
29}
30
Terry Wilson428af5a2012-11-01 16:12:39 -040031function recreate_database_mysql {
32 local db=$1
33 local charset=$2
34 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -e "DROP DATABASE IF EXISTS $db;"
35 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -e "CREATE DATABASE $db CHARACTER SET $charset;"
36}
37
38function configure_database_mysql {
39 echo_summary "Configuring and starting MySQL"
40
Vincent Untzc18b9652012-12-04 12:36:34 +010041 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040042 MY_CONF=/etc/mysql/my.cnf
43 MYSQL=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010044 elif is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040045 MY_CONF=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010046 MYSQL=mysqld
47 elif is_suse; then
48 MY_CONF=/etc/my.cnf
49 MYSQL=mysql
50 else
51 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040052 fi
53
54 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010055 if is_fedora || is_suse; then
56 # service is not started by default
Terry Wilson428af5a2012-11-01 16:12:39 -040057 start_service $MYSQL
Vincent Untz00011c02012-12-06 09:56:32 +010058 fi
59
60 # Set the root password - only works the first time. For Ubuntu, we already
61 # did that with debconf before installing the package.
62 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040063 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
64 fi
Vincent Untz00011c02012-12-06 09:56:32 +010065
Terry Wilson428af5a2012-11-01 16:12:39 -040066 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
67 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
68
69 # Now update ``my.cnf`` for some local needs and restart the mysql service
70
71 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0)
72 sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF
73
74 # Set default db type to InnoDB
75 if sudo grep -q "default-storage-engine" $MY_CONF; then
76 # Change it
77 sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB"
78 else
79 # Add it
80 sudo sed -i -e "/^\[mysqld\]/ a \
81default-storage-engine = InnoDB" $MY_CONF
82 fi
83
Monty Taylor7c73e8d2013-01-07 08:17:01 +000084 # Turn on slow query log
85 sudo sed -i '/log.slow.queries/d' $MY_CONF
86 sudo sed -i -e "/^\[mysqld\]/ a \
87log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF
88
Joe Gordon767cd632013-01-18 17:15:44 -050089 # Log all queries (any query taking longer than 0 seconds)
Monty Taylor7c73e8d2013-01-07 08:17:01 +000090 sudo sed -i '/long.query.time/d' $MY_CONF
91 sudo sed -i -e "/^\[mysqld\]/ a \
Joe Gordon767cd632013-01-18 17:15:44 -050092long-query-time = 0" $MY_CONF
Monty Taylor7c73e8d2013-01-07 08:17:01 +000093
94 # Log all non-indexed queries
95 sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF
96 sudo sed -i -e "/^\[mysqld\]/ a \
97log-queries-not-using-indexes" $MY_CONF
98
Terry Wilson428af5a2012-11-01 16:12:39 -040099 restart_service $MYSQL
100}
101
102function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100103 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400104 # Seed configuration with mysql password so that apt-get install doesn't
105 # prompt us for a password upon install.
106 cat <<MYSQL_PRESEED | sudo debconf-set-selections
107mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
108mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
109mysql-server-5.1 mysql-server/start_on_boot boolean true
110MYSQL_PRESEED
111 fi
112
113 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
114 # as it allows you to access the mysql databases via ``mysql nova`` instead
115 # of having to specify the username/password each time.
116 if [[ ! -e $HOME/.my.cnf ]]; then
117 cat <<EOF >$HOME/.my.cnf
118[client]
119user=$DATABASE_USER
120password=$DATABASE_PASSWORD
121host=$DATABASE_HOST
122EOF
123 chmod 0600 $HOME/.my.cnf
124 fi
125 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100126 if is_ubuntu || is_fedora; then
127 install_package mysql-server
128 elif is_suse; then
Vincent Untzca5c4712012-11-21 17:45:49 +0100129 install_package mysql-community-server
130 else
Vincent Untz00011c02012-12-06 09:56:32 +0100131 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100132 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400133}
134
135function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100136 local db=$1
137 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400138}
139
140# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600141$MY_XTRACE