Member-only story

Quick Rename MySQL Database and Tables.

Maciej
Oct 26, 2020

--

Introduction

In this short post I will show you how to quickly change the name of the database and tables that are in the database.

MySQL there is no support for something like database renaming.

You can manually do this with the commands below or via shell script:

Manual way:

  • Create new database:
[root@master vagrant]# mysql -uroot -p -h 10.21.21.12 -e "CREATE DATABASE new_db"
  • Use mysqldump to backup old database
[root@master vagrant]# mysqldump -uroot -p -h 10.21.21.12 old_db > old_db_dump.sql
  • Restore dumped database under a new name.
[root@master vagrant]# mysql -uroot -p -h 10.21.21.12 new_db < db_name_dump.sql
  • Rename table
[root@master vagrant]# mysql -uroot -p -h 10.21.21.12 -e "RENAME TABLE old_db.table1 TO new_db, old_db.table2 TO new_db;"
  • Drop old database
[root@master vagrant]# mysql -uroot -p -h 10.21.21.12 -e "DROP DATABASE old_db"

Do it all with simple bash script

--

--

Maciej
Maciej

Written by Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.

No responses yet