37421991b4
This allows you to specify MYSQL_DRIVER=PyMySQL and get it in the environment. Change-Id: Ic9d75266640b7aa6d7efb6e882d3027e81414059
183 lines
5.4 KiB
Bash
183 lines
5.4 KiB
Bash
#!/bin/bash
|
||
#
|
||
# lib/databases/mysql
|
||
# Functions to control the configuration and operation of the **MySQL** database backend
|
||
|
||
# Dependencies:
|
||
#
|
||
# - DATABASE_{HOST,USER,PASSWORD} must be defined
|
||
|
||
# Save trace setting
|
||
MY_XTRACE=$(set +o | grep xtrace)
|
||
set +o xtrace
|
||
|
||
MYSQL_DRIVER=${MYSQL_DRIVER:-MySQL-python}
|
||
# Force over to pymysql driver by default if we are using it.
|
||
if is_service_enabled mysql; then
|
||
if [[ "$MYSQL_DRIVER" == "PyMySQL" ]]; then
|
||
SQLALCHEMY_DATABASE_DRIVER=${SQLALCHEMY_DATABASE_DRIVER:-"pymysql"}
|
||
fi
|
||
fi
|
||
|
||
register_database mysql
|
||
|
||
# Linux distros, thank you for being incredibly consistent
|
||
MYSQL=mysql
|
||
if is_fedora && ! is_oraclelinux; then
|
||
MYSQL=mariadb
|
||
fi
|
||
|
||
# Functions
|
||
# ---------
|
||
|
||
# Get rid of everything enough to cleanly change database backends
|
||
function cleanup_database_mysql {
|
||
stop_service $MYSQL
|
||
if is_ubuntu; then
|
||
# Get ruthless with mysql
|
||
apt_get purge -y mysql* mariadb*
|
||
sudo rm -rf /var/lib/mysql
|
||
sudo rm -rf /etc/mysql
|
||
return
|
||
elif is_suse || is_oraclelinux; then
|
||
uninstall_package mysql-community-server
|
||
sudo rm -rf /var/lib/mysql
|
||
elif is_fedora; then
|
||
uninstall_package mariadb-server
|
||
sudo rm -rf /var/lib/mysql
|
||
else
|
||
return
|
||
fi
|
||
}
|
||
|
||
function recreate_database_mysql {
|
||
local db=$1
|
||
mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "DROP DATABASE IF EXISTS $db;"
|
||
mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -h$MYSQL_HOST -e "CREATE DATABASE $db CHARACTER SET utf8;"
|
||
}
|
||
|
||
function configure_database_mysql {
|
||
local my_conf mysql slow_log
|
||
echo_summary "Configuring and starting MySQL"
|
||
|
||
if is_ubuntu; then
|
||
my_conf=/etc/mysql/my.cnf
|
||
mysql=mysql
|
||
elif is_suse || is_oraclelinux; then
|
||
my_conf=/etc/my.cnf
|
||
mysql=mysql
|
||
elif is_fedora; then
|
||
mysql=mariadb
|
||
my_conf=/etc/my.cnf
|
||
else
|
||
exit_distro_not_supported "mysql configuration"
|
||
fi
|
||
|
||
# Start mysql-server
|
||
if is_fedora || is_suse; then
|
||
# service is not started by default
|
||
start_service $mysql
|
||
fi
|
||
|
||
# Set the root password - only works the first time. For Ubuntu, we already
|
||
# did that with debconf before installing the package.
|
||
if ! is_ubuntu; then
|
||
sudo mysqladmin -u root password $DATABASE_PASSWORD || true
|
||
fi
|
||
|
||
# Update the DB to give user ‘$DATABASE_USER’@’%’ full control of the all databases:
|
||
sudo mysql -uroot -p$DATABASE_PASSWORD -h127.0.0.1 -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
|
||
|
||
# Now update ``my.cnf`` for some local needs and restart the mysql service
|
||
|
||
# Change ‘bind-address’ from localhost (127.0.0.1) to any (0.0.0.0) and
|
||
# set default db type to InnoDB
|
||
sudo bash -c "source $TOP_DIR/functions && \
|
||
iniset $my_conf mysqld bind-address 0.0.0.0 && \
|
||
iniset $my_conf mysqld sql_mode STRICT_ALL_TABLES && \
|
||
iniset $my_conf mysqld default-storage-engine InnoDB"
|
||
|
||
|
||
if [[ "$DATABASE_QUERY_LOGGING" == "True" ]]; then
|
||
echo_summary "Enabling MySQL query logging"
|
||
if is_fedora; then
|
||
slow_log=/var/log/mariadb/mariadb-slow.log
|
||
else
|
||
slow_log=/var/log/mysql/mysql-slow.log
|
||
fi
|
||
sudo sed -e '/log.slow.queries/d' \
|
||
-e '/long.query.time/d' \
|
||
-e '/log.queries.not.using.indexes/d' \
|
||
-i $my_conf
|
||
|
||
# Turn on slow query log, log all queries (any query taking longer than
|
||
# 0 seconds) and log all non-indexed queries
|
||
sudo bash -c "source $TOP_DIR/functions && \
|
||
iniset $my_conf mysqld slow-query-log 1 && \
|
||
iniset $my_conf mysqld slow-query-log-file $slow_log && \
|
||
iniset $my_conf mysqld long-query-time 0 && \
|
||
iniset $my_conf mysqld log-queries-not-using-indexes 1"
|
||
|
||
fi
|
||
|
||
restart_service $mysql
|
||
}
|
||
|
||
function install_database_mysql {
|
||
if is_ubuntu; then
|
||
# Seed configuration with mysql password so that apt-get install doesn't
|
||
# prompt us for a password upon install.
|
||
sudo debconf-set-selections <<MYSQL_PRESEED
|
||
mysql-server mysql-server/root_password password $DATABASE_PASSWORD
|
||
mysql-server mysql-server/root_password_again password $DATABASE_PASSWORD
|
||
mysql-server mysql-server/start_on_boot boolean true
|
||
MYSQL_PRESEED
|
||
fi
|
||
|
||
# while ``.my.cnf`` is not needed for OpenStack to function, it is useful
|
||
# as it allows you to access the mysql databases via ``mysql nova`` instead
|
||
# of having to specify the username/password each time.
|
||
if [[ ! -e $HOME/.my.cnf ]]; then
|
||
cat <<EOF >$HOME/.my.cnf
|
||
[client]
|
||
user=$DATABASE_USER
|
||
password=$DATABASE_PASSWORD
|
||
host=$DATABASE_HOST
|
||
EOF
|
||
chmod 0600 $HOME/.my.cnf
|
||
fi
|
||
# Install mysql-server
|
||
if is_suse || is_oraclelinux; then
|
||
if ! is_package_installed mariadb; then
|
||
install_package mysql-community-server
|
||
fi
|
||
elif is_fedora; then
|
||
install_package mariadb-server
|
||
elif is_ubuntu; then
|
||
install_package mysql-server
|
||
else
|
||
exit_distro_not_supported "mysql installation"
|
||
fi
|
||
}
|
||
|
||
function install_database_python_mysql {
|
||
# Install Python client module
|
||
pip_install_gr $MYSQL_DRIVER
|
||
if [[ "$MYSQL_DRIVER" == "MySQL-python" ]]; then
|
||
ADDITIONAL_VENV_PACKAGES+=",MySQL-python"
|
||
fi
|
||
}
|
||
|
||
function database_connection_url_mysql {
|
||
local db=$1
|
||
echo "$BASE_SQL_CONN/$db?charset=utf8"
|
||
}
|
||
|
||
|
||
# Restore xtrace
|
||
$MY_XTRACE
|
||
|
||
# Local variables:
|
||
# mode: shell-script
|
||
# End:
|