如何使用php从服务器导出和导入mysql数据库

时间:2013-03-22 11:13:27

标签: import phpmyadmin export

如何使用php从服务器导出和导入mysql数据库?

1 个答案:

答案 0 :(得分:1)

对于导出和导入,我使用SSH命令,它非常快。

对于Export Databse:

$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");

请用它来备份和恢复数据库。