How to Rename a Database

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 我想rename database 如何实现

| username: TiDBer_VHf8RBib

I want to change the database name, how can I achieve this?

| username: caiyfc | Original post link

Roundabout tactics, hahaha.

| username: ealam_小羽 | Original post link

Refer to this:
https://zhuanlan.zhihu.com/p/264878599

| username: liuis | Original post link

First create the database, then just dump it over.

| username: xingzhenxiang | Original post link

1 First, create a new database:

create database new_db;

2 Use the RENAME TABLE command to change the table name and move the table to the new database:

rename table old_db.tb to new_db.tb;

3 Delete the old database after completion:

drop database old_db;
| username: ffeenn | Original post link

You can only recreate the database. If the business cannot be stopped, try using DM binlog synchronization.

| username: Kongdom | Original post link

Yes, generally the method used is to rename the table.

| username: db_user | Original post link

In lower versions of MySQL, you can still rename the database, but in higher versions, you can’t. I guess TiDB doesn’t support it either, so it’s better to use the rename db.table syntax.

| username: erwadba | Original post link

For versions above v6.4, you can use flashback database to change the database name.
FLASHBACK DATABASE | PingCAP Documentation Center

DROP DATABASE test;
FLASHBACK DATABASE test TO test1;
| username: xfworld | Original post link

This approach is very impressive~ :upside_down_face: :+1:

| username: liuis | Original post link

It’s pretty good, but will there be any exceptions that can’t be recovered from…

| username: Jellybean | Original post link

Haha, this idea is very bold and also very risky.

Under normal circumstances, it is indeed feasible, but under abnormal circumstances, you might need to run away :sweat_smile:

| username: liuis | Original post link

I think it’s more reliable to dump it out and then import it again, at least there will be backup data.

| username: Jellybean | Original post link

Yes, I agree with your statement, it is stable and reliable.

| username: YuchongXU | Original post link

Logical backup and restore