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 |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 11 | _XTRACE_DB_MYSQL=$(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} |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 15 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 16 | register_database mysql |
| 17 | |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 18 | MYSQL_SERVICE_NAME=mysql |
Adam Spiers | bc2a88d | 2019-01-24 18:57:33 +0000 | [diff] [blame] | 19 | if is_fedora && ! is_oraclelinux; then |
| 20 | MYSQL_SERVICE_NAME=mariadb |
| 21 | elif is_suse && systemctl list-unit-files | grep -q 'mariadb\.service'; then |
| 22 | # Older mariadb packages on SLES 12 provided mysql.service. The |
| 23 | # newer ones on SLES 12 and 15 use mariadb.service; they also |
| 24 | # provide a mysql.service symlink for backwards-compatibility, but |
| 25 | # let's not rely on that. |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 26 | MYSQL_SERVICE_NAME=mariadb |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 27 | fi |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 28 | |
| 29 | # Functions |
| 30 | # --------- |
| 31 | |
Julien Danjou | 0eec4f8 | 2015-09-08 10:45:06 +0000 | [diff] [blame] | 32 | function get_database_type_mysql { |
| 33 | if [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then |
| 34 | echo mysql+pymysql |
| 35 | else |
| 36 | echo mysql |
| 37 | fi |
| 38 | } |
| 39 | |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 40 | # Get rid of everything enough to cleanly change database backends |
| 41 | function cleanup_database_mysql { |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 42 | stop_service $MYSQL_SERVICE_NAME |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 43 | if is_ubuntu; then |
| 44 | # Get ruthless with mysql |
Sean Dague | 8f90f76 | 2015-01-14 10:36:48 -0500 | [diff] [blame] | 45 | apt_get purge -y mysql* mariadb* |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 46 | sudo rm -rf /var/lib/mysql |
Tiago Mello | 4376ae0 | 2014-03-14 10:48:56 -0300 | [diff] [blame] | 47 | sudo rm -rf /etc/mysql |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 48 | return |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 49 | elif is_oraclelinux; then |
Wiekus Beukes | ec47bc1 | 2015-03-19 08:20:38 -0700 | [diff] [blame] | 50 | uninstall_package mysql-community-server |
| 51 | sudo rm -rf /var/lib/mysql |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 52 | elif is_suse || is_fedora; then |
Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 53 | uninstall_package mariadb-server |
| 54 | sudo rm -rf /var/lib/mysql |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 55 | else |
| 56 | return |
| 57 | fi |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 58 | } |
| 59 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 60 | function recreate_database_mysql { |
| 61 | local db=$1 |
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;" |
Ihar Hrachyshka | 157c84b | 2014-10-06 13:29:39 +0200 | [diff] [blame] | 63 | 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] | 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 |
Wiekus Beukes | ec47bc1 | 2015-03-19 08:20:38 -0700 | [diff] [blame] | 72 | elif is_suse || is_oraclelinux; then |
| 73 | my_conf=/etc/my.cnf |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 74 | elif is_fedora; then |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 75 | my_conf=/etc/my.cnf |
Yuval Brik | 13e81ad | 2017-06-23 10:32:16 +0300 | [diff] [blame] | 76 | local cracklib_conf=/etc/my.cnf.d/cracklib_password_check.cnf |
| 77 | if [ -f "$cracklib_conf" ]; then |
| 78 | inicomment -sudo "$cracklib_conf" "mariadb" "plugin-load-add" |
| 79 | fi |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 80 | else |
| 81 | exit_distro_not_supported "mysql configuration" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 82 | fi |
| 83 | |
| 84 | # Start mysql-server |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 85 | if is_fedora || is_suse; then |
| 86 | # service is not started by default |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 87 | start_service $MYSQL_SERVICE_NAME |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 88 | fi |
| 89 | |
| 90 | # Set the root password - only works the first time. For Ubuntu, we already |
Jens Rosenboom | 9abb26d | 2016-12-07 21:12:55 +0100 | [diff] [blame] | 91 | # did that with debconf before installing the package, but we still try, |
| 92 | # because the package might have been installed already. |
| 93 | sudo mysqladmin -u root password $DATABASE_PASSWORD || true |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 94 | |
Marian Horban | ea21eb4 | 2015-08-18 06:57:18 -0400 | [diff] [blame] | 95 | # 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] | 96 | sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';" |
| 97 | |
| 98 | # Now update ``my.cnf`` for some local needs and restart the mysql service |
| 99 | |
Marian Horban | ea21eb4 | 2015-08-18 06:57:18 -0400 | [diff] [blame] | 100 | # Change bind-address from localhost (127.0.0.1) to any (::) and |
Ralf Haferkamp | 0526bb8 | 2014-04-03 08:27:33 +0200 | [diff] [blame] | 101 | # set default db type to InnoDB |
Jens Harbott | dc7b429 | 2017-09-19 10:52:32 +0000 | [diff] [blame] | 102 | iniset -sudo $my_conf mysqld bind-address "$(ipv6_unquote $SERVICE_LISTEN_ADDRESS)" |
Roman Podoliaka | 88b8409 | 2017-02-07 13:34:12 +0200 | [diff] [blame] | 103 | iniset -sudo $my_conf mysqld sql_mode TRADITIONAL |
Ian Wienand | 9c0b9f3 | 2015-07-22 06:08:09 +1000 | [diff] [blame] | 104 | iniset -sudo $my_conf mysqld default-storage-engine InnoDB |
Jens Rosenboom | 4b59fbb | 2017-03-15 21:58:48 +0000 | [diff] [blame] | 105 | iniset -sudo $my_conf mysqld max_connections 1024 |
Ian Wienand | 9c0b9f3 | 2015-07-22 06:08:09 +1000 | [diff] [blame] | 106 | iniset -sudo $my_conf mysqld query_cache_type OFF |
| 107 | iniset -sudo $my_conf mysqld query_cache_size 0 |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 108 | |
Jeremy Stanley | c4f4734 | 2014-01-25 01:10:31 +0000 | [diff] [blame] | 109 | if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then |
| 110 | echo_summary "Enabling MySQL query logging" |
Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 111 | if is_fedora; then |
Attila Fazekas | 3b53aeb | 2014-04-30 11:57:22 +0200 | [diff] [blame] | 112 | slow_log=/var/log/mariadb/mariadb-slow.log |
| 113 | else |
| 114 | slow_log=/var/log/mysql/mysql-slow.log |
| 115 | fi |
Ralf Haferkamp | 0526bb8 | 2014-04-03 08:27:33 +0200 | [diff] [blame] | 116 | sudo sed -e '/log.slow.queries/d' \ |
| 117 | -e '/long.query.time/d' \ |
| 118 | -e '/log.queries.not.using.indexes/d' \ |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 119 | -i $my_conf |
Monty Taylor | 7c73e8d | 2013-01-07 08:17:01 +0000 | [diff] [blame] | 120 | |
Ralf Haferkamp | 0526bb8 | 2014-04-03 08:27:33 +0200 | [diff] [blame] | 121 | # Turn on slow query log, log all queries (any query taking longer than |
| 122 | # 0 seconds) and log all non-indexed queries |
Ian Wienand | 9c0b9f3 | 2015-07-22 06:08:09 +1000 | [diff] [blame] | 123 | iniset -sudo $my_conf mysqld slow-query-log 1 |
| 124 | iniset -sudo $my_conf mysqld slow-query-log-file $slow_log |
| 125 | iniset -sudo $my_conf mysqld long-query-time 0 |
| 126 | iniset -sudo $my_conf mysqld log-queries-not-using-indexes 1 |
Jeremy Stanley | c4f4734 | 2014-01-25 01:10:31 +0000 | [diff] [blame] | 127 | fi |
Monty Taylor | 7c73e8d | 2013-01-07 08:17:01 +0000 | [diff] [blame] | 128 | |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 129 | restart_service $MYSQL_SERVICE_NAME |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | function install_database_mysql { |
Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 133 | if is_ubuntu; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 134 | # Seed configuration with mysql password so that apt-get install doesn't |
| 135 | # prompt us for a password upon install. |
Bob Ball | 9033343 | 2015-01-19 10:56:42 +0000 | [diff] [blame] | 136 | sudo debconf-set-selections <<MYSQL_PRESEED |
| 137 | mysql-server mysql-server/root_password password $DATABASE_PASSWORD |
| 138 | mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD |
| 139 | mysql-server mysql-server/start_on_boot boolean true |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 140 | MYSQL_PRESEED |
| 141 | fi |
| 142 | |
| 143 | # while ``.my.cnf`` is not needed for OpenStack to function, it is useful |
| 144 | # as it allows you to access the mysql databases via ``mysql nova`` instead |
| 145 | # of having to specify the username/password each time. |
| 146 | if [[ ! -e $HOME/.my.cnf ]]; then |
| 147 | cat <<EOF >$HOME/.my.cnf |
| 148 | [client] |
| 149 | user=$DATABASE_USER |
| 150 | password=$DATABASE_PASSWORD |
Johan Pas | 199d857 | 2015-11-17 00:56:25 +0100 | [diff] [blame] | 151 | host=$MYSQL_HOST |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 152 | EOF |
| 153 | chmod 0600 $HOME/.my.cnf |
| 154 | fi |
| 155 | # Install mysql-server |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 156 | if is_oraclelinux; then |
| 157 | install_package mysql-community-server |
| 158 | elif is_fedora || is_suse; then |
Wiekus Beukes | ec47bc1 | 2015-03-19 08:20:38 -0700 | [diff] [blame] | 159 | install_package mariadb-server |
Dirk Mueller | 1d968d7 | 2017-09-23 14:45:42 +0200 | [diff] [blame] | 160 | sudo systemctl enable $MYSQL_SERVICE_NAME |
Wiekus Beukes | ec47bc1 | 2015-03-19 08:20:38 -0700 | [diff] [blame] | 161 | elif is_ubuntu; then |
| 162 | install_package mysql-server |
Vincent Untz | ca5c471 | 2012-11-21 17:45:49 +0100 | [diff] [blame] | 163 | else |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 164 | exit_distro_not_supported "mysql installation" |
Vincent Untz | ca5c471 | 2012-11-21 17:45:49 +0100 | [diff] [blame] | 165 | fi |
Dean Troyer | 5686dbc | 2015-03-09 14:27:51 -0500 | [diff] [blame] | 166 | } |
Dean Troyer | b1d8e8e | 2015-02-16 13:58:35 -0600 | [diff] [blame] | 167 | |
Dean Troyer | 5686dbc | 2015-03-09 14:27:51 -0500 | [diff] [blame] | 168 | function install_database_python_mysql { |
Dean Troyer | b1d8e8e | 2015-02-16 13:58:35 -0600 | [diff] [blame] | 169 | # Install Python client module |
Sean Dague | 3742199 | 2015-05-20 06:37:11 -0700 | [diff] [blame] | 170 | pip_install_gr $MYSQL_DRIVER |
| 171 | if [[ "$MYSQL_DRIVER" == "MySQL-python" ]]; then |
| 172 | ADDITIONAL_VENV_PACKAGES+=",MySQL-python" |
Julien Danjou | 0f63eb3 | 2015-06-12 09:05:12 +0200 | [diff] [blame] | 173 | elif [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then |
| 174 | ADDITIONAL_VENV_PACKAGES+=",PyMySQL" |
Sean Dague | 3742199 | 2015-05-20 06:37:11 -0700 | [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 |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 185 | $_XTRACE_DB_MYSQL |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 186 | |
| 187 | # Local variables: |
| 188 | # mode: shell-script |
| 189 | # End: |