MySQL 忽略我在 ~/.my.conf 中设置的选项

MySQL ignoring the options I set in ~/.my.conf

我正在尝试关闭本地 ~.my.cnf 选项文件中的 ONLY_FULL_GROUP_BY。

这是我的系统详细信息:

$ sudo service mysql status
 * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.7.25, for Linux on x86_64
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version          5.7.25-0ubuntu0.18.04.2
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 19 min 35 sec

Threads: 1  Questions: 14  Slow queries: 0  Opens: 108  Flush tables: 1  
Open tables: 27  Queries per second avg: 0.011`

~.my.cnf内容如下:

[mysqld]
key_buffer_size=32M
max_allowed_packet=512M 
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, 
ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

当我使用 "mysqld --help --verbose" 时,这是我看到的:

$ sudo mysqld --help --verbose
mysqld  Ver 5.7.25-0ubuntu0.18.04.2 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 ~/.my.cnf

所以我的 ~/.my.cnf 被最后读取意味着它应该覆盖以前文件中所做的任何设置。


当我使用 "mysqld --print-defaults" 查看正在设置的内容时,这是我得到的:

$ sudo mysqld --print-defaults
mysqld would have been started with the following arguments:
--user=mysql --pid-file=/var/run/mysqld/mysqld.pid 
-- socket=/var/run/mysqld/mysqld.sock --port=3306 --basedir=/usr 
--datadir=/var/lib/mysql --tmpdir=/tmp --lc-messages-dir=/usr/share/mysql 
--skip-external-locking 
--bind-address=127.0.0.1 --key_buffer_size=16M --max_allowed_packet=16M 
--thread_stack=192K --thread_cache_size=8 
--myisam-recover-options=BACKUP --query_cache_limit=1M 
--query_cache_size=16M --log_error=/var/log/mysql/error.log 
--expire_logs_days=10 --max_binlog_size=100M --key_buffer_size=32M 
--max_allowed_packet=512M 
--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,
    ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

看来我的设置已被执行。


使用另一种方法显示相同:

$ sudo mysqladmin variables | grep sql_mode
| sql_mode   
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,
ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

$ sudo mysqladmin variables | grep max_allowed_packet
| max_allowed_packet| 16777216  

但是,当我查看 mysql 中 sql_mode 和 max_allowed_packet 的设置时,我发现它们仍然具有系统默认值:

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)    
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@global.sql_mode;
+--------------------------------------------------------------------------+
| @@global.sql_mode                                                        |
+--------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,                  |
| NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,             |
| NO_ENGINE_SUBSTITUTION                                                   |
+--------------------------------------------------------------------------+

mysql> select @@global.max_allowed_packet;
+-----------------------------+
| @@global.max_allowed_packet |
+-----------------------------+
|                    16777216 |
+-----------------------------+

我做错了什么?

mysqld 通常以自己的用户 mysql 运行,所以 ~/.my.cnf 没有意义,除非它在 ​​mysql 用户的主目录中。对于服务器配置,您通常将设置放在 /etc/my.cnf 文件或 /etc/my.cnf.d 中,具体取决于您的 mysql 版本。

您的个人 ~/.my.cnf 只能影响您的程序 运行,例如 mysql 客户端或 mysqldump

在您的个人 ~/.my.cnf 文件中为 [mysqld] 写一段没有好处,因为您的用户可能不是启动 mysqld 的用户。

您可以通过将选项写入 [client] 部分来在 MySQL 客户端中为您自己的会话声明您的首选 sql_mode:

[client]
max_allowed_packet=512M 
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, 
ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

但是 [client] 仅适用于 MySQL 工具,例如 mysqlmysqldump。它不会影响您自己的任何应用程序,例如您在 Java、Python、Ruby 或 PHP 等

中编写某些内容

如果您希望某个选项影响所有客户端,包括您的应用程序,则必须在全局 MySQL 服务器配置中应用该选项,这可能是 /etc/my.cnf,如其他答案中所述。

您还可以在不重新启动 mysqld 的情况下动态更改许多全局选项。我经常在编辑 /etc/my.cnf 文件后立即执行此操作,因此我可以在不中断服务的情况下进行更改。

mysql> SET GLOBAL sql_mode='...whatever...';

最后说明:我建议不要禁用 ONLY_FULL_GROUP_BY。 sql_mode 是防止虚假查询结果的有用方法。最好保持 sql_mode 启用,并修复您的 SQL 查询,以免它们导致错误。