如何在 MySQL 中为数据库设置别名?

2021-11-27 00:00:00 alias schema mysql

我正在寻找一种在 MySQL 中为数据库设置别名的方法.原因是能够在不关闭系统的情况下重命名实时生产数据库.我想我可以将数据库别名为新名称,在空闲时更改和部署连接到它的代码,并最终删除旧别名.

I'm looking for a way to alias a database in MySQL. The reason is to be able to rename a live, production database without bringing the system down. I figure I can alias the database to the new name, change and deploy the code connecting to it at my leisure, and eventually remove the old alias.

如果有更好的方法可以做到这一点,请告诉我.

If there's a better way to accomplish this please let me know.

推荐答案

https://dev.mysql.com/doc/refman/5.7/en/symbolic-links-to-databases.html 说

MySQL 不支持将一个目录链接到多个数据库.

MySQL does not support linking one directory to multiple databases.

您可以使用符号链接将数据库目录链接到其他位置,例如在 datadir 之外.

You can use symbolic links to link a database directory to some other location, for example outside the datadir.

$ cd /var/lib/mysql
$ ln -s /other/dir/mydatabase .

但是您不能使用符号链接使一个数据库目录成为另一个 MySQL 数据库的别名":

But you can't use symbolic links to make one database directory an "alias" for another MySQL database:

$ cd /var/lib/mysql
$ ln -s `pwd`/mydatabase1 mydatabase2  # WRONG

原因是 InnoDB 将数据库名称和其他元数据保留在其自己的数据字典中,存储在表空间文件中.如果你混淆了它,你就得不到你想要的.

The reason is that InnoDB retains database names and other metadata inside its own data dictionary, stored in the tablespace file. If you confuse it, you won't get what you want.

MySQL 没有任何用于为数据库设置别名的 DDL 语法.

MySQL doesn't have any DDL syntax for aliasing a database.

相关文章