MySQL 安装后,必须初始化数据目录,包括mysql系统架构中的表:After MySQL is installed, the data directory must be initialized,
including the tables in the mysql system
schema:
对于某些 MySQL 安装方法,数据目录初始化是自动的,如第 2.9 节“安装后设置和测试”。For some MySQL installation methods, data directory initialization is automatic, as described in Section 2.9, “Postinstallation Setup and Testing”.
对于其他安装方法,您必须手动初始化数据目录。其中包括在 Unix 和类 Unix 系统上从通用二进制和源发行版安装,以及在 Windows 上从 ZIP 存档包安装。For other installation methods, you must initialize the data directory manually. These include installation from generic binary and source distributions on Unix and Unix-like systems, and installation from a ZIP Archive package on Windows.
本节介绍如何为数据目录初始化不是自动的 MySQL 安装方法手动初始化数据目录。有关用于测试服务器是否可访问并正常工作的一些建议命令,请参阅第 2.9.3 节“测试服务器”。This section describes how to initialize the data directory manually for MySQL installation methods for which data directory initialization is not automatic. For some suggested commands that enable testing whether the server is accessible and working properly, see Section 2.9.3, “Testing the Server”.
默认身份验证插件为 caching_sha2_password,'root'@'localhost' 管理帐户默认使用 caching_sha2_password。The default authentication plugin is
caching_sha2_password, and the
'root'@'localhost' administrative account
uses caching_sha2_password by default.
mysql_native_password(MySQL 8.0 之前的默认身份验证插件)仍然受支持,但从 MySQL 8.4.0 开始默认禁用,从 MySQL 9.0.0 开始删除。mysql_native_password (the default authentication plugin prior to MySQL 8.0) is still supported but disabled by default as of MySQL 8.4.0 and removed as of MySQL 9.0.0.
在此处显示的示例中,服务器旨在在 mysql 登录帐户。如果该帐户不存在,请创建它(请参阅创建 mysql 用户和组),或者替换您计划用于运行服务器的其他现有登录帐户的名称。In the examples shown here, the server is intended to run under
the user ID of the mysql login account.
Either create the account if it does not exist (see
Create a mysql User and Group), or
substitute the name of a different existing login account that
you plan to use for running the server.
将位置更改为 MySQL 安装的顶级目录,通常为
/usr/local/mysql(根据需要调整系统的路径名称):Change location to the top-level directory of your MySQL installation, which is typically/usr/local/mysql(adjust the path name for your system as necessary):cd /usr/local/mysql
在此目录中,您可以找到多个文件和子目录,包括包含服务器以及客户端和实用程序的
bin子目录。Within this directory you can find several files and subdirectories, including thebinsubdirectory that contains the server, as well as client and utility programs.secure_file_priv系统变量将导入和导出操作限制到特定目录。创建一个目录,其位置可以指定为该变量的值:Thesecure_file_privsystem variable limits import and export operations to a specific directory. Create a directory whose location can be specified as the value of that variable:mkdir mysql-files
将目录用户和组所有权授予
mysql用户和mysql组,并适当设置目录权限:Grant directory user and group ownership to themysqluser andmysqlgroup, and set the directory permissions appropriately:chown mysql:mysql mysql-files chmod 750 mysql-files
使用服务器初始化数据目录,包括
mysql架构,该架构包含初始 MySQL 授权表,这些授权表确定如何允许用户连接到服务器。例如:Use the server to initialize the data directory, including themysqlschema containing the initial MySQL grant tables that determine how users are permitted to connect to the server. For example:bin/mysqld --initialize --user=mysql
有关该命令的重要信息,尤其是有关您可能使用的命令选项的重要信息,请参阅数据目录初始化过程。有关服务器如何进行初始化的详细信息,请参见数据目录初始化期间的服务器操作。For important information about the command, especially regarding command options you might use, see Data Directory Initialization Procedure. For details about how the server performs initialization, see Server Actions During Data Directory Initialization.
通常,只有在首次安装 MySQL 后才需要进行数据目录初始化。 (要升级到现有安装,请执行升级过程;请参阅第 3 章,升级 MySQL。)但是,初始化数据目录的命令不会覆盖任何现有的
mysql架构表,因此在任何情况下都可以安全运行。Typically, data directory initialization need be done only after you first install MySQL. (For upgrades to an existing installation, perform the upgrade procedure instead; see Chapter 3, Upgrading MySQL.) However, the command that initializes the data directory does not overwrite any existingmysqlschema tables, so it is safe to run in any circumstances.在没有任何选项的情况下文件时,服务器以其默认设置启动。 (请参见第 7.1.2 节“服务器配置默认值”。)要显式指定 MySQL 服务器在启动时应使用的选项,请将它们放入选项文件中,例如
/etc/my.cnf或/etc/mysql/my.cnf。 (请参阅第 6.2.2.2 节“使用选项文件”。)例如,您可以使用选项文件来设置secure_file_priv系统In the absence of any option files, the server starts with its default settings. (See Section 7.1.2, “Server Configuration Defaults”.) To explicitly specify options that the MySQL server should use at startup, put them in an option file such as/etc/my.cnfor/etc/mysql/my.cnf. (See Section 6.2.2.2, “Using Option Files”.) For example, you can use an option file to set thesecure_file_privsystem variable.要安排 MySQL 在系统启动时无需手动干预即可启动,请参阅第 2.9.5 节“自动启动和停止 MySQL”。To arrange for MySQL to start without manual intervention at system boot time, see Section 2.9.5, “Starting and Stopping MySQL Automatically”.
数据目录初始化会在
mysql架构中创建时区表,但不会填充它们。为此,请使用第 7.1.15 节“MySQL 服务器时区支持”中的说明。Data directory initialization creates time zone tables in themysqlschema but does not populate them. To do so, use the instructions in Section 7.1.15, “MySQL Server Time Zone Support”.
将位置更改为 MySQL 安装的顶级目录,通常为 /usr/local/mysql(根据需要调整系统的路径名称):Change location to the top-level directory of your MySQL
installation, which is typically
/usr/local/mysql (adjust the path name for
your system as necessary):
cd /usr/local/mysql
要初始化数据目录,请调用mysqld 以及 --initialize 或 --initialize-insecure 选项,具体取决于您是否希望服务器为 'root'@'localhost' 帐户生成随机初始密码,还是创建没有密码的帐户:To initialize the data directory, invoke
mysqld with the
--initialize or
--initialize-insecure option,
depending on whether you want the server to generate a random
initial password for the 'root'@'localhost'
account, or to create that account with no password:
使用
--initialize进行“默认安全”安装(即,包括生成随机初始root密码)。在这种情况下,密码将被标记为已过期,您必须选择一个新密码。Use--initializefor “secure by default” installation (that is, including generation of a random initialrootpassword). In this case, the password is marked as expired and you must choose a new one.使用
--initialize-insecure时,不会生成root密码。这是不安全的;假定您打算在将服务器投入生产使用之前及时为该帐户分配密码。With--initialize-insecure, norootpassword is generated. This is insecure; it is assumed that you intend to assign a password to the account in a timely fashion before putting the server into production use.
有关分配新的 'root'@'localhost' 密码的说明,请参阅初始化后 root 密码分配。For instructions on assigning a new
'root'@'localhost' password, see
Post-Initialization root Password Assignment.
服务器将任何消息(包括任何初始密码)写入其标准错误输出。这可能会被重定向到错误日志,因此如果您在屏幕上没有看到消息,请查看那里。有关错误日志的信息,包括其所在位置,请参阅第 7.4.2 节“错误日志”。The server writes any messages (including any initial password) to its standard error output. This may be redirected to the error log, so look there if you do not see the messages on your screen. For information about the error log, including where it is located, see Section 7.4.2, “The Error Log”.
在 Windows 上,使用 --console 选项将消息定向到On Windows, use the --console
option to direct messages to the console.
在 Unix 和类 Unix 系统上,数据库目录和文件由 mysql 登录帐户拥有非常重要,以便服务器在稍后运行时具有对它们的读写访问权限。为了确保这一点,请从系统 root 帐户启动 mysqld 并包含 --user 选项,如图所示这里:On Unix and Unix-like systems, it is important for the database
directories and files to be owned by the
mysql login account so that the server has
read and write access to them when you run it later. To ensure
this, start mysqld from the system
root account and include the
--user option as shown here:
bin/mysqld --initialize --user=mysql bin/mysqld --initialize-insecure --user=mysql
或者,登录mysqld时mysql时可以省略--user option from the command.Alternatively, execute mysqld while logged in
as mysql, in which case you can omit the
--user option from the command.
在 Windows 上,使用以下命令之一:On Windows, use one of these commands:
bin\mysqld --initialize --console bin\mysqld --initialize-insecure --console
如果缺少所需的系统库,数据目录初始化可能会失败。例如,您可能会看到如下错误:Data directory initialization might fail if required system libraries are missing. For example, you might see an error like this:
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
如果发生这种情况,您必须手动或使用系统的包管理器安装缺少的库。然后重试数据目录初始化命令。If this happens, you must install the missing libraries manually or with your system's package manager. Then retry the data directory initialization command.
It might be necessary to specify other options such as
--basedir或者--datadir返回mysqld cannot identify the correct locations
for the installation directory or data directory. For example
(enter the command on a single line):It might be necessary to specify other options such as
--basedir or
--datadir if
mysqld cannot identify the correct locations
for the installation directory or data directory. For example
(enter the command on a single line):
bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt/mysql/mysql/data
或者,将相关选项设置放入选项文件中,并将该文件的名称传递给 mysqld。对于 Unix 和类 Unix 系统,假设选项文件名为 /opt/mysql/mysql/etc/my.cnf。将这些行放入文件中:Alternatively, put the relevant option settings in an option
file and pass the name of that file to
mysqld. For Unix and Unix-like systems,
suppose that the option file name is
/opt/mysql/mysql/etc/my.cnf. Put these
lines in the file:
[mysqld] basedir=/opt/mysql/mysql datadir=/opt/mysql/mysql/data
然后调用 mysqld,如下所示(在单行上输入命令,并使用 --defaults-file 选项先):Then invoke mysqld as follows (enter the
command on a single line, with the
--defaults-file option first):
bin/mysqld --defaults-file=/opt/mysql/mysql/etc/my.cnf --initialize --user=mysql
在 Windows 上,假设 C:\my.ini 包含以下行:On Windows, suppose that C:\my.ini contains
these lines:
[mysqld] basedir=C:\\Program Files\\MySQL\\MySQL Server 8.4 datadir=D:\\MySQLdata
然后调用 mysqld 如下(同样,您应该在单行上输入命令,并首先输入 --defaults-file 选项):Then invoke mysqld as follows (again, you
should enter the command on a single line, with the
--defaults-file option first):
bin\mysqld --defaults-file=C:\my.ini --initialize --console
When initializing the data directory, you should not specify
any options other than those used for setting directory
locations such as --basedir或者--datadir, and the
--user option if needed.
Options to be employed by the MySQL server during normal use
can be set when restarting it following initialization. See
the description of the
--initialize option for further
information.When initializing the data directory, you should not specify
any options other than those used for setting directory
locations such as --basedir or
--datadir, and the
--user option if needed.
Options to be employed by the MySQL server during normal use
can be set when restarting it following initialization. See
the description of the
--initialize option for further
information.
服务器执行的数据目录初始化序列不能替代 mysql_secure_installation。The data directory initialization sequence performed by the server does not substitute for the actions performed by mysql_secure_installation.
当使用 --initialize 或 --initialize-insecure 选项调用时,mysqld 在数据目录初始化序列期间执行以下操作:When invoked with the
--initialize or
--initialize-insecure option,
mysqld performs the following actions during
the data directory initialization sequence:
服务器按如下方式检查数据目录是否存在:The server checks for the existence of the data directory as follows:
如果不存在数据目录,服务器将创建它。If no data directory exists, the server creates it.
如果数据目录存在但不为空(即包含文件或子目录),则服务器退出生成错误消息后:If the data directory exists but is not empty (that is, it contains files or subdirectories), the server exits after producing an error message:
[ERROR] --initialize specified but the data directory exists. Aborting.
在这种情况下,删除或重命名数据目录,然后重试。In this case, remove or rename the data directory and try again.
如果每个条目的名称都以句点 (
.) 开头,则现有数据目录允许为非空。An existing data directory is permitted to be nonempty if every entry has a name that begins with a period (.).
在数据目录中,服务器创建
mysql系统架构及其表,包括数据字典表、授权表、时区表和服务器端帮助表。请参阅第 7.3 节“mysql 系统架构”。Within the data directory, the server creates themysqlsystem schema and its tables, including the data dictionary tables, grant tables, time zone tables, and server-side help tables. See Section 7.3, “The mysql System Schema”.服务器初始化系统表空间以及管理
InnoDB表。The server initializes the system tablespace and related data structures needed to manageInnoDBtables.Notemysqld 设置
InnoDB系统表空间,对表空间特性的某些更改需要设置一个全新的实例。合格的更改包括系统表空间中第一个文件的文件名和撤消日志的数量。如果您不想使用默认值,请确保innodb_data_file_path和innodb_log_file_size配置参数位于 MySQL 配置文件 在运行mysqld。另外,请确保根据需要指定影响InnoDB文件的创建和位置的其他参数,例如innodb_data_home_dir和innodb_log_group_home_dir。After mysqld sets up theInnoDBsystem tablespace, certain changes to tablespace characteristics require setting up a whole new instance. Qualifying changes include the file name of the first file in the system tablespace and the number of undo logs. If you do not want to use the default values, make sure that the settings for theinnodb_data_file_pathandinnodb_log_file_sizeconfiguration parameters are in place in the MySQL configuration file before running mysqld. Also make sure to specify as necessary other parameters that affect the creation and location ofInnoDBfiles, such asinnodb_data_home_dirandinnodb_log_group_home_dir.找到关于 Oracle Linux 和 ULN 的信息。
--defaults-extra-file如果这些选项位于您的配置文件中,但该文件不在 MySQL 默认读取的位置,请在运行时使用mysqld.If those options are in your configuration file but that file is not in a location that MySQL reads by default, specify the file location using the--defaults-extra-fileoption when you run mysqld.服务器创建一个
'root'@'localhost'超级用户帐户和其他保留帐户(请参阅第 8.2.9 节“保留帐户”)。某些保留帐户被锁定,客户端无法使用,但'root'@'localhost'用于管理用途,您应该为其分配密码。The server creates a'root'@'localhost'superuser account and other reserved accounts (see Section 8.2.9, “Reserved Accounts”). Some reserved accounts are locked and cannot be used by clients, but'root'@'localhost'is intended for administrative use and you should assign it a password.与
'root'@'localhost'帐户密码相关的服务器操作取决于您调用它的方式:Server actions with respect to a password for the'root'@'localhost'account depend on how you invoke it:使用
--initialize但不是--initialize-insecure,服务器会生成一个随机密码,将其标记为过期,并写入一条显示密码的消息:With--initializebut not--initialize-insecure, the server generates a random password, marks it as expired, and writes a message displaying the password:[Warning] A temporary password is generated for root@localhost: iTag*AfrH5ej
使用
--initialize-insecure,(有或没有--initialize,因为--initialize-insecure意味着--initialize),服务器不会生成密码或将其标记为过期,并写入警告消息:With--initialize-insecure, (either with or without--initializebecause--initialize-insecureimplies--initialize), the server does not generate a password or mark it expired, and writes a warning message:[Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
有关分配新的
'root'@'localhost'密码的说明,请参阅初始化后 root 密码分配。For instructions on assigning a new'root'@'localhost'password, see Post-Initialization root Password Assignment.服务器填充用于
HELP语句的服务器端帮助表(请参阅第 15.8.3 节“HELP 语句”)。服务器不填充时区表。要手动执行此操作,请参阅第 7.1.15 节“MySQL 服务器时区支持”。The server populates the server-side help tables used for theHELPstatement (see Section 15.8.3, “HELP Statement”). The server does not populate the time zone tables. To do so manually, see Section 7.1.15, “MySQL Server Time Zone Support”.如果指定
init_file系统变量来命名 SQL 文件语句,服务器执行文件中的语句。此选项使您能够执行自定义引导序列。If theinit_filesystem variable was given to name a file of SQL statements, the server executes the statements in the file. This option enables you to perform custom bootstrapping sequences.当服务器在引导模式下运行时,某些功能不可用,从而限制了文件中允许的语句。其中包括与帐户管理相关的语句(例如
CREATE USER或GRANT)、复制和全局事务标识符。When the server operates in bootstrap mode, some functionality is unavailable that limits the statements permitted in the file. These include statements that relate to account management (such asCREATE USERorGRANT), replication, and global transaction identifiers.服务器退出。The server exits.
通过使用 --initialize 或 --initialize-insecure 启动服务器来初始化数据目录后,正常启动服务器(即,不使用其中任何一个)选项)并为 'root'@'localhost' 帐户分配一个新密码:After you initialize the data directory by starting the server
with --initialize or
--initialize-insecure, start the
server normally (that is, without either of those options) and
assign the 'root'@'localhost' account a new
password:
启动服务器。有关说明,请参阅第 2.9.2 节“启动服务器”。Start the server. For instructions, see Section 2.9.2, “Starting the Server”.
连接到服务器:Connect to the server:
如果您使用了
--initialize但未使用--initialize-insecure初始化数据目录,以root连接到服务器:If you used--initializebut not--initialize-insecureto initialize the data directory, connect to the server asroot:mysql -u root -p
然后,在出现密码提示时,输入服务器在初始化过程中生成的随机密码:Then, at the password prompt, enter the random password that the server generated during the initialization sequence:
Enter password:
(enter the random root password here)如果您不知道该密码,请查看服务器错误日志Look in the server error log if you do not know this password.
如果您使用
--initialize-insecure初始化数据目录,请以root身份连接到服务器,无需密码:If you used--initialize-insecureto initialize the data directory, connect to the server asrootwithout a password:mysql -u root --skip-password
连接后,使用
ALTER USER语句分配新的root密码:After connecting, use anALTER USERstatement to assign a newrootpassword:ALTER USER 'root'@'localhost' IDENTIFIED BY '
root-password';
另请参阅第 2.9.4 节“保护初始 MySQL”帐户”。See also Section 2.9.4, “Securing the Initial MySQL Account”.
尝试连接到主机 127.0.0.1 通常会解析为 localhost 帐户。但是,如果服务器在启用 skip_name_resolve 的情况下运行,则会失败。如果您打算这样做,请确保存在可以接受连接的帐户。例如,为了能够使用 --host=127.0.0.1 或 --host=::1 作为 root 连接,请创建以下帐户:Attempts to connect to the host 127.0.0.1
normally resolve to the localhost account.
However, this fails if the server is run with
skip_name_resolve enabled. If
you plan to do that, make sure that an account exists that can
accept a connection. For example, to be able to connect as
root using
--host=127.0.0.1 or
--host=::1, create these accounts:
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root-password'; CREATE USER 'root'@'::1' IDENTIFIED BY 'root-password';
可以将这些语句放入要使用 init_file 系统变量,如数据目录初始化期间的服务器操作中所述。It is possible to put those statements in a file to be
executed using the init_file
system variable, as discussed in
Server Actions During Data Directory Initialization.