简体   繁体   English

命令恢复PHP中的MySQL数据库备份

[英]command restore mysql db backup in php

i'm having a problem when restoring the db file. 还原db文件时出现问题。 Below is that script that I use to restore: 以下是我用来还原的脚本:

<?php
$dbhost   = "localhost";
$dbuser   = "root";
$dbpwd    = "";
$dbname   = "fhc_test";

$dumpfile = "backup/".$_GET['id'].".sql";

exec("C://xampp/mysql/bin/mysql -u $dbuser -p $dbpwd $dbname < $dumpfile");
?>

Below is the script that I use to do the backup: 以下是用于执行备份的脚本:

<?php
$dbhost   = "localhost";
$dbuser   = "root";
$dbpwd    = "";
$dbname   = "fhc";
date_default_timezone_set("Asia/Singapore");

$dumpfile = "backup/". $dbname . "_" . date("d-m-Y_H_i_s") . ".sql";

exec("C://xampp/mysql/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
?>

There is no problem found when backup-ing the file. 备份文件时没有发现问题。 When i copy n paste the backup content into the database manually, it can restore perfectly without problem. 当我复制n并将备份内容手动粘贴到数据库中时,它可以完美还原而不会出现问题。 But when i'm running the restore script to perform the restore, the file keeps loading for so long without prompting any error. 但是,当我运行还原脚本以执行还原时,文件会保持加载很长时间而不会提示任何错误。 So, I stop the loading, and it seems like there is no tables been restored into the database. 因此,我停止了加载,并且似乎没有表还原到数据库中。

When i try to run the script from the cmd, the restore can be done completely and fast. 当我尝试从cmd运行脚本时,还原可以完全而快速地完成。 In this case, I put the backup file in the same folder as the mysql.exe 在这种情况下,我将备份文件放在与mysql.exe相同的文件夹中

This is how i do the command in the cmd: 这是我在cmd中执行命令的方式:

C:\xampp\mysql\bin>mysql -u root -p  fhc_test < fhc.sql

can anyone help me figure out what is the problem that i have in my restore script? 谁能帮助我找出还原脚本中存在的问题? bcos i have done so many checking and it seems like there is no error with the code. bcos我已经做了很多检查,而且代码似乎没有错误。 Do i have to copy the mysql.exe file from c:/xampp/mysql/bin and put it into the same folder where i put my php file? 我是否必须从c:/ xampp / mysql / bin复制mysql.exe文件,并将其放入放置php文件的文件夹中?

I really dunno what I have to do with this. 我真的不知道该怎么办。

Given that you use: 鉴于您使用:

C:\xampp\mysql\bin>mysql -u root -p  fhc_test < fhc.sql

it is the user root you are trying with. 它是您尝试使用的用户root。 Not phpmyadmin user. 不是phpmyadmin用户。 So you need to use the -p option to give the password for root. 因此,您需要使用-p选项来提供root用户的密码。

Run the command in PowerShell and you will get this error: 在PowerShell中运行命令,您将收到此错误:

The '<' operator is reserved for future use.

You are obviously running this in Windows so you need to adjust your syntax to this: 您显然是在Windows中运行此程序,因此需要将语法调整为:

Get-Content fhc.sql | mysql -u root -p  fhc_test

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

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