如何使用覆盖从命令行导入 MySQL 转储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12677037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:03:19  来源:igfitidea点击:

How to import a MySQL dump from command line WITH overwrite

mysqlimportoverwrite

提问by CalfCrusher

i googled a lot and i can't found nothing about it !

我用谷歌搜索了很多,我什么也找不到!

[root@someday backups]# mysql -u username_1 -p db_1 < tables_to_import/tables.sql 
ERROR 1050 (42S01) at line 19: Table 'ps_customer' already exists

with mysql -fis the same. i wish simply import that .sql and rewrite that tables, can someone help me ?

mysql -f是一样的。我希望只需导入该 .sql 并重写该表,有人可以帮助我吗?

p.s. i know that when you export a db you can choose option "DROP TABLE" but if i have a backup, without this declaration ? how can i force ? Thanks

ps我知道当你导出一个数据库时你可以选择选项“DROP TABLE”但是如果我有一个备份,没有这个声明?我怎么能强迫?谢谢

回答by DrewB

When you do mysqldump add --add-drop-table (like twihoX mentioned). Then do your import as usual. So something like:

当您执行 mysqldump 添加 --add-drop-table (如提到的 twihoX)。然后像往常一样进行导入。所以像:

mysqldump --add-drop-table -u user -p db_1 > dumpfile.sql

mysql -u user -p db_1 < dumpfile.sql 

回答by octern

Are you trying to overwrite the entirety of the database? If so, you could manually drop all the tables, and then run your import script. This is easy to do in phpmyadmin. If you're using the CLI, the fastest way would be to use DROP DATABASE databasenameand then create database, though I think you'd then have to re-grant privileges for any non-root users.

您是否试图覆盖整个数据库?如果是这样,您可以手动删除所有表,然后运行导入脚本。这在 phpmyadmin 中很容易做到。如果您使用的是 CLI,最快的方法是使用DROP DATABASE databasenameand then create database,但我认为您必须为任何非 root 用户重新授予权限。

Another option would be to open up your dump file and add DROP TABLE tablenamebefore each of the CREATE TABLE commands. You could probably do this easily with some clever regex.

另一种选择是打开转储文件并DROP TABLE tablename在每个 CREATE TABLE 命令之前添加。您可能可以使用一些巧妙的正则表达式轻松完成此操作。

回答by twihoX

I'd suggest --add-drop-table option.

我建议 --add-drop-table 选项。