Home Database Mysql Tutorial mysql mysqldump数据库备份命令介绍

mysql mysqldump数据库备份命令介绍

Jun 07, 2016 pm 05:51 PM
mysql backup

文章收集了大量的关于mysqldump命令的语句与实例,同时也有介绍国外网站上的说明用法,有需要了解的朋友可以参考一下。

 代码如下 复制代码
dump -hhostname -uusername -ppassword databasename > backupfile.sql

备份MySQL为带删除表的格式备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库。

mysqldump ---add-drop-table -uusername -ppassword databasename > backupfile.sql直接将MySQL数据库压缩备份

 代码如下 复制代码
mysqldump -hhostname -uusername -ppassword databasename | gzip > backupfile.sql.gz

备份MySQL数据库某个(些)表

 代码如下 复制代码
mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2 > backupfile.sql

同时备份多个MySQL数据库

 代码如下 复制代码
mysqldump -hhostname -uusername -ppassword --databases databasename1 databasename2 databasename3 > multibackupfile.sql

仅仅备份数据库结构

 代码如下 复制代码
mysqldump --no-data --databases databasename1 databasename2 databasename3 > structurebackupfile.sql

备份服务器上所有数据库

 代码如下 复制代码

mysqldump --all-databases  allbackupfile.sql还原MySQL数据库的命令

mysql -hhostname -uusername -ppassword databasename

gunzip

mysqldump -uusername -ppassword databasename | mysql --host=*.*.*.* -C databasename压缩备份

备份并用gzip压缩:

 代码如下 复制代码

mysqldump | gzip > outputfile.sql.gz从gzip备份恢复:

gunzip 备份并用bzip压缩:

mysqldump | bzip2 > outputfile.sql.bz2从bzip2备份恢复:

bunzip2


mysqldump支持下列选项:  

--add-locks  
在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(为了使得更快地插入到MySQL)。  
--add-drop-table  
在每个create语句之前增加一个drop table。  
--allow-keywords  
允许创建是关键词的列名字。这由表名前缀于每个列名做到。  
-c, --complete-insert  
使用完整的insert语句(用列名字)。  
-C, --compress  
如果客户和服务器均支持压缩,压缩两者间所有的信息。  
--delayed  
用INSERT DELAYED命令插入行。  
-e, --extended-insert  
使用全新多行INSERT语法。(给出更紧缩并且更快的插入语句)  
-#, --debug[=option_string]  
跟踪程序的使用(为了调试)。  
--help  
显示一条帮助消息并且退出。  
--fields-terminated-by=...  
   
--fields-enclosed-by=...  
   
--fields-optionally-enclosed-by=...  
   
--fields-escaped-by=...  
   
--fields-terminated-by=...  
这些选择与-T选择一起使用,并且有相应的LOAD DATA INFILE子句相同的含义。  
LOAD DATA INFILE语法。  
-F, --flush-logs  
在开始导出前,洗掉在MySQL服务器中的日志文件。  
-f, --force,  
即使我们在一个表导出期间得到一个SQL错误,继续。  
-h, --host=..  
从命名的主机上的MySQL服务器导出数据。缺省主机是localhost。  
-l, --lock-tables.  
为开始导出锁定所有表。  
-t, --no-create-info  
不写入表创建信息(CREATE TABLE语句)  
-d, --no-data  
不写入表的任何行信息。如果你只想得到一个表的结构的导出,这是很有用的!  
--opt  
同--quick --add-drop-table --add-locks --extended-insert --lock-tables。  
应该给你为读入一个MySQL服务器的尽可能最快的导出。  
-pyour_pass, --password[=your_pass]  
与服务器连接时使用的口令。如果你不指定“=your_pass”部分,mysqldump需要来自终端的口令。  
-P port_num, --port=port_num  
与一台主机连接时使用的TCP/IP端口号。(这用于连接到localhost以外的主机,因为它使用 Unix套接字。)  
-q, --quick  
不缓冲查询,直接导出至stdout;使用mysql_use_result()做它。  
-S /path/to/socket, --socket=/path/to/socket  
与localhost连接时(它是缺省主机)使用的套接字文件。  
-T, --tab=path-to-some-directory  
对于每个给定的表,创建一个table_name.sql文件,它包含SQL CREATE 命令,和一个table_name.txt文件,它包含数据。 注意:这只有在mysqldump运行在mysqld守护进程运行的同一台机器上的时候才工作。.txt文件的格式根据--fields-xxx和--lines--xxx选项来定。  
-u user_name, --user=user_name  
与服务器连接时,MySQL使用的用户名。缺省值是你的Unix登录名。  
-O var=option, --set-variable var=option设置一个变量的值。可能的变量被列在下面。  
-v, --verbose  
冗长模式。打印出程序所做的更多的信息。  
-V, --version  
打印版本信息并且退出。  
-w, --where=@#where-condition@#  
只导出被选择了的记录;注意引号是强制的!  
"--where=user=@#jimf@#" "-wuserid>1" "-wuserid


MySQL Dump/Restore


Dump ALL MySQL Databases

 代码如下 复制代码
mysqldump --user=XXXXXXXX --password=XXXXXXX -A > /PATH/TO/DUMPFILE.SQL

 

Dump Individual or Multiple MySQL Databases

 代码如下 复制代码
mysqldump --user=XXXXXXXX --password=XXXXXXX --databases DB_NAME1 DB_NAME2 DB_NAME3 > /PATH/TO/DUMPFILE.SQL

 

Dump only certain tables from a MySQL Database

 代码如下 复制代码
mysqldump --user=XXXXXXXX --password=XXXXXXXX --databases DB_NAME --tables TABLE_NAME > /PATH/TO/DUMPFILE.SQL

 

I'm using MySQL 4.1.8 on my development server, but am behind a few releases on our production server. In order to make dum compatible with the old MySQL version, add the following switch:  --compatible=mysql323
--------------------------------------------------------------------------------
Use the following procedure to reload the contents of a database:
Unzip the backup file you wish to use.
Open it up and pull out only the information that you will need.
Save this text file.
Use the following command to feed back in the contents of a text file:

 代码如下 复制代码
mysql --verbose --user=XXXXXXXX --password=XXXXXXXX DB_NAME


Restoring MySQL database.
The mysqldump utility is used only to take the MySQL dump. To restore the database from the dump file that you created in previous step, use mysql command.

 代码如下 复制代码

shell> mysql --u [username] --password=[password] [database name]

Example:

 代码如下 复制代码


shell> mysql --user root --password=myrootpassword new_db

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
How to backup and restore database after mysql installation How to backup and restore database after mysql installation Apr 08, 2025 am 11:45 AM

There is no absolutely optimal MySQL database backup and recovery solution, and it needs to be selected based on the amount of data, business importance, RTO and RPO. 1. Logical backup (mysqldump) is simple and easy to use, suitable for small databases, but slow and huge files; 2. Physical backup (xtrabackup) is fast, suitable for large databases, but is more complicated to use. The backup strategy needs to consider the backup frequency (RPO decision), backup method (data quantity and time requirement decision) and storage location (off-site storage is more secure), and regularly test the backup and recovery process to avoid backup file corruption, permission problems, insufficient storage space, network interruption and untested issues, and ensure data security.

How to use MySQL data backup and recovery tools for disaster recovery How to use MySQL data backup and recovery tools for disaster recovery Aug 02, 2023 am 09:06 AM

How to use MySQL data backup and recovery tools to achieve disaster recovery. Data backup and recovery are a very important part of the database management process. Backing up your data protects your database from accidental corruption, hardware failure, or other catastrophic events. As a popular relational database management system, MySQL provides some powerful tools to achieve data backup and recovery. This article will introduce how to use MySQL's data backup and recovery tools to achieve disaster recovery. MySQL data backup tool-mysql

How to effectively manage and maintain ibd files in MySQL database How to effectively manage and maintain ibd files in MySQL database Mar 16, 2024 am 11:21 AM

In the MySQL database, each InnoDB table corresponds to an .ibd file, which stores the table's data and indexes. Therefore, for the management and maintenance of MySQL database, the management of ibd files is also particularly important. This article will introduce how to effectively manage and maintain ibd files in a MySQL database and provide specific code examples. 1. Check and optimize table space First, we can check the disk space usage of the table using the following SQL statement: SELECTTAB

Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Jun 15, 2023 pm 03:28 PM

MySql is a commonly used relational database management system that is widely used in various business and application scenarios. For MySQL backup issues, the selection and execution method of the backup plan are crucial. In this article, we will introduce various backup options and how to create and restore MySQL backups efficiently. 1. Selection of backup plan In the process of selecting a MySQL backup plan, you should choose a backup plan that suits you based on the business scenario and actual situation. Cold backup The so-called cold backup is to complete the MySQL database.

MySql database backup: How to achieve efficient MySQL database backup and recovery MySql database backup: How to achieve efficient MySQL database backup and recovery Jun 15, 2023 pm 11:37 PM

MySQL is one of the most widely used relational database management systems currently. Its efficiency and reliability make it the first choice for many enterprises and developers. But for various reasons, we need to back up the MySQL database. Backing up a MySQL database is not an easy task because once the backup fails, important data may be lost. Therefore, in order to ensure data integrity and recoverability, some measures must be taken to achieve efficient MySQL database backup and recovery. This article will introduce how to achieve

MySQL rolling backup techniques for data MySQL rolling backup techniques for data Jun 15, 2023 pm 07:47 PM

MySQL is a popular relational database that is widely used in various fields. However, like other applications, MySQL has risks such as data corruption, crashes, and malicious attacks. Therefore, backing up your data is crucial. Backups can provide security and some form of "undo" functionality to data, reducing or even eliminating instability and risk. The most common backup types are full backup and incremental backup. However, if you need frequent, real-time backups, rolling backups are a better approach. A rolling backup is when an acceptable

A comprehensive guide to MySQL backup and recovery A comprehensive guide to MySQL backup and recovery Jun 15, 2023 am 09:48 AM

MySQL is currently one of the most popular relational database management systems and is widely used in enterprise-level applications. Whether you are a developer or a data administrator, you need to understand the basic knowledge of MySQL backup and recovery. Backup and recovery not only help enterprises protect data, but also enable systems to respond quickly to adverse situations and restore them to normal operating conditions as much as possible. This article will detail the steps for MySQL backup and recovery and provide some best practices to help readers go further in protecting their MySQL databases.

How do you back up and restore a MySQL database? How do you back up and restore a MySQL database? Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

See all articles