[英]mysqldump is not dumping any table
I can't manage to backup my MySQL database.我无法备份我的 MySQL 数据库。 I use the following command:
我使用以下命令:
mysqldump --user=user --password=password db > db.sql
This is what the output file looks like:这是 output 文件的样子:
-- 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 */;
That's it.而已。 Not any
CREATE TABLE
and not any INSERT
... I also noticed in posts for similar problems that there always was a -- Dump completed on YYYY-MM-DD HH:MM:SS
at the end of the output, which is not the case here.没有任何
CREATE TABLE
也没有任何INSERT
...我还在针对类似问题的帖子中注意到,在 output 的末尾总是有一个-- Dump completed on YYYY-MM-DD HH:MM:SS
,这不是案例在这里。 My username and my password are correct and the user has all possible privileges.我的用户名和密码正确,用户拥有所有可能的权限。
What am I doing wrong?我究竟做错了什么?
Try this: 尝试这个:
mysqldump -u username -p DB_Name > dumpfile.sql
It should work. 它应该工作。
Try the same but with the normal mysql command, ie, if you have 尝试相同的操作,但使用正常的mysql命令,即,如果您有
mysqldump --user=root --password=secret db
then try 然后尝试
mysql --user=root --password=secret db
You should be able to see all tables and data that way, if you're not it's probably the user that's wrong. 您应该能够以这种方式查看所有表和数据,如果不是,那可能是用户错了。
Since asking this question, my web host deactivated the shell_exec()
command, so now I'm using a PHP script to backup my database. 既然问了这个问题,我的Web主机就停用了
shell_exec()
命令,所以现在我正在使用PHP脚本来备份数据库。 Here's the function I'm using: 这是我正在使用的功能:
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);
}
Example usage: backup_tables('localhost','user','pass','database', 'users, posts, comments, subscriptions', '/home/user/db_backups/');
用法示例:
backup_tables('localhost','user','pass','database', 'users, posts, comments, subscriptions', '/home/user/db_backups/');
. 。 You just have to remember to put the tables with foreign keys at the end of the list.
您只需要记住将带有外键的表放在列表的末尾。
It also happens if the version of mysqldump is lower than the server version.如果 mysqldump 的版本低于服务器版本,也会发生这种情况。
试试这个mysql -u root -p db_name <mydb.sql
This worked for me. 这对我有用。 Please try it once
请尝试一次
mysql -u <username> -p"<password>" database_name < sqlfile.sql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.