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