blob: f5ee3c0ed06b33016da38993af7fcc32fe4ce857 [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
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010024 apt_get purge -y mysql*
Dean Troyer995eb922013-03-07 16:11:40 -060025 sudo rm -rf /var/lib/mysql
26 return
27 elif is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +010028 if [[ $DISTRO =~ (rhel7) ]]; then
29 MYSQL=mariadb
30 else
31 MYSQL=mysqld
32 fi
Dean Troyer995eb922013-03-07 16:11:40 -060033 elif is_suse; then
34 MYSQL=mysql
35 else
36 return
37 fi
38 stop_service $MYSQL
39}
40
Terry Wilson428af5a2012-11-01 16:12:39 -040041function recreate_database_mysql {
42 local db=$1
43 local charset=$2
zhhuabj2832f282013-05-08 18:43:26 +080044 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
45 mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET $charset;"
Terry Wilson428af5a2012-11-01 16:12:39 -040046}
47
48function configure_database_mysql {
49 echo_summary "Configuring and starting MySQL"
50
Vincent Untzc18b9652012-12-04 12:36:34 +010051 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040052 MY_CONF=/etc/mysql/my.cnf
53 MYSQL=mysql
Vincent Untz00011c02012-12-06 09:56:32 +010054 elif is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +010055 if [[ $DISTRO =~ (rhel7) ]]; then
56 MYSQL=mariadb
57 else
58 MYSQL=mysqld
59 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040060 MY_CONF=/etc/my.cnf
Vincent Untz00011c02012-12-06 09:56:32 +010061 elif is_suse; then
62 MY_CONF=/etc/my.cnf
63 MYSQL=mysql
64 else
65 exit_distro_not_supported "mysql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040066 fi
67
68 # Start mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010069 if is_fedora || is_suse; then
70 # service is not started by default
Terry Wilson428af5a2012-11-01 16:12:39 -040071 start_service $MYSQL
Vincent Untz00011c02012-12-06 09:56:32 +010072 fi
73
74 # Set the root password - only works the first time. For Ubuntu, we already
75 # did that with debconf before installing the package.
76 if ! is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040077 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
78 fi
Vincent Untz00011c02012-12-06 09:56:32 +010079
Terry Wilson428af5a2012-11-01 16:12:39 -040080 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
81 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
82
83 # Now update ``my.cnf`` for some local needs and restart the mysql service
84
85 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0)
86 sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF
87
88 # Set default db type to InnoDB
89 if sudo grep -q "default-storage-engine" $MY_CONF; then
90 # Change it
91 sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB"
92 else
93 # Add it
94 sudo sed -i -e "/^\[mysqld\]/ a \
95default-storage-engine = InnoDB" $MY_CONF
96 fi
97
Jeremy Stanleyc4f47342014-01-25 01:10:31 +000098 if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
99 echo_summary "Enabling MySQL query logging"
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000100
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000101 # Turn on slow query log
102 sudo sed -i '/log.slow.queries/d' $MY_CONF
103 sudo sed -i -e "/^\[mysqld\]/ a \
104 log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000105
Jeremy Stanleyc4f47342014-01-25 01:10:31 +0000106 # Log all queries (any query taking longer than 0 seconds)
107 sudo sed -i '/long.query.time/d' $MY_CONF
108 sudo sed -i -e "/^\[mysqld\]/ a \
109 long-query-time = 0" $MY_CONF
110
111 # Log all non-indexed queries
112 sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF
113 sudo sed -i -e "/^\[mysqld\]/ a \
114 log-queries-not-using-indexes" $MY_CONF
115
116 fi
Monty Taylor7c73e8d2013-01-07 08:17:01 +0000117
Terry Wilson428af5a2012-11-01 16:12:39 -0400118 restart_service $MYSQL
119}
120
121function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +0100122 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400123 # Seed configuration with mysql password so that apt-get install doesn't
124 # prompt us for a password upon install.
125 cat <<MYSQL_PRESEED | sudo debconf-set-selections
126mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
127mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
128mysql-server-5.1 mysql-server/start_on_boot boolean true
129MYSQL_PRESEED
130 fi
131
132 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
133 # as it allows you to access the mysql databases via ``mysql nova`` instead
134 # of having to specify the username/password each time.
135 if [[ ! -e $HOME/.my.cnf ]]; then
136 cat <<EOF >$HOME/.my.cnf
137[client]
138user=$DATABASE_USER
139password=$DATABASE_PASSWORD
140host=$DATABASE_HOST
141EOF
142 chmod 0600 $HOME/.my.cnf
143 fi
144 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +0100145 if is_ubuntu || is_fedora; then
Attila Fazekas2d650592014-02-20 15:49:13 +0100146 if [[ $DISTRO =~ (rhel7) ]]; then
147 install_package mariadb-server
148 else
149 install_package mysql-server
150 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100151 elif is_suse; then
Vincent Untz623a0a52013-04-11 08:41:27 +0200152 if ! is_package_installed mariadb; then
153 install_package mysql-community-server
154 fi
Vincent Untzca5c4712012-11-21 17:45:49 +0100155 else
Vincent Untz00011c02012-12-06 09:56:32 +0100156 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +0100157 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400158}
159
160function database_connection_url_mysql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100161 local db=$1
162 echo "$BASE_SQL_CONN/$db?charset=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400163}
164
Dean Troyercc6b4432013-04-08 15:38:03 -0500165
Terry Wilson428af5a2012-11-01 16:12:39 -0400166# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600167$MY_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400168
169# Local variables:
170# mode: shell-script
171# End: