如何使用php从服务器导出和导入mysql数据库?
问问题
234 次
1 回答
1
对于导出和导入,我使用 SSH 命令,它非常快。
对于导出数据库:
$db_host = '15.2.4.222’; // database server
$db_user = 'db_user'; // database user name
$db_pwd = 'dbpasword'; // database password
$db_schema = 'db_name'; // name of database to be exported
exec(sprintf(
'mysqldump --opt -h%s -u%s -p%s %s | gzip > /var/www/vhosts/yoursitename.com/httpdocs/ backups_db/backup_filename_%s.sql.gz',
$db_host,
$db_user,
$db_pwd,
$db_schema,
$today
));
exit;
现在进口:
$db_host = '15.2.4.222’; // database server
$db_user = 'db_user'; // database user name
$db_pwd = 'dbpasword'; // database password
$db_schema = 'db_name'; // name of database to be exported
// Command to restore database
exec("mysql -h [db server ip here] -u [db username here] –p[password here] [db name here] < /var/www/vhosts/yoursite.com/httpdocs/foldername_wherefile_exist/filename.sql");
// Example:
exec("mysql -h 15.2.4.222 -u db_username -ptestpassword test_db_name < /var/www/vhosts/phproots.com/httpdocs/backups_db/filename.sql");
请使用它来备份和恢复数据库。
于 2013-03-22T11:23:06.323 回答