MySQL配置文件

MySQL程序可以从配置文件中读取配置参数。配置文件提供了一种指定常用参数的方便方法,不用每次启动MySQL时在命令行输入参数。

可以使用以下命令查看MySQL服务读取的配置文件及顺序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ mysqld --verbose --help

mysqld Ver 8.0.34 for Linux on x86_64 (MySQL Community Server - GPL)

BuildID[sha1]=f183cd3ecfc35a4aa5da997063d5e8c97ffca986

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Starts the MySQL database server.

Usage: mysqld [OPTIONS]

Default options are read from the following files in the given order:

/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf /opt/mysql/my.cnf ~/.my.cnf

The following groups are read: mysql_cluster mysqld server mysqld-8.0

在Linux中,配置文件的位置和读取顺序如下表:

文件名 用途
/etc/my.cnf 全局配置
/etc/mysql/my.cnf 全局配置
*SYSCONFDIR*/my.cnf 全局配置
$MYSQL_HOME/my.cnf 服务器的特定配置
defaults-extra-file 如果有的话,用[--defaults-extra-file](https://dev.mysql.com/doc/refman/8.4/en/option-file-options.html#option_general_defaults-extra-file)指定
~/.my.cnf 用户特定配置
~/.mylogin.cnf 用户特定登陆配置(仅限客户端)
*DATADIR*/mysqld-auto.cnf 使用 SET_PERSIST 或 SET_PERSIST_ONLY保存的系统变量(仅限服务器)

全局配置文件内容格式一般如下:

Text
1
2
3
4
5
6
7
8
9
10
11
12
[client]
port=3306
socket=/tmp/mysql.sock

[mysqld]
port=3306
socket=/tmp/mysql.sock
key_buffer_size=16M
max_allowed_packet=128M

[mysqldump]
quick

用户配置文件内容格式一般如下:

Text
1
2
3
4
5
6
7
[client]
# The following password is sent to all standard MySQL clients
password="my password"

[mysql]
no-auto-rehash
connect_timeout=2