如何在PHP中备份MySQL数据库?

问题描述:

我从互联网上获得了本教程.但是我什至对如何通过 MySQL 数据库的方法没有基本的了解. ="http://en.wikipedia.org/wiki/PHP" rel ="noreferrer"> PHP 可以使用.

I got this tutorial from the Internet. But I don't even have a basic understanding on how backup of a MySQL database through PHP would work.

使用PHP备份MySQL数据库

您能推荐一些我可以参考的网站吗?

Can you recommend some sites that I can use as a reference so that I can study it?

虽然您可以从PHP执行备份命令,但它们实际上与PHP没有任何关系.都是关于MySQL的.

While you can execute backup commands from PHP, they don't really have anything to do with PHP. It's all about MySQL.

我建议使用mysqldump实用程序备份数据库.可以在这里找到文档: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html .

I'd suggest using the mysqldump utility to back up your database. The documentation can be found here : http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html.

mysqldump的基本用法是

The basic usage of mysqldump is

mysqldump -u user_name -p name-of-database >file_to_write_to.sql

然后您可以使用类似的命令来还原备份

You can then restore the backup with a command like

mysql -u user_name -p <file_to_read_from.sql

您可以使用cron吗?我建议制作一个运行mysqldump作为cron作业的PHP脚本.就像

Do you have access to cron? I'd suggest making a PHP script that runs mysqldump as a cron job. That would be something like

<?php

$filename='database_backup_'.date('G_a_m_d_y').'.sql';

$result=exec('mysqldump database_name --password=your_pass --user=root --single-transaction >/var/backups/'.$filename,$output);

if($output==''){/* no output is good */}
else {/* we have something to log the output here*/}

如果mysqldump不可用,本文将介绍使用SELECT INTO OUTFILELOAD DATA INFILE命令的另一种方法.与PHP的唯一连接是您正在使用PHP连接到数据库并执行SQL命令.您也可以从命令行MySQL程序MySQL监视器执行此操作.

If mysqldump is not available, the article describes another method, using the SELECT INTO OUTFILE and LOAD DATA INFILE commands. The only connection to PHP is that you're using PHP to connect to the database and execute the SQL commands. You could also do this from the command line MySQL program, the MySQL monitor.

这非常简单,您可以使用一个命令编写一个SQL文件,并在需要恢复时加载/执行该文件.

It's pretty simple, you're writing an SQL file with one command, and loading/executing it when it's time to restore.

您可以在此处中找到要选择的文档(只需在页面上搜索外文件). LOAD DATA INFILE本质上是相反的.有关文档,请参见此处.

You can find the docs for select into outfile here (just search the page for outfile). LOAD DATA INFILE is essentially the reverse of this. See here for the docs.