Dean Troyer | 6d04fd7 | 2012-12-21 11:03:37 -0600 | [diff] [blame^] | 1 | # lib/databases/mysql |
| 2 | # Functions to control the configuration and operation of the **MySQL** database backend |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 3 | |
| 4 | # Dependencies: |
| 5 | # DATABASE_{HOST,USER,PASSWORD} must be defined |
| 6 | |
| 7 | # Save trace setting |
| 8 | XTRACE=$(set +o | grep xtrace) |
| 9 | set +o xtrace |
| 10 | |
| 11 | register_database mysql |
| 12 | |
| 13 | function 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 | |
| 20 | function configure_database_mysql { |
| 21 | echo_summary "Configuring and starting MySQL" |
| 22 | |
Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 23 | if is_ubuntu; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 24 | MY_CONF=/etc/mysql/my.cnf |
| 25 | MYSQL=mysql |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 26 | elif is_fedora; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 27 | MY_CONF=/etc/my.cnf |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 28 | 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 Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 34 | fi |
| 35 | |
| 36 | # Start mysql-server |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 37 | if is_fedora || is_suse; then |
| 38 | # service is not started by default |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 39 | start_service $MYSQL |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 40 | 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 Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 45 | sudo mysqladmin -u root password $DATABASE_PASSWORD || true |
| 46 | fi |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 47 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 48 | # 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 \ |
| 63 | default-storage-engine = InnoDB" $MY_CONF |
| 64 | fi |
| 65 | |
| 66 | restart_service $MYSQL |
| 67 | } |
| 68 | |
| 69 | function install_database_mysql { |
Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 70 | if is_ubuntu; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 71 | # 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 |
| 74 | mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD |
| 75 | mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD |
| 76 | mysql-server-5.1 mysql-server/start_on_boot boolean true |
| 77 | MYSQL_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] |
| 86 | user=$DATABASE_USER |
| 87 | password=$DATABASE_PASSWORD |
| 88 | host=$DATABASE_HOST |
| 89 | EOF |
| 90 | chmod 0600 $HOME/.my.cnf |
| 91 | fi |
| 92 | # Install mysql-server |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 93 | if is_ubuntu || is_fedora; then |
| 94 | install_package mysql-server |
| 95 | elif is_suse; then |
Vincent Untz | ca5c471 | 2012-11-21 17:45:49 +0100 | [diff] [blame] | 96 | install_package mysql-community-server |
| 97 | else |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 98 | exit_distro_not_supported "mysql installation" |
Vincent Untz | ca5c471 | 2012-11-21 17:45:49 +0100 | [diff] [blame] | 99 | fi |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | function 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 |