简体   繁体   English

完成MySQL转储的数据库重置?

[英]Complete database reset for MySQL dump?

This may seem like a very dumb question but I didn't learn it in any other way and I just want to have some clarification. 这似乎是一个非常愚蠢的问题,但我没有以任何其他方式学习它,我只是想澄清一下。

I started to use MySQL a while ago and in order to test various scenarios, I back up my databases. 我刚刚开始使用MySQL,为了测试各种场景,我备份了我的数据库。 I used MySQL dump for that: 我使用MySQL转储:

Export: 出口:

mysqldump -hSERVER -uUSER -pPASSWORD --all-databases > filename.sql mysqldump -hSERVER -uUSER -pPASSWORD --all-databases> filename.sql

Import: 进口:

mysql -hSERVER -uUSER -pPASSWORD < filename.sql mysql -hSERVER -uUSER -pPASSWORD <filename.sql

Easy enough and it worked quite well up until now, when I noticed a little problem with this "setup": It does not fully "reset" the databases and tables. 很容易,直到现在它工作得很好,当我注意到这个“设置”有点问题:它没有完全“重置”数据库和表。 If, for example, there is an additional table added AFTER a dump file has been created, that additional table will not disappear if you import the same dump file. 例如,如果在创建转储文件后添加了附加表,则在导入相同的转储文件时,该附加表不会消失。 It essentially only "corrects" tables already there and recreates any databaes or tables missing, but does not remove any additional tables, which happen to have names that are not in the dump file. 它本质上只是“纠正”已存在的表并重新创建任何数据库或表缺失,但不会删除任何其他表,这些表恰好具有不在转储文件中的名称。

What I want to do is to completely reset all the databases on a server when I import such a dump file. 我想要做的是在导入这样的转储文件时完全重置服务器上的所有数据库。 What would be the best solution? 什么是最好的解决方案? Is there a special import function reserved for that purpose or do I have to delete the databases myself first? 是否有为此目的保留的特殊导入功能,还是我必须先自行删除数据库? Or is that a bad idea? 或者这是一个坏主意?

You can use the parameter --add-drop-database to add a "drop database" statement to the dump before each "create database" statement. 您可以使用参数--add-drop-database在每个“create database”语句之前向转储添加“drop database”语句。

eg 例如

mysqldump -hSERVER -uUSER -pPASSWORD --all-databases --add-drop-database >filename.sql mysqldump -hSERVER -uUSER -pPASSWORD --all-databases --add-drop-database> filename.sql

see here for details. 看到这里了解详情。

There's nothing magic about the dump and restore processes you describe. 您描述的转储和恢复过程没有任何魔力。 mysqldump writes out SQL statements that describe the current state of the database or databases you are dumping. mysqldump写出描述您要转储的数据库或数据库的当前状态的SQL语句。 It has to fetch a list of tables in each database you're dumping, then it has to read the tables one by one and write them out as SQL. 它必须在您要转储的每个数据库中获取一个表列表,然后它必须逐个读取这些表并将它们写为SQL。 On databases of any size, this takes time. 在任何规模的数据库上,这需要时间。

So, if you create a new table while mysqldump is running, it may not pick up that new table. 因此,如果在mysqldump运行时创建新表,则可能无法获取该新表。 Similarly, if your application software changes contents of tables while mysqldump is running, those changes may or may not show up in the backup. 同样,如果您的应用程序软件在mysqldump运行时更改了表的内容,则这些更改可能会也可能不会显示在备份中。

You can look at the .sql files mysqldump writes out to see what they have picked up. 您可以查看mysqldump写出的.sql文件以查看它们已经获取的内容。 If you want to be sure that your dumped .sql files are perfect, you need to run mysqldump on a quiet server -- one where nobody is running data definition language. 如果你想确保你的转储.sql文件是完美的,你需要在一个安静的服务器上运行mysqldump - 没有人运行数据定义语言。

MySQL hot backup solutions are available. MySQL热备份解决方案可用。 You may need to look into that. 你可能需要研究一下。

The OP may want look into OP可能需要调查

mysql_install_db

if they want a fresh start with the post-install default settings before restoring one or more dumped DBs. 如果他们想要在恢复一个或多个转储的数据库之前重新启动安装后的默认设置。 For production servers, another useful script is: 对于生产服务器,另一个有用的脚本是:

mysql_secure_installation

Also, they may prefer to dump the DB(s) they created separately: 此外,他们可能更喜欢转储他们单独创建的数据库:

mysqldump -hSERVER -uUSER -pPASSWORD --database foo > foo.sql

to avoid inadvertently changing the internal DBs: mysql, information_schema, performance_schema. 避免无意中更改内部数据库:mysql,information_schema,performance_schema。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM