One of the most important task of a database administrator is backing up and restoring databases. MySQL is a popular database server and provides tool named mysqldump for backup and restore operations. We will look different usage types of mysqldump in this tutorial.

数据库管理员最重要的任务之一就是备份和还原数据库。 MySQL是一种流行的数据库服务器,并提供名为mysqldump工具用于备份和还原操作。 在本教程中,我们将介绍mysqldump的不同用法类型。

句法 (Syntax)

mysqldump [OPTIONS] database [tables] 
OR     
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] 
OR     
mysqldump [OPTIONS] --all-databases [OPTIONS]

帮帮我 (Help)

$ mysqldump --help
在这里插入图片描述

连接远程数据库服务器(Connect Remote Database Server)

By default mysqldump will try to connect local MySQL server database. Remote databases server can be specified with -h option as IP address or domain name. In this example we will try to connect host named poftut1 .

默认情况下,mysqldump将尝试连接本地MySQL服务器数据库。 可以使用-h选项将远程数据库服务器指定为IP地址或域名。 在此示例中,我们将尝试连接名为poftut1主机。

$ mysqldump -h poftut1 -u root -p  mysql > back.sql

备份单个数据库 (Backup Single Database)

Mysqldump have a lot of usage possibilities but the most popular one is backing up single database. This will only backup single specified database. We will specify user name and password with database name. Username will be specified with -u options and password specified with -p option. We will specify database name after these information without an option.

Mysqldump有很多使用可能性,但是最流行的一种是备份单个数据库。 这将仅备份单个指定的数据库。 我们将使用数据库名称指定用户名和密码。 用-u选项指定用户名,用-p选项指定密码。 我们将在这些信息之后指定数据库名称,而无需任何选择。

$ mysqldump -u root -p mysql > mysql.sql

在这里插入图片描述

By default backup output is written to the standard output. But in this example we have redirected the output to the file named mysql.sql . the backup file is an sql file which holds sql schemas and data.

默认情况下,备份输出将写入标准输出。 但是在此示例中,我们将输出重定向到名为mysql.sql的文件。 备份文件是一个sql文件,其中包含sql模式和数据。
备份多个数据库 (Backup Multiple Databases)

We can backup multiple databases just adding more databases to the end of the mysqldump command. In this example we will backup databases mysql , sys . We will use --databases option.

我们可以备份多个数据库,只需在mysqldump命令的末尾添加更多数据库即可。 在此示例中,我们将备份数据库mysql , sys 。 我们将使用–databases选项。

$ mysqldump -u root -p --databases mysql sys  > bak.sql

在这里插入图片描述

备份所有数据库(Backup All Databases)

In previous example we have specified multiple databases for backup. If we need to backup all databases in the database server specifying them one by one is error prone work. We can use --all-databases command to backup all databases without specifying them one by one.

在前面的示例中,我们指定了多个数据库进行备份。 如果我们需要备份数据库服务器中的所有数据库,则一一指定它们是容易出错的工作。 我们可以使用–all-databases命令来备份所有数据库,而无需一一指定。

$ mysqldump -u root -p --all-databases  > bak.sql

在这里插入图片描述

备份单表(Backup Single Table)

Sometimes our application databases uses only single table. All transactions are done in a single database. In this situation we may need backup single table. We can specify the table name after the database name which contains the table. In this example we will backup table named general_log which resides in database mysql

有时我们的应用程序数据库仅使用单个表。 所有交易均在单个数据库中完成。 在这种情况下,我们可能需要备份单个表。 我们可以在包含表的数据库名称之后指定表名称。 在此示例中,我们将备份数据库mysql中名为general_log表

$ mysqldump -u root -p mysql general_log > mysql_general_log.sql

在这里插入图片描述

备份到远程(Backup To Remote)

Backup on the local directory is practical but some times we may need to backup the databases to the remote server. There are different ways to backup remote directory like scp, file share etc. We prefer a pratical one where we will use ssh command to redirect to backup content to the remote server. We provide password of the database server in command line. In this example we will copy database named mysql to the remote system or server named ubu2 with shh by using cat > big.sql command.

在本地目录上备份是可行的,但是有时我们可能需要将数据库备份到远程服务器。 有多种备份远程目录的方法,例如scp,文件共享等。我们更喜欢一种实用的方法,我们将使用ssh命令将备份内容重定向到远程服务器。 我们在命令行中提供数据库服务器的密码。 在本示例中,我们将使用cat > big.sql命令,使用shh将名为mysql数据库复制到名为ubu2的远程系统或服务器。

$ mysqldump -u root -p123456  mysql | ssh ubu2 'cat > big.sql'

Check remote backup file can be done with following command.

可以使用以下命令检查远程备份文件。

$ ssh ubu2 head big.sql

在这里插入图片描述

仅备份数据库架构(Backup Only Database Schema)

The backup of databases, tables will take copy of the schema of databases, tables and currently stored data on these tables and databases. If the data is test data which is not important for us or if we want to create clone of the current database and tables without data we can omit the data. We wil use --no-data or -d option to specify this.

数据库,表的备份将复制数据库,表的架构以及这些表和数据库上当前存储的数据的副本。 如果数据是对我们来说不重要的测试数据,或者如果我们想创建没有数据的当前数据库和表的克隆,则可以忽略数据。 我们将使用–no-data或-d选项进行指定。

$ mysqldump -u root -p -d mysql  > schema.sql

在这里插入图片描述

压缩备份(Compress Backup)

Creating backups take time and storage. Especially if we have more than one backup there will be need a lot of storage. As we know the backup files are sql format by default. Sql is a text file and have a high compression rate. We can write backup as compressed format which will make storage usage very effective. We will use gzip to enable msqldump compression.

创建备份需要时间和存储空间。 特别是如果我们有多个备份,则将需要大量存储。 众所周知,默认情况下备份文件是sql格式。 Sql是一个文本文件,压缩率很高。 我们可以将备份写为压缩格式,这将使存储使用非常有效。 我们将使用gzip启用msqldump压缩。

$ mysqldump -u root -p  mysql | gzip > back.gz

在这里插入图片描述

压缩服务器客户端传输(Compress Server Client Transmission)

Another useful performance option is compression transmission between mysqldump client and MySQL server. As there may be a bulk of data while transmitting them through network compressing is a good habit for network performance . We will use -C or --compress options to enable compression.

另一个有用的性能选项是mysqldump客户端和MySQL服务器之间的压缩传输。 由于通过网络压缩传输数据时可能会有大量数据,因此这是网络性能的好习惯。 我们将使用-C或–compress选项来启用压缩。

$ mysqldump -h localhost -u root -p  mysql -C > back.sql

强制备份操作 (Force Backup Operation)

While taking backup some times errors or warnings occurs. These warnings and errors can interrupt the backup process b$ mysql -h poftut1 -u root -p < back.sqly default. We can use -f option to force and omit the errors and warnings.

进行备份时,有时会发生错误或警告。 这些警告和错误可能会中断备份过程b $ mysql -h poftut1 -u root -p <back.sqly默认值。 我们可以使用-f选项强制并忽略错误和警告。

$ mysqldump -u root -p -f  mysql > back.sql

恢复和导入数据库 (Restore and Import Database)

We will use mysql tool to restore database. We assume that backup is in Sql format. We will redirect the backup file to the mysql command. While using mysql command we need to specify the username with -u and password with -p options.

我们将使用mysql工具还原数据库。 我们假定备份为Sql格式。 我们将备份文件重定向到mysql命令。 使用mysql命令时,我们需要使用-u指定用户名,并使用-p选项指定密码。

$ mysql -u root -p < back.sql

恢复远程数据库 (Restore Remote Database)

In order to restore database to the remote database server we will specify the remote databases server IP address or hostname. In order to specify IP address or hostname we will use -h option. In this example we use remote database server named poftut1

为了将数据库还原到远程数据库服务器,我们将指定远程数据库服务器的IP地址或主机名。 为了指定IP地址或主机名,我们将使用-h选项。 在此示例中,我们使用名为poftut1远程数据库服务器。

$ mysql -h poftut1 -u root -p < back.sql
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐