blob: ec65c36396f596c07c9979dbc25f6ef52eda722d [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
13function recreate_database_mysql {
14 local db=$1
15 local charset=$2
16 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -e "DROP DATABASE IF EXISTS $db;"
17 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -e "CREATE DATABASE $db CHARACTER SET $charset;"
18}
19
20function configure_database_mysql {
21 echo_summary "Configuring and starting MySQL"
22
Vincent Untzc18b9652012-12-04 12:36:34 +010023 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040024 MY_CONF=/etc/mysql/my.cnf
25 MYSQL=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010026 elif is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040027 MY_CONF=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010028 MYSQL=mysqld
29 elif is_suse; then
30 MY_CONF=/etc/my.cnf
31 MYSQL=mysql
32 else
33 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040034 fi
35
36 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010037 if is_fedora || is_suse; then
38 # service is not started by default
Terry Wilson428af5a2012-11-01 16:12:39 -040039 start_service $MYSQL
Vincent Untz00011c02012-12-06 09:56:32 +010040 fi
41
42 # Set the root password - only works the first time. For Ubuntu, we already
43 # did that with debconf before installing the package.
44 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040045 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
46 fi
Vincent Untz00011c02012-12-06 09:56:32 +010047
Terry Wilson428af5a2012-11-01 16:12:39 -040048 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
49 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
50
51 # Now update ``my.cnf`` for some local needs and restart the mysql service
52
53 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0)
54 sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF
55
56 # Set default db type to InnoDB
57 if sudo grep -q "default-storage-engine" $MY_CONF; then
58 # Change it
59 sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB"
60 else
61 # Add it
62 sudo sed -i -e "/^\[mysqld\]/ a \
63default-storage-engine = InnoDB" $MY_CONF
64 fi
65
Monty Taylor7c73e8d2013-01-07 08:17:01 +000066 # Turn on slow query log
67 sudo sed -i '/log.slow.queries/d' $MY_CONF
68 sudo sed -i -e "/^\[mysqld\]/ a \
69log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF
70
Joe Gordon767cd632013-01-18 17:15:44 -050071 # Log all queries (any query taking longer than 0 seconds)
Monty Taylor7c73e8d2013-01-07 08:17:01 +000072 sudo sed -i '/long.query.time/d' $MY_CONF
73 sudo sed -i -e "/^\[mysqld\]/ a \
Joe Gordon767cd632013-01-18 17:15:44 -050074long-query-time = 0" $MY_CONF
Monty Taylor7c73e8d2013-01-07 08:17:01 +000075
76 # Log all non-indexed queries
77 sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF
78 sudo sed -i -e "/^\[mysqld\]/ a \
79log-queries-not-using-indexes" $MY_CONF
80
Terry Wilson428af5a2012-11-01 16:12:39 -040081 restart_service $MYSQL
82}
83
84function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +010085 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040086 # Seed configuration with mysql password so that apt-get install doesn't
87 # prompt us for a password upon install.
88 cat <<MYSQL_PRESEED | sudo debconf-set-selections
89mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
90mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
91mysql-server-5.1 mysql-server/start_on_boot boolean true
92MYSQL_PRESEED
93 fi
94
95 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
96 # as it allows you to access the mysql databases via ``mysql nova`` instead
97 # of having to specify the username/password each time.
98 if [[ ! -e $HOME/.my.cnf ]]; then
99 cat <<EOF >$HOME/.my.cnf
100[client]
101user=$DATABASE_USER
102password=$DATABASE_PASSWORD
103host=$DATABASE_HOST
104EOF
105 chmod 0600 $HOME/.my.cnf
106 fi
107 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100108 if is_ubuntu || is_fedora; then
109 install_package mysql-server
110 elif is_suse; then
Vincent Untzca5c4712012-11-21 17:45:49 +0100111 install_package mysql-community-server
112 else
Vincent Untz00011c02012-12-06 09:56:32 +0100113 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100114 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400115}
116
117function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100118 local db=$1
119 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400120}
121
122# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600123$MY_XTRACE