一台机器上的每个 MySQL 实例都应该有自己的数据目录。位置使用 --datadir= 选项指定。Each MySQL Instance on a machine should have its own data
directory. The location is specified using the
dir_name--datadir=
option.dir_name
为新实例设置数据目录有不同的方法:There are different methods of setting up a data directory for a new instance:
创建一个新的数据目录。Create a new data directory.
复制一个已有的数据目录。Copy an existing data directory.
以下讨论提供了关于每种方法的更多细节。The following discussion provides more detail about each method.
通常情况下,你不应该有两个服务器同时更新同一个数据库。如果你的操作系统不支持无故障的系统锁定,这可能会导致意想不到的问题。如果(尽管有此警告)你使用相同的数据目录运行多个服务器并启用了日志记录,你必须使用适当的选项来指定每个服务器唯一的日志文件名。否则,服务器会尝试将日志记录到相同的文件中。Normally, you should never have two servers that update data in the same databases. This may lead to unpleasant surprises if your operating system does not support fault-free system locking. If (despite this warning) you run multiple servers using the same data directory and they have logging enabled, you must use the appropriate options to specify log file names that are unique to each server. Otherwise, the servers try to log to the same files.
即使遵守前述预防措施,这种设置也只适用于 MyISAM 和 MERGE 表,而不适用于任何其他存储引擎。此外,有关在服务器之间共享数据目录的警告在 NFS 环境中始终适用。允许多个 MySQL 服务器通过 NFS 访问共享数据目录是一个 非常不好的主意。主要问题是 NFS 是速度瓶颈,它并非为这种用途设计。使用 NFS 的另一个风险是,你必须想办法确保两个或多个服务器不会互相干扰。通常 NFS 文件锁由 lockd 守护进程处理,但目前没有任何平台能够在所有情况下 100% 可靠地执行锁定。Even when the preceding precautions are observed, this kind of
setup works only with MyISAM and
MERGE tables, and not with any of the other
storage engines. Also, this warning against sharing a data
directory among servers always applies in an NFS environment.
Permitting multiple MySQL servers to access a common data
directory over NFS is a very bad idea. The
primary problem is that NFS is the speed bottleneck. It is not
meant for such use. Another risk with NFS is that you must
devise a way to ensure that two or more servers do not interfere
with each other. Usually NFS file locking is handled by the
lockd daemon, but at the moment there is no
platform that performs locking 100% reliably in every situation.
创建新的数据目录Create a New Data Directory
使用此方法,数据目录与您首次安装 MySQL 时的状态相同,具有默认的 MySQL 账户集合,并且没有用户数据。With this method, the data directory is in the same state as when you first install MySQL, and has the default set of MySQL accounts and no user data.
在 Unix 上,初始化数据目录。请参阅 第 2.9 节,“安装后设置和测试”。On Unix, initialize the data directory. See Section 2.9, “Postinstallation Setup and Testing”.
在 Windows 上,数据目录包含在 MySQL 发行版中:On Windows, the data directory is included in the MySQL distribution:
用于 Windows 的 MySQL Zip 压缩包分发包含未经修改的数据目录。您可以将这样的分发包解压到临时位置,然后将其
data目录复制到设置新实例的位置。MySQL Zip archive distributions for Windows contain an unmodified data directory. You can unpack such a distribution into a temporary location, then copy itdatadirectory to where you are setting up the new instance.Windows MSI 软件包安装程序会创建并设置已安装服务器使用的数据目录,同时还会在安装目录下创建一个名为
data的原始 “模板” 数据目录。使用 MSI 软件包完成安装后,可以复制模板数据目录来设置其他 MySQL 实例。Windows MSI package installers create and set up the data directory that the installed server uses, but also create a pristine “template” data directory nameddataunder 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
使用此方法,数据目录中存在的任何 MySQL 账户或用户数据都会被迁移到新的数据目录中。With this method, any MySQL accounts or user data present in the data directory are carried over to the new data directory.
停止使用该数据目录的现有 MySQL 实例。必须进行干净关闭,以便实例将任何待处理的更改刷新到磁盘。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.
将数据目录复制到新的数据目录应在的位置。Copy the data directory to the location where the new data directory should be.
复制现有实例使用的
my.cnf或my.ini选项文件。这将作为新实例的基础。Copy themy.cnformy.inioption file used by the existing instance. This serves as a basis for the new instance.修改新的选项文件,使任何指向原始数据目录的路径名称都指向新的数据目录。同时,修改每个实例必须唯一的其他选项,例如 TCP/IP 端口号和日志文件。有关必须每个实例唯一的参数列表,请参见 第 7.8 节,“在一台机器上运行多个 MySQL 实例”。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 7.8, “Running Multiple MySQL Instances on One Machine”.
启动新的实例,告诉它使用新的选项文件。Start the new instance, telling it to use the new option file.