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: |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame^] | 5 | # |
| 6 | # - DATABASE_{HOST,USER,PASSWORD} must be defined |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 7 | |
| 8 | # Save trace setting |
Dean Troyer | 41bf452 | 2013-01-28 14:04:39 -0600 | [diff] [blame] | 9 | MY_XTRACE=$(set +o | grep xtrace) |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 10 | set +o xtrace |
| 11 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 12 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 13 | register_database mysql |
| 14 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 15 | |
| 16 | # Functions |
| 17 | # --------- |
| 18 | |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 19 | # Get rid of everything enough to cleanly change database backends |
| 20 | function cleanup_database_mysql { |
| 21 | if is_ubuntu; then |
| 22 | # Get ruthless with mysql |
| 23 | stop_service $MYSQL |
| 24 | sudo aptitude purge -y ~nmysql-server |
| 25 | sudo rm -rf /var/lib/mysql |
| 26 | return |
| 27 | elif is_fedora; then |
| 28 | MYSQL=mysqld |
| 29 | elif is_suse; then |
| 30 | MYSQL=mysql |
| 31 | else |
| 32 | return |
| 33 | fi |
| 34 | stop_service $MYSQL |
| 35 | } |
| 36 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 37 | function recreate_database_mysql { |
| 38 | local db=$1 |
| 39 | local charset=$2 |
zhhuabj | 2832f28 | 2013-05-08 18:43:26 +0800 | [diff] [blame] | 40 | mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;" |
| 41 | mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET $charset;" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | function configure_database_mysql { |
| 45 | echo_summary "Configuring and starting MySQL" |
| 46 | |
Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 47 | if is_ubuntu; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 48 | MY_CONF=/etc/mysql/my.cnf |
| 49 | MYSQL=mysql |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 50 | elif is_fedora; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 51 | MY_CONF=/etc/my.cnf |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 52 | MYSQL=mysqld |
| 53 | elif is_suse; then |
| 54 | MY_CONF=/etc/my.cnf |
| 55 | MYSQL=mysql |
| 56 | else |
| 57 | exit_distro_not_supported "mysql configuration" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 58 | fi |
| 59 | |
| 60 | # Start mysql-server |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 61 | if is_fedora || is_suse; then |
| 62 | # service is not started by default |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 63 | start_service $MYSQL |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 64 | fi |
| 65 | |
| 66 | # Set the root password - only works the first time. For Ubuntu, we already |
| 67 | # did that with debconf before installing the package. |
| 68 | if ! is_ubuntu; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 69 | sudo mysqladmin -u root password $DATABASE_PASSWORD || true |
| 70 | fi |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 71 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 72 | # Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases: |
| 73 | sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';" |
| 74 | |
| 75 | # Now update ``my.cnf`` for some local needs and restart the mysql service |
| 76 | |
| 77 | # Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0) |
| 78 | sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF |
| 79 | |
| 80 | # Set default db type to InnoDB |
| 81 | if sudo grep -q "default-storage-engine" $MY_CONF; then |
| 82 | # Change it |
| 83 | sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB" |
| 84 | else |
| 85 | # Add it |
| 86 | sudo sed -i -e "/^\[mysqld\]/ a \ |
| 87 | default-storage-engine = InnoDB" $MY_CONF |
| 88 | fi |
| 89 | |
Monty Taylor | 7c73e8d | 2013-01-07 08:17:01 +0000 | [diff] [blame] | 90 | # Turn on slow query log |
| 91 | sudo sed -i '/log.slow.queries/d' $MY_CONF |
| 92 | sudo sed -i -e "/^\[mysqld\]/ a \ |
| 93 | log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF |
| 94 | |
Joe Gordon | 767cd63 | 2013-01-18 17:15:44 -0500 | [diff] [blame] | 95 | # Log all queries (any query taking longer than 0 seconds) |
Monty Taylor | 7c73e8d | 2013-01-07 08:17:01 +0000 | [diff] [blame] | 96 | sudo sed -i '/long.query.time/d' $MY_CONF |
| 97 | sudo sed -i -e "/^\[mysqld\]/ a \ |
Joe Gordon | 767cd63 | 2013-01-18 17:15:44 -0500 | [diff] [blame] | 98 | long-query-time = 0" $MY_CONF |
Monty Taylor | 7c73e8d | 2013-01-07 08:17:01 +0000 | [diff] [blame] | 99 | |
| 100 | # Log all non-indexed queries |
| 101 | sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF |
| 102 | sudo sed -i -e "/^\[mysqld\]/ a \ |
| 103 | log-queries-not-using-indexes" $MY_CONF |
| 104 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 105 | restart_service $MYSQL |
| 106 | } |
| 107 | |
| 108 | function install_database_mysql { |
Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 109 | if is_ubuntu; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 110 | # Seed configuration with mysql password so that apt-get install doesn't |
| 111 | # prompt us for a password upon install. |
| 112 | cat <<MYSQL_PRESEED | sudo debconf-set-selections |
| 113 | mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD |
| 114 | mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD |
| 115 | mysql-server-5.1 mysql-server/start_on_boot boolean true |
| 116 | MYSQL_PRESEED |
| 117 | fi |
| 118 | |
| 119 | # while ``.my.cnf`` is not needed for OpenStack to function, it is useful |
| 120 | # as it allows you to access the mysql databases via ``mysql nova`` instead |
| 121 | # of having to specify the username/password each time. |
| 122 | if [[ ! -e $HOME/.my.cnf ]]; then |
| 123 | cat <<EOF >$HOME/.my.cnf |
| 124 | [client] |
| 125 | user=$DATABASE_USER |
| 126 | password=$DATABASE_PASSWORD |
| 127 | host=$DATABASE_HOST |
| 128 | EOF |
| 129 | chmod 0600 $HOME/.my.cnf |
| 130 | fi |
| 131 | # Install mysql-server |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 132 | if is_ubuntu || is_fedora; then |
| 133 | install_package mysql-server |
| 134 | elif is_suse; then |
Vincent Untz | 623a0a5 | 2013-04-11 08:41:27 +0200 | [diff] [blame] | 135 | if ! is_package_installed mariadb; then |
| 136 | install_package mysql-community-server |
| 137 | fi |
Vincent Untz | ca5c471 | 2012-11-21 17:45:49 +0100 | [diff] [blame] | 138 | else |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 139 | exit_distro_not_supported "mysql installation" |
Vincent Untz | ca5c471 | 2012-11-21 17:45:49 +0100 | [diff] [blame] | 140 | fi |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | function database_connection_url_mysql { |
Attila Fazekas | 7e79d91 | 2013-03-03 12:23:04 +0100 | [diff] [blame] | 144 | local db=$1 |
| 145 | echo "$BASE_SQL_CONN/$db?charset=utf8" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 148 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 149 | # Restore xtrace |
Dean Troyer | 41bf452 | 2013-01-28 14:04:39 -0600 | [diff] [blame] | 150 | $MY_XTRACE |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 151 | |
| 152 | # Local variables: |
| 153 | # mode: shell-script |
| 154 | # End: |