Last active
          February 26, 2018 09:21 
        
      - 
      
- 
        Save krasnuydyx/ffae5dd5e4de8e8bdbcd4f12281d6fb5 to your computer and use it in GitHub Desktop. 
    How to change database name in Amazon RDS MySQL? (taken from https://dba.stackexchange.com/questions/76091/amazon-how-to-change-database-name-in-amazon-rds-mysql)
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #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} | |
| #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 | |
| #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; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment