0

我无法备份我的 MySQL 数据库。我使用以下命令:

mysqldump --user=user --password=password db > db.sql

这是输出文件的样子:

-- MySQL dump 10.11
--
-- Host: localhost    Database: db
-- ------------------------------------------------------
-- Server version   5.0.96-community

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

就是这样。没有CREATE TABLE,也没有INSERT……我还在类似问题的帖子中注意到,-- Dump completed on YYYY-MM-DD HH:MM:SS输出的末尾总是有 a ,这里不是这种情况。我的用户名和密码正确,用户拥有所有可能的权限。

我究竟做错了什么?

4

5 回答 5

2

试试这个:

mysqldump -u username -p DB_Name > dumpfile.sql  

它应该工作。

于 2013-05-15T18:47:01.590 回答
1

尝试相同但使用正常的mysql命令,即,如果你有

mysqldump --user=root --password=secret db

然后尝试

mysql --user=root --password=secret db

您应该能够以这种方式查看所有表和数据,如果不是,则可能是用户错了。

于 2013-01-12T15:39:26.683 回答
1

自从提出这个问题后,我的网络主机停用了该shell_exec()命令,所以现在我使用 PHP 脚本来备份我的数据库。这是我正在使用的功能:

function backup_tables($host, $user, $pass, $name, $tables='*', $backup_path) {
    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($name,$link);

    //get all of the tables
    if($tables == '*') {
        $tables = array();
        $result = mysql_query('SHOW TABLES');

        while($row = mysql_fetch_row($result)) {
            $tables[] = $row[0];
        }
    }
    else {
        $tables = is_array($tables) ? $tables : explode(', ',$tables);
    }

    $droptables = 'DROP TABLE IF EXISTS ';

    for ($i = sizeof($tables) - 1; $i >= 0; $i--) {
        $droptables .= '`' . $tables[$i] . '`';
        if ($i > 0) {
            $droptables .= ', ';
        } else {
            $droptables .= ';';
        }
    }

    //cycle through
    foreach($tables as $table) {
        $result = mysql_query('SELECT * FROM '.$table);
        $num_fields = mysql_num_fields($result);

        // $droptables.= '`' . $table . '`, ';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
        $data .= "\n\n".$row2[1].";\n\n";

        for ($i = 0; $i < $num_fields; $i++) {
            while($row = mysql_fetch_row($result)) {
                $data .= 'INSERT INTO '.$table.' VALUES(';

                for ($j = 0; $j < $num_fields; $j++) {
                    if (isset($row[$j])) {
                        $row[$j] = addslashes($row[$j]);
                        $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                        $data .= '"'.$row[$j].'"' ;
                    } else {
                        $data .= 'NULL';
                    }

                    if ($j < ($num_fields-1)) {
                        $data .= ',';
                    }
                }

                $data .= ");\n";
            }
        }

        $data .= "\n\n\n";
    }

    $return = $droptables . $return;

    //save file
    $date = date('Ymd-His');
    $filename = 'db-backup-' . $date . '.sql';
    $handle = fopen($backup_path.$filename,'w+');
    fwrite($handle, utf8_encode($return));
    fclose($handle);

    $gzfile = $filename . '.gz';
    $handle = gzopen($backup_path.$gzfile, 'w9');
    gzwrite($handle, file_get_contents($backup_path.$filename));
    gzclose($handle);
}

示例用法:backup_tables('localhost','user','pass','database', 'users, posts, comments, subscriptions', '/home/user/db_backups/');. 您只需要记住将带有外键的表放在列表的末尾。

于 2013-05-19T13:29:25.287 回答
-3

试试这个 mysql -u root -p db_name < mydb.sql

于 2013-06-27T10:48:41.890 回答
-3

这对我有用。请尝试一次

mysql -u <username> -p"<password>" database_name < sqlfile.sql
于 2016-12-14T07:28:38.870 回答