PHP - PDO mysql 备份到远程主机?
欢迎,
我如何使用 PHP PDO 创建数据库(内部所有表)到远程 mysql 服务器的完整备份?
有什么简单的方法吗?
Welcome,
How can i create a full backup of my database (all tables inside) to remote mysql server using PHP PDO ?
Is there any easy way ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将从有关复制的 MySQL 文档开始。我不建议使用 PHP 和 PDO 执行此操作。当您使用正确的工具来完成工作时,您将获得更好的结果。恕我直言。
I would start with the MySQL documentation on replication. I would not recommend doing this with PHP and PDO. You'll have better results when you use the right tool for the job. IMHO.
您必须对源数据库中的每个表执行 SELECT * FROM [table],然后在目标数据库上运行一个大事务,删除所有先前的表数据,然后插入新的表数据数据。
基本上类似于此代码片段:
注意:
中的
$destDB->quote()
array_map() 应该是安全的,但我不确定这是否适用于所有数据类型和所有数据库系统。编辑:
另外还可以使用特殊的 INFORMATION_SCHEMA 数据库 来查询表名并自动确定外键依赖关系。
另外,您可能需要禁用外键检查<当您具有自引用外键关系时,在传输过程中在目标数据库上执行 /a>(并在之后重新启用它们),以防止由于违反约束而导致失败。
You'll have to perform a
SELECT * FROM [table]
on every table you have in your source database, then run a big transaction on your destination database that deletes all previous table data and then inserts the new data.Basically something similar to this snippet:
Notes:
$destDB->quote()
in thearray_map()
should be safe then, but I don't know for sure if this applies for all data types and all database systems.mysqldump
or the like to verify that they are identical and that the backup is complete. You might even encounter nasty PDO bugs in the worst case that you may need to work around – Google and SO are your friends :-).EDIT:
One could additionally use the special INFORMATION_SCHEMA database to query the table names and determine the foreign key dependencies automatically.
Also, you might want to disable foreign key checks on the target database during transfer (and reenable them afterwards) when you have self-referential foreign key relations to prevent failures due to constraint violations.
如果涉及备份,您应该真正坚持使用现有的管理工具。使用 SQLBuddy 或 PHPMySQLAdmin。
您可以使用 RPC 方法或使用 JSON 等数据传输来编写复制系统。但这需要保护,速度会很慢,并且只对内容传输有帮助,对 SQL 模式复制没有帮助。
If it's about having a backup, you should really stick to established admin tools. Use SQLBuddy or PHPMySQLAdmin.
You could write a duplication system using a RPC method or data transfer with JSON or something. But that needs securing, would be slow and only helpful for content transfer, not SQL schema replication.