Amazon: How to change database name in Amazon RDS MySQL?

you can rename the database using the following command but make sure to take backup first

CREATE database new_db_name;
RENAME TABLE db_name.table1 TO new_db_name, db_name.table2 TO new_db_name;
DROP database db_name;

references: http://www.rndblog.com/how-to-rename-a-database-in-mysql/ https://stackoverflow.com/questions/12190000/rename-mysql-database


You can pull this off using mysqldump. Here the catch: You cannot ship the data because there may be a cost associated with shipping the data.

For this example, let's says you want to rename mydb to ourdb

STEP 01 : Create the new database

mysql> CREATE DATABASE ourdb;

STEP 02 : Get schema without the triggers

mysqldump -hrdshost -uuser -ppassword -d -t -R --skip-triggers mydb > /tmp/schema.sql

STEP 03 : Get the triggers

mysqldump -hrdshost -uuser -ppassword --skip-routines --triggers mydb > /tmp/triggers.sql

STEP 04 : Generate script to do INSERT ... SELECT across all tables

IF THERE ARE FOREIGN KEY CONSTRAINTS

ETL_DATA_SCRIPT=/tmp/DataTransfer.sql
echo -n > ${ETL_DATA_SCRIPT}
echo "SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT;" >> ${ETL_DATA_SCRIPT}
echo "SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS;" >> ${ETL_DATA_SCRIPT}
echo "SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;" >> ${ETL_DATA_SCRIPT}
echo "SET NAMES utf8;" >> ${ETL_DATA_SCRIPT}
echo "SET @OLD_TIME_ZONE=@@TIME_ZONE;" >> ${ETL_DATA_SCRIPT}
echo "SET TIME_ZONE='+00:00';" >> ${ETL_DATA_SCRIPT}
echo "SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;" >> ${ETL_DATA_SCRIPT}
echo "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;" >> ${ETL_DATA_SCRIPT}
echo "SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';" >> ${ETL_DATA_SCRIPT}
echo "SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0;" >> ${ETL_DATA_SCRIPT}
echo "SET group_concat_max_len = 104857600;" >> ${ETL_DATA_SCRIPT}
SQL="SELECT CONCAT('INSERT INTO ourdb.',table_name,' SELECt * FROM mydb.',table_name,';')"
SQL="${SQL} FROM information_schema.tables WHERE table_schema='mydb'"
mysql -hrdshost -uuser -ppassword -ANe"${SQL}" >> ${ETL_DATA_SCRIPT}

IF THERE ARE NO FOREIGN KEY CONSTRAINTS

ETL_DATA_SCRIPT=/tmp/DataTransfer.sql
SQL="SELECT CONCAT('ALTER TABLE mydb.',table_name,' RENAME ourdb.',table_name,';')"
SQL="${SQL} FROM information_schema.tables WHERE table_schema='mydb'"
mysql -hrdshost -uuser -ppassword -ANe"${SQL}" > ${ETL_DATA_SCRIPT}

If there are no foreign key constraints, this should go fast.

STEP 05 : Combine the files into a single script

ETL_SCRIPT=/tmp/ETL.sql
cat /tmp/schema.sql        > ${ETL_SCRIPT}
cat /tmp/DataTransfer.sql >> ${ETL_SCRIPT}
cat /tmp/triggers.sql     >> ${ETL_SCRIPT}

STEP 06 : Review the script

vi -R /tmp/ETL.sql

or

less /tmp/ETL.sql

STEP 07 : Run the script

mysql -hrdshost -uuser -ppassword -Dourdb < ${ETL_SCRIPT}

STEP 08 : Make sure all the data is in the target database

You can do that

If there were no foreign key constraints, mydb should be empty and ourdb should have all the tables.

If there were foreign key constraints, make sure mydb and ourdb have the same number of tables and the same number of rows. Make sure all triggers present by running

SELECT COUNT(1) trigger_count,table_schema
FROM information_schema.triggers
GROUP By table_schema;

STEP 09 : Drop the Old Database Manually

mysql> DROP DATABASE mydb;

I did not include dropping the database in the script, just in case. :-)

GIVE IT A TRY !!!