blob: 1c0f5ebff6722cb8b47aef2b719052199c4ca907 [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
8XTRACE=$(set +o | grep xtrace)
9set +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
66 restart_service $MYSQL
67}
68
69function install_database_mysql {
Vincent Untzc18b9652012-12-04 12:36:34 +010070 if is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040071 # Seed configuration with mysql password so that apt-get install doesn't
72 # prompt us for a password upon install.
73 cat <<MYSQL_PRESEED | sudo debconf-set-selections
74mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
75mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
76mysql-server-5.1 mysql-server/start_on_boot boolean true
77MYSQL_PRESEED
78 fi
79
80 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
81 # as it allows you to access the mysql databases via ``mysql nova`` instead
82 # of having to specify the username/password each time.
83 if [[ ! -e $HOME/.my.cnf ]]; then
84 cat <<EOF >$HOME/.my.cnf
85[client]
86user=$DATABASE_USER
87password=$DATABASE_PASSWORD
88host=$DATABASE_HOST
89EOF
90 chmod 0600 $HOME/.my.cnf
91 fi
92 # Install mysql-server
Vincent Untz00011c02012-12-06 09:56:32 +010093 if is_ubuntu || is_fedora; then
94 install_package mysql-server
95 elif is_suse; then
Vincent Untzca5c4712012-11-21 17:45:49 +010096 install_package mysql-community-server
97 else
Vincent Untz00011c02012-12-06 09:56:32 +010098 exit_distro_not_supported "mysql installation"
Vincent Untzca5c4712012-11-21 17:45:49 +010099 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400100}
101
102function database_connection_url_mysql {
103 local output=$1
104 local db=$2
105 eval "$output=$BASE_SQL_CONN/$db?charset=utf8"
106}
107
108# Restore xtrace
109$XTRACE