8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 3 升级 MySQL / 3.15 将 MySQL 数据库复制到另一台机器
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

3.15 将 MySQL 数据库复制到另一台机器3.15 Copying MySQL Databases to Another Machine

在需要在不同架构之间传输数据库的情况下,你可以使用 mysqldump 来创建包含 SQL 语句的文件。然后,你可以将该文件传输到另一台机器,并将其作为输入提供给 mysql 客户端。In cases where you need to transfer databases between different architectures, you can use mysqldump to create a file containing SQL statements. You can then transfer the file to the other machine and feed it as input to the mysql client.

使用 mysqldump --help 查看可用选项。Use mysqldump --help to see what options are available.

Note

如果在创建转储的服务器上使用了 GTID(gtid_mode=ON),默认情况下,mysqldump 会在转储中包含 gtid_executed 集的内容,以将其传输到新的机器上。根据所涉及的 MySQL 服务器版本,这一结果可能有所不同。请检查 mysqldump --set-gtid-purged 选项的描述,以了解在您使用的版本中会发生什么,以及如果默认行为的结果不适合您的情况,如何更改该行为。If GTIDs are in use on the server where you create the dump (gtid_mode=ON), by default, mysqldump includes the contents of the gtid_executed set in the dump to transfer these to the new machine. The results of this can vary depending on the MySQL Server versions involved. Check the description for the mysqldump --set-gtid-purged option to find what happens with the versions you are using, and how to change the behavior if the outcome of the default behavior is not suitable for your situation.

在两台机器之间移动数据库最简单(虽然不是最快)的方法是在数据库所在的机器上运行以下命令:The easiest (although not the fastest) way to move a database between two machines is to run the following commands on the machine on which the database is located:

mysqladmin -h 'other_hostname' create db_name
mysqldump db_name | mysql -h 'other_hostname' db_name
  

如果你想通过缓慢的网络从远程机器复制数据库,可以使用这些命令:If you want to copy a database from a remote machine over a slow network, you can use these commands:

mysqladmin create db_name
mysqldump -h 'other_hostname' --compress db_name | mysql db_name
  

你也可以将转储存储在一个文件中,将文件传输到目标机器,然后在那里的数据库中加载该文件。例如,你可以在源机器上将数据库转储到压缩文件中,如下所示:You can also store the dump in a file, transfer the file to the target machine, and then load the file into the database there. For example, you can dump a database to a compressed file on the source machine like this:

mysqldump --quick db_name | gzip > db_name.gz

将包含数据库内容的文件传输到目标机器,并在那里运行这些命令:Transfer the file containing the database contents to the target machine and run these commands there:

mysqladmin create db_name
gunzip < db_name.gz | mysql db_name
  

你也可以使用 mysqldump 和 mysqlimport 来传输数据库。对于大型表格,这比单纯使用 mysqldump 要快得多。在以下命令中,DUMPDIR 代表你用于存储 mysqldump 输出文件的目录的完整路径名。You can also use mysqldump and mysqlimport to transfer the database. For large tables, this is much faster than simply using mysqldump. In the following commands, DUMPDIR represents the full path name of the directory you use to store the output from mysqldump.

首先,创建用于输出文件的目录并导出数据库:First, create the directory for the output files and dump the database:

mkdir DUMPDIR
mysqldump --tab=DUMPDIR
   db_name
  

然后将 DUMPDIR 目录中的文件传输到目标机器上的相应目录,并在那里的 MySQL 中加载这些文件:Then transfer the files in the DUMPDIR directory to some corresponding directory on the target machine and load the files into MySQL there:

mysqladmin create db_name           # create database
cat DUMPDIR/*.sql | mysql db_name   # create tables in database
mysqlimport db_name
   DUMPDIR/*.txt   # load data into tables

不要忘记复制 mysql 数据库,因为权限表存储在那里。在新机器上,你可能需要以 MySQL root 用户身份运行命令,直到 mysql 数据库就位为止。Do not forget to copy the mysql database because that is where the grant tables are stored. You might have to run commands as the MySQL root user on the new machine until you have the mysql database in place.

在新机器上导入 mysql 数据库后,执行 mysqladmin flush-privileges,以便服务器重新加载权限表信息。After you import the mysql database on the new machine, execute mysqladmin flush-privileges so that the server reloads the grant table information.