2013-03-05 23:35
浏览 70


I'm trying to backup one of my databases with mysqldump and I'm trying to export it to an xml file which already exists. Here is what I have:

$dbUser = 'user';    // db User
$dbPass = 'pass';    // db User Password
$dbName = 'db';      // db name
$dest   = $_SERVER['DOCUMENT_ROOT'] . 'backup'; // Path to directory

class MySQLDump {
private $cmd;

function MySQLDump($dbUser,$dbPass,$dbName,$dest) {
    $fname = $dbName.'.xml';
    $this->cmd='mysqldump -X -u'.$dbUser.' -p'.$dbPass.' '.$dbName.
        ' >'.$dest.'/'.$fname;

public function backup() {
    system($this->cmd, $error);
    if($error) {
        trigger_error ('Backup failed: ' . $error . '<br />Attempted: ' . $this->cmd);
} // end class def

$mysqlDump = new MySQLDump($dbUser, $dbPass, $dbName, $dest);  

This always generates the error thrown in the backup function. Here is a sample of $cmd's output:

mysqldump -X -udan -pdanPass danDB >/var/www/prod/dan/web/backup/danDB.xml

Any idea what's going wrong? I've never really used mysqldump so I'm not sure if I'm approaching this correctly. Any help is greatly appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongyou8087
    dongyou8087 2013-03-05 23:42

    try adding a space between -u and your $dbUser.

    点赞 评论
  • duandange7480
    duandange7480 2013-03-06 04:46

    According to Does mysqldump return a status? something is going wrong, yes. We can not say if a warning or an error because you didn't share the return code.

    Apart from that you can easily learn what exactly goes wrong by looking into STDERR output returned from the mysqldump command. E.g. you can pipe it into database.err:

    mysqldump -X -udan -pdanPass danDB >/path/to/backup/danDB.xml 2>database.err
    点赞 评论
  • dse3168
    dse3168 2014-03-19 15:49

    This one work for me

    mysqldump --user=xxx --password=xxx  databasename  --xml > file.xml
    点赞 评论