是否有一个MySQL命令来定位my.cnf配置文件,类似于PHP的phpinfo()定位它的PHP .ini?


当前回答

实际上,你可以“请求”MySQL提供它搜索my.cnf(或Windows上的my.ini)的所有位置的列表。但它不是SQL查询。相反,执行:

$ mysqladmin --help

或者,5.7之前:

$ mysqld --help --verbose

在最初几行中,您将发现一条消息,其中包含它要查找的所有my.cnf位置的列表。在我的机器上是:

Default options are read from the following files in the given order:
/etc/my.cnf
/etc/mysql/my.cnf
/usr/etc/my.cnf
~/.my.cnf

或者,在Windows上:

Default options are read from the following files in the given order:
C:\Windows\my.ini
C:\Windows\my.cnf
C:\my.ini
C:\my.cnf
C:\Program Files\MySQL\MySQL Server 5.5\my.ini
C:\Program Files\MySQL\MySQL Server 5.5\my.cnf

但是请注意,在这些位置中都可能没有my.cnf文件。因此,您可以自己创建这个文件——使用MySQL发行版提供的一个配置文件示例(在Linux上——请参阅/usr/share/mysql/*.cnf文件并使用适合您的文件——将其复制到/etc/my.cnf,然后根据需要进行修改)。

另外,请注意还有一个命令行选项——defaults-file,它可以定义my.cnf或my.ini文件的自定义路径。例如,Windows上的MySQL 5.5就是这种情况——它指向data目录中的my.ini文件,通常不会在mysqld——help——verbose中列出。在Windows上-请参阅服务属性以确定是否适合您。

最后,查看https://dev.mysql.com/doc/refman/8.0/en/option-files.html—那里有更详细的描述。

其他回答

对于Ubuntu 20.04.4 LTS上的MariaDB 10.5 (Focal Fossa):

# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.

我不知道你是如何在Linux环境下安装MySQL的,但你检查过吗?

/etc/my.cnf

另一个选项是使用whereis命令。

例如:my.cnf在哪里

如果你在VPS中,并试图在已经运行的服务器上编辑my.cnf,你可以尝试:

ps aux | grep mysql

您将显示mysql命令正在运行的参数以及——defaults-file指向的位置

请注意,您的服务器可能运行多个MySQL/MariaDB服务器。如果您看到一行没有——defaults-file参数,该实例可能正在从mysqladmin上提到的.cnf中检索配置——帮助其他人指出。

这可能有用:

strace mysql ";" 2>&1  | grep cnf

在我的机器上输出:

stat64("/etc/my.cnf", 0xbf9faafc)       = -1 ENOENT (No such file or directory)
stat64("/etc/mysql/my.cnf", {st_mode=S_IFREG|0644, st_size=4271, ...}) = 0
open("/etc/mysql/my.cnf", O_RDONLY|O_LARGEFILE) = 3
read(3, "# /etc/mysql/my.cnf: The global "..., 4096) = 4096
stat64("/home/xxxxx/.my.cnf", 0xbf9faafc) = -1 ENOENT (No such file or directory)

因此,看起来/etc/mysql/my.cnf是一个,因为它的stat64()和read()是成功的。