创建mysqldump备份数据库

时间:2013-10-21 11:04:57

标签: mysql

我知道mysqldump是如何运作的。 但是不知道在哪里使用它?

如果我在启动mysql程序后执行此命令,则表示错误。

我正在使用ubuntu。那么我该如何使用这个实用程序呢?

5 个答案:

答案 0 :(得分:1)

是的,你可以。

有关该工具的详情,请参阅http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html

答案 1 :(得分:1)

mysqldump -u MYSQL_USER -h MYSQL_SERVER -pMYSQL_PASS --all-databases > "dbs.sql"

答案 2 :(得分:1)

您可以直接在终端上使用它,就像它自己mysql一样,并将参数直接传递给它。

mysqldump -u [user] -p[password] [database name] > dumpfilename.sql

答案 3 :(得分:1)

以这种方式备份您的数据库..

mysql -u root -p DB_NAME > db_name_backup.sql

如果要备份所有数据库,只需运行此

即可
mysql -u root -p > mysql_db_backup.sql

您将在此处了解有关mysql和mysqldump的更多信息..

<强>指南:
mysqldump and mysql
MySQL Database Backup using mysqldump

壳&GT; mysqldump --opt db_name&gt;备份file.sql

You can read the dump file back into the server like this:

shell> mysql db_name < backup-file.sql

Or like this:

shell> mysql -e "source /path-to-backup/backup-file.sql" db_name

mysqldump is also very useful for populating databases by copying data
from one MySQL server to another:

shell> mysqldump --opt db_name | mysql --host=remote_host -C db_name

It is possible to dump several databases with one command:

shell> mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql

If you want to dump all databases, use the --all-databases option:

shell> mysqldump --all-databases > all_databases.sql

If tables are stored in the InnoDB storage engine, mysqldump provides a
way of making an online backup of these (see command below). This
backup just needs to acquire a global read lock on all tables (using
FLUSH TABLES WITH READ LOCK) at the beginning of the dump. As soon as
this lock has been acquired, the binary log coordinates are read and
lock is released. So if and only if one long updating statement is
running when the FLUSH...  is issued, the MySQL server may get stalled
until that long statement finishes, and then the dump becomes
lock-free. So if the MySQL server receives only short (in the sense of
"short execution time") updating statements, even if there are plenty
of them, the initial lock period should not be noticeable.

shell> mysqldump --all-databases --single-transaction > all_databases.sql

For point-in-time recovery (also known as “roll-forward”, when you need
to restore an old backup and replay the changes which happened since
that backup), it is often useful to rotate the binary log (see
Section 8.4, “The Binary Log”) or at least know the binary log
coordinates to which the dump corresponds:

shell> mysqldump --all-databases --master-data=2 > all_databases.sql
or
shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql

The simultaneous use of --master-data and --single-transaction works as
of MySQL 4.1.8. It provides a convenient way to make an online backup
suitable for point-in-time recovery if tables are stored in the InnoDB
storage engine.

For more information on making backups, see Section 6.1, “Database
Backups”.

答案 4 :(得分:1)

如果是整个数据库,那么:

  

$ mysqldump -u [uname] -p [pass] db_name&gt; db_backup.sql

如果是所有数据库,那么:

  

$ mysqldump -u [uname] -p [pass] --all-databases&gt; all_db_backup.sql

如果是DB中的特定表,那么:

  

$ mysqldump -u [uname] -p [pass] db_name table1 table2&gt;   table_backup.sql

您甚至可以使用gzip自动压缩输出(如果您的数据库非常大):

  

$ mysqldump -u [uname] -p [pass] db_name | gzip&gt; db_backup.sql.gz

如果您想远程执行此操作并且您可以访问相关服务器,那么以下操作(假设MySQL服务器位于端口3306上):

  

$ mysqldump -P 3306 -h [ip_address] -u [uname] -p [pass] db_name&gt;   db_backup.sql

IMPORT

使用以下命令导入sql数据文件:

  

$ mysql -u username -p -h localhost DATA-BASE-NAME&lt; data.sql

在此示例中,使用vivek将'data.sql'文件导入'blog'数据库作为用户名:

  

$ mysql -u sat -p -h localhost blog&lt; data.sql

如果您有专用数据库服务器,请将localhost主机名替换为实际服务器名称或IP地址,如下所示:

  

$ mysql -u username -p -h 202.54.1.10 databasename&lt; data.sql

或使用主机名,例如mysql.cyberciti.biz

  

$ mysql -u username -p -h mysql.cyberciti.biz database-name&lt; data.sql

如果您不知道sql dump中包含数据库名称或数据库名称,您可以尝试以下内容:

  

$ mysql -u username -p -h 202.54.1.10&lt; data.sql

参考:http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html