5.7.1 设置多个数据目录

机器上的每个 MySQL 实例都应该有自己的数据目录。使用该 选项指定位置。 --datadir=dir_name

为新实例设置数据目录有不同的方法:

  • 创建一个新的数据目录。

  • 复制现有数据目录。

以下讨论提供了有关每种方法的更多详细信息。

警告

通常,您永远不应该有两台服务器在同一个数据库中更新数据。如果您的操作系统不支持无故障系统锁定,这可能会导致令人不快的意外。如果(尽管有此警告)您使用相同的数据目录运行多个服务器并且它们启用了日志记录,则必须使用适当的选项来指定每个服务器唯一的日志文件名。否则,服务器会尝试登录到相同的文件。

即使遵守了前面的预防措施,这种设置也只适用于表,而不适用于任何其他存储引擎MyISAMMERGE此外,此关于在服务器之间共享数据目录的警告始终适用于 NFS 环境。允许多个 MySQL 服务器通过 NFS 访问一个公共数据目录是一个非常糟糕的主意。主要问题是 NFS 是速度瓶颈。它不适用于此类用途。NFS 的另一个风险是您必须设计一种方法来确保两个或多个服务器不会相互干扰。通常 NFS 文件锁定由 lockd守护进程,但目前还没有平台在任何情况下都能 100% 可靠地执行锁定。

创建一个新的数据目录

使用这种方法,数据目录与您第一次安装 MySQL 时的状态相同。它有一组默认的 MySQL 帐户,没有用户数据。

在 Unix 上,初始化数据目录。请参阅 第 2.10 节 “安装后设置和测试”

在 Windows 上,数据目录包含在 MySQL 发行版中:

  • Windows 的 MySQL Zip 存档分发包含未修改的数据目录。您可以将此类分发包解压缩到一个临时位置,然后将其 data目录复制到您设置新实例的位置。

  • Windows MSI package installers create and set up the data directory that the installed server uses, but also create a pristine template data directory named data under the installation directory. After an installation has been performed using an MSI package, the template data directory can be copied to set up additional MySQL instances.

Copy an Existing Data Directory

With this method, any MySQL accounts or user data present in the data directory are carried over to the new data directory.

  1. Stop the existing MySQL instance using the data directory. This must be a clean shutdown so that the instance flushes any pending changes to disk.

  2. Copy the data directory to the location where the new data directory should be.

  3. Copy the my.cnf or my.ini option file used by the existing instance. This serves as a basis for the new instance.

  4. Modify the new option file so that any pathnames referring to the original data directory refer to the new data directory. Also, modify any other options that must be unique per instance, such as the TCP/IP port number and the log files. For a list of parameters that must be unique per instance, see Section 5.7, “Running Multiple MySQL Instances on One Machine”.

  5. Start the new instance, telling it to use the new option file.