如何更改 MySQL 数据库中所有表的前缀?
我的提供商安装到我的网站 Drupal CMS。现在我需要从旧站点复制所有数据。我的旧数据库中有没有前缀的表,但在新数据库中,所有表都有 dp_[table_name]
前缀。
My provider installed to my site Drupal CMS. Now I need copy all my data from old site. I have tables without prefixes in my old DB, but in new DB all tables have dp_[table_name]
prefix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
zerkms 解决方案对我不起作用。我必须指定
information_schema
数据库才能查询Tables
表。编辑:
优化查询以仅调用 RENAME TABLE 一次。我遇到的问题是,连接的输出被截断为 341 个字符。这可以通过将 MySQL 变量
group_concat_max_len
设置为更高的值来解决(如果您的服务器允许):zerkms solution didn't work for me. I had to specify the
information_schema
database to be able to query theTables
table.Edit:
Optimized the query to only call RENAME TABLE once. Something I walked into was the fact that the concatenated output got truncated at 341 characters. This can be solved (if allowed by your server) by setting the MySQL variable
group_concat_max_len
to a higher value:PhpMyAdmin 现在允许您执行此操作。在“数据库”级别,选择“结构”选项卡以查看所有表。单击“全部检查”(在表格列表下方)。在“选择”下拉列表中选择:“替换表前缀”。
PhpMyAdmin allows you to do this now. At the "Database" level select the Structure tab to see all the tables. Click 'check all' (below the table listing). On the 'With selected' dropdown choose: 'Replace table prefix'.
编写一个将为每个表运行 RENAME TABLE 的脚本。
其中“test”是预期的数据库名称,
在此之后您可以进行长查询,如果您执行它,它将添加前缀;-)
write a script that will run RENAME TABLE for each table.
where "test" is expected database name
after this you can long query that will add prefixes if you execute it ;-)
只是稍微修改了一下,以解决前缀也出现在表名称中的情况。
Just modded this slightly to account for situations where the prefix is also in the table name.
您可以简单地转储数据库,使用文本编辑器打开转储,将所有出现的“CREATE TABLE”替换为“CREATE TABLE dp_”并恢复数据库。需要几分钟才能完成。
You can simply dump the database, open the dump with a text editor, replace all occurrences of "CREATE TABLE " with "CREATE TABLE dp_" and restore the database. It takes a couple of minutes to do.
如果有人还想知道如何执行此操作(因为它对我来说其他选项不起作用),您可以运行此命令(当然,更改您的值的前三个变量):
然后系统会提示您一堆更改数据库中的所有表所需的查询。您只需复制它,运行它,瞧!
If there's someone out there yet wondering how to do this (as it did not work form me the other options) you can run this (changing the first three variables for your values, of course):
And then you will be prompted with a bunch of queries needed in order to change all the tables in your database. You simply have to copy that, run it and voilá!
将 "Database-Name-Here" 替换为您的数据库名称,将 "wp_" 替换为旧前缀,将 "ab_" 替换为新的前缀。
前面的代码将生成对 MySQL DB 的查询,而且上面代码创建的查询是用单引号引起来的,必须将其替换。但是,如果您正在使用 WordPress,则需要执行更多步骤,否则您的网站将被破坏。在上述代码创建的查询之后,必须执行以下查询。
将 "NEWPREFIX" 替换为新前缀,将 "OLDPREFIX" 替换为旧前缀。
Replace "Database-Name-Here" with the name of your database, "wp_" with the old prefix, and "ab_" with the new prefix.
The preceding code will produce queries for the MySQL DB, also, the queries created by the above code are surrounded by single quotes, which must be replaced. However, if you are dealing with WordPress, you will need to do some more steps otherwise your site will be broken. Following the queries created by the aforementioned code, the following queries must be executed.
Replace "NEWPREFIX" with your new prefix and "OLDPREFIX" with your old prefix.