lib/databases/mysql
6d04fd7b
 # lib/databases/mysql
 # Functions to control the configuration and operation of the **MySQL** database backend
428af5a2
 
 # Dependencies:
 # DATABASE_{HOST,USER,PASSWORD} must be defined
 
 # Save trace setting
41bf4520
 MY_XTRACE=$(set +o | grep xtrace)
428af5a2
 set +o xtrace
 
 register_database mysql
 
995eb927
 # Get rid of everything enough to cleanly change database backends
 function cleanup_database_mysql {
     if is_ubuntu; then
         # Get ruthless with mysql
         stop_service $MYSQL
         sudo aptitude purge -y ~nmysql-server
         sudo rm -rf /var/lib/mysql
         return
     elif is_fedora; then
         MYSQL=mysqld
     elif is_suse; then
         MYSQL=mysql
     else
         return
     fi
     stop_service $MYSQL
 }
 
428af5a2
 function recreate_database_mysql {
     local db=$1
     local charset=$2
     mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -e "DROP DATABASE IF EXISTS $db;"
     mysql -u$DATABASE_USER -p$DATABASE_PASSWORD -e "CREATE DATABASE $db CHARACTER SET $charset;"
 }
 
 function configure_database_mysql {
     echo_summary "Configuring and starting MySQL"
 
c18b9651
     if is_ubuntu; then
428af5a2
         MY_CONF=/etc/mysql/my.cnf
         MYSQL=mysql
00011c08
     elif is_fedora; then
         MY_CONF=/etc/my.cnf
         MYSQL=mysqld
     elif is_suse; then
428af5a2
         MY_CONF=/etc/my.cnf
00011c08
         MYSQL=mysql
     else
         exit_distro_not_supported "mysql configuration"
428af5a2
     fi
 
     # Start mysql-server
00011c08
     if is_fedora || is_suse; then
         # service is not started by default
428af5a2
         start_service $MYSQL
00011c08
     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
428af5a2
         sudo mysqladmin -u root password $DATABASE_PASSWORD || true
     fi
00011c08
 
428af5a2
     # 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)
     sudo sed -i '/^bind-address/s/127.0.0.1/0.0.0.0/g' $MY_CONF
 
     # Set default db type to InnoDB
     if sudo grep -q "default-storage-engine" $MY_CONF; then
         # Change it
         sudo bash -c "source $TOP_DIR/functions; iniset $MY_CONF mysqld default-storage-engine InnoDB"
     else
         # Add it
         sudo sed -i -e "/^\[mysqld\]/ a \
 default-storage-engine = InnoDB" $MY_CONF
     fi
 
7c73e8de
     # Turn on slow query log
     sudo sed -i '/log.slow.queries/d' $MY_CONF
     sudo sed -i -e "/^\[mysqld\]/ a \
 log-slow-queries = /var/log/mysql/mysql-slow.log" $MY_CONF
 
767cd631
     # Log all queries (any query taking longer than 0 seconds)
7c73e8de
     sudo sed -i '/long.query.time/d' $MY_CONF
     sudo sed -i -e "/^\[mysqld\]/ a \
767cd631
 long-query-time = 0" $MY_CONF
7c73e8de
 
     # Log all non-indexed queries
     sudo sed -i '/log.queries.not.using.indexes/d' $MY_CONF
     sudo sed -i -e "/^\[mysqld\]/ a \
 log-queries-not-using-indexes" $MY_CONF
 
428af5a2
     restart_service $MYSQL
 }
 
 function install_database_mysql {
c18b9651
     if is_ubuntu; then
428af5a2
         # Seed configuration with mysql password so that apt-get install doesn't
         # prompt us for a password upon install.
         cat <<MYSQL_PRESEED | sudo debconf-set-selections
 mysql-server-5.1 mysql-server/root_password password $DATABASE_PASSWORD
 mysql-server-5.1 mysql-server/root_password_again password $DATABASE_PASSWORD
 mysql-server-5.1 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
00011c08
     if is_ubuntu || is_fedora; then
         install_package mysql-server
     elif is_suse; then
ca5c4713
         install_package mysql-community-server
     else
00011c08
         exit_distro_not_supported "mysql installation"
ca5c4713
     fi
428af5a2
 }
 
 function database_connection_url_mysql {
7e79d913
     local db=$1
     echo "$BASE_SQL_CONN/$db?charset=utf8"
428af5a2
 }
 
 # Restore xtrace
41bf4520
 $MY_XTRACE
584d90ec
 
 # Local variables:
 # mode: shell-script
 # End: