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