blob: 41e3236f6939eb827e512af35934ce91f8ddef90 [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
Dean Troyercc6b4432013-04-08 15:38:03 -050011
Terry Wilson428af5a2012-11-01 16:12:39 -040012register_database mysql
13
Dean Troyercc6b4432013-04-08 15:38:03 -050014
15# Functions
16# ---------
17
Dean Troyer995eb922013-03-07 16:11:40 -060018# Get rid of everything enough to cleanly change database backends
19function cleanup_database_mysql {
20 if is_ubuntu; then
21 # Get ruthless with mysql
22 stop_service $MYSQL
23 sudo aptitude purge -y ~nmysql-server
24 sudo rm -rf /var/lib/mysql
25 return
26 elif is_fedora; then
27 MYSQL=mysqld
28 elif is_suse; then
29 MYSQL=mysql
30 else
31 return
32 fi
33 stop_service $MYSQL
34}
35
Terry Wilson428af5a2012-11-01 16:12:39 -040036function recreate_database_mysql {
37 local db=$1
38 local charset=$2
zhhuabj2832f282013-05-08 18:43:26 +080039 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
40 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET $charset;"
Terry Wilson428af5a2012-11-01 16:12:39 -040041}
42
43function configure_database_mysql {
44 echo_summary "Configuring and starting MySQL"
45
Vincent Untzc18b9652012-12-04 12:36:34 +010046 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040047 MY_CONF=/etc/mysql/my.cnf
48 MYSQL=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010049 elif is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040050 MY_CONF=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010051 MYSQL=mysqld
52 elif is_suse; then
53 MY_CONF=/etc/my.cnf
54 MYSQL=mysql
55 else
56 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040057 fi
58
59 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010060 if is_fedora || is_suse; then
61 # service is not started by default
Terry Wilson428af5a2012-11-01 16:12:39 -040062 start_service $MYSQL
Vincent Untz00011c02012-12-06 09:56:32 +010063 fi
64
65 # Set the root password - only works the first time. For Ubuntu, we already
66 # did that with debconf before installing the package.
67 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040068 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
69 fi
Vincent Untz00011c02012-12-06 09:56:32 +010070
Terry Wilson428af5a2012-11-01 16:12:39 -040071 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
72 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
73
74 # Now update ``my.cnf`` for some local needs and restart the mysql service
75
76 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0)
77 sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF
78
79 # Set default db type to InnoDB
80 if sudo grep -q "default-storage-engine" $MY_CONF; then
81 # Change it
82 sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB"
83 else
84 # Add it
85 sudo sed -i -e "/^\[mysqld\]/ a \
86default-storage-engine = InnoDB" $MY_CONF
87 fi
88
Monty Taylor7c73e8d2013-01-07 08:17:01 +000089 # Turn on slow query log
90 sudo sed -i '/log.slow.queries/d' $MY_CONF
91 sudo sed -i -e "/^\[mysqld\]/ a \
92log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF
93
Joe Gordon767cd632013-01-18 17:15:44 -050094 # Log all queries (any query taking longer than 0 seconds)
Monty Taylor7c73e8d2013-01-07 08:17:01 +000095 sudo sed -i '/long.query.time/d' $MY_CONF
96 sudo sed -i -e "/^\[mysqld\]/ a \
Joe Gordon767cd632013-01-18 17:15:44 -050097long-query-time = 0" $MY_CONF
Monty Taylor7c73e8d2013-01-07 08:17:01 +000098
99 # Log all non-indexed queries
100 sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF
101 sudo sed -i -e "/^\[mysqld\]/ a \
102log-queries-not-using-indexes" $MY_CONF
103
Terry Wilson428af5a2012-11-01 16:12:39 -0400104 restart_service $MYSQL
105}
106
107function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100108 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400109 # Seed configuration with mysql password so that apt-get install doesn't
110 # prompt us for a password upon install.
111 cat <<MYSQL_PRESEED | sudo debconf-set-selections
112mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
113mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
114mysql-server-5.1 mysql-server/start_on_boot boolean true
115MYSQL_PRESEED
116 fi
117
118 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
119 # as it allows you to access the mysql databases via ``mysql nova`` instead
120 # of having to specify the username/password each time.
121 if [[ ! -e $HOME/.my.cnf ]]; then
122 cat <<EOF >$HOME/.my.cnf
123[client]
124user=$DATABASE_USER
125password=$DATABASE_PASSWORD
126host=$DATABASE_HOST
127EOF
128 chmod 0600 $HOME/.my.cnf
129 fi
130 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100131 if is_ubuntu || is_fedora; then
132 install_package mysql-server
133 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200134 if ! is_package_installed mariadb; then
135 install_package mysql-community-server
136 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100137 else
Vincent Untz00011c02012-12-06 09:56:32 +0100138 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100139 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400140}
141
142function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100143 local db=$1
144 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400145}
146
Dean Troyercc6b4432013-04-08 15:38:03 -0500147
Terry Wilson428af5a2012-11-01 16:12:39 -0400148# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600149$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400150
151# Local variables:
152# mode: shell-script
153# End: