blob: eb84f2ca40c80deec33364dc77c8543370e32757 [file] [log] [blame]
Terry Wilson428af5a2012-11-01 16:12:39 -04001# lib/mysql
2# Functions to control the configuration and operation of the MySQL database backend
3
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
23 if [[ "$os_PACKAGE" = "deb" ]]; then
24 MY_CONF=/etc/mysql/my.cnf
25 MYSQL=mysql
26 else
27 MY_CONF=/etc/my.cnf
Vincent Untz856a11e2012-11-21 16:04:12 +010028 if is_suse; then
29 MYSQL=mysql
30 else
31 MYSQL=mysqld
32 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040033 fi
34
35 # Start mysql-server
36 if [[ "$os_PACKAGE" = "rpm" ]]; then
37 # RPM doesn't start the service
38 start_service $MYSQL
39 # Set the root password - only works the first time
40 sudo mysqladmin -u root password $DATABASE_PASSWORD || true
41 fi
42 # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
43 sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
44
45 # Now update ``my.cnf`` for some local needs and restart the mysql service
46
47 # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0)
48 sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF
49
50 # Set default db type to InnoDB
51 if sudo grep -q "default-storage-engine" $MY_CONF; then
52 # Change it
53 sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB"
54 else
55 # Add it
56 sudo sed -i -e "/^\[mysqld\]/ a \
57default-storage-engine = InnoDB" $MY_CONF
58 fi
59
60 restart_service $MYSQL
61}
62
63function install_database_mysql {
64 if [[ "$os_PACKAGE" = "deb" ]]; then
65 # Seed configuration with mysql password so that apt-get install doesn't
66 # prompt us for a password upon install.
67 cat <<MYSQL_PRESEED | sudo debconf-set-selections
68mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
69mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
70mysql-server-5.1 mysql-server/start_on_boot boolean true
71MYSQL_PRESEED
72 fi
73
74 # while ``.my.cnf`` is not needed for OpenStack to function, it is useful
75 # as it allows you to access the mysql databases via ``mysql nova`` instead
76 # of having to specify the username/password each time.
77 if [[ ! -e $HOME/.my.cnf ]]; then
78 cat <<EOF >$HOME/.my.cnf
79[client]
80user=$DATABASE_USER
81password=$DATABASE_PASSWORD
82host=$DATABASE_HOST
83EOF
84 chmod 0600 $HOME/.my.cnf
85 fi
86 # Install mysql-server
Vincent Untzca5c4712012-11-21 17:45:49 +010087 if is_suse; then
88 install_package mysql-community-server
89 else
90 install_package mysql-server
91 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040092}
93
94function database_connection_url_mysql {
95 local output=$1
96 local db=$2
97 eval "$output=$BASE_SQL_CONN/$db?charset=utf8"
98}
99
100# Restore xtrace
101$XTRACE