且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们如何在MySQL 5.0中重命名数据库名称

更新时间:2023-02-03 22:24:48

我认为只有一种方法(除了重命名MySQL数据目录中的目录(对于InnoDB表而言,该目录将失败):

I think there is only one way (besides renaming directory in the MySQL datadir which will fail for InnoDB tables):

  • 创建新数据库(使用新名称)
  • 对旧数据库进行转储
  • 将转储的数据导入新数据库
  • 删除旧数据库

要创建新的数据库,请执行以下操作:

To create the new DB:

mysql> CREATE DATABASE new_database;

要创建旧数据库的转储,请执行以下操作:

To create the dump of the old DB:

mysqldump -u "your_username" -p --lock-tables old_database > old_database_dump.sql

要将转储的数据导入新数据库:

To import dumped data into the new DB:

mysql -u "your username" -p new_database  < old_database_dump.sql

要删除旧数据库,请执行以下操作:

To delete the old DB:

mysql> DROP DATABASE old_database;

请记住,您对旧数据库的权限也需要删除.请参阅此处以获取更多信息: 撤消MySQL数据库上所有用户的所有特权

Bear in mind that your permissions on the old DB will need to be deleted as well. See here for more info: Revoke all privileges for all users on a MySQL DB

从MySQL 5.1.7到MySQL 5.1.22,有一个RENAME {DATABASE | SCHEMA} db_name TO new_db_name;命令,但是由于过于危险,该命令已在MySQL 5.1.23中删除.

MySQL 5.1.7 to MySQL 5.1.22 had a RENAME {DATABASE | SCHEMA} db_name TO new_db_name; command but this one has been removed in MySQL 5.1.23 for being too dangerous.