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