6

I want to change MYSQL database name of the DB instance of Amazon RDS.

How it can be possible?

Brent Ozar
  • 43,325
  • 51
  • 233
  • 390
Nisarg
  • 113
  • 1
  • 1
  • 7

6 Answers6

7

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

Ahmad Abuhasna
  • 2,718
  • 4
  • 25
  • 36
6

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 !!!

RolandoMySQLDBA
  • 185,223
  • 33
  • 326
  • 536
0

The best way is to create a new database, then do show tables in the database you want to move from, and then do for every table:

rename table old_db.TABLE_NAME to new_db.TABLE_NAME
András Váczi
  • 31,778
  • 13
  • 102
  • 151
0

In my case, I realized there was no DB name when I've tried to modify it.

I couldn't find why this happened; I ultimately rebuilt the RDS to get things working.

RDFozz
  • 11,731
  • 4
  • 25
  • 38
Ording
  • 1
0

You can export the database, then import it again as a different name:

mysqldump -u username -p pwd -v old_db_name > old_db_name.sql mysqladmin -u username -p pwd create new_db_name mysql -u username -p pwd new_db_name < old_db_name.sql

Source: MySQL Reference for dumps

LowlyDBA - John M
  • 11,059
  • 11
  • 45
  • 63
-1
  1. Under the RDS "Instances" listing, click the arrow next to the DB Instance name.
  2. You will find additional details on the selected instance
  3. Click on the "Instance Actions" button and click "Modify"
  4. Modify your instance information and save
Tan Hong Tat
  • 284
  • 1
  • 6