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

标签 mysql

我正在尝试关闭本地 ~.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_pa​​cket 的设置时,我发现它们仍然具有系统默认值:

$ 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 |
+-----------------------------+

我做错了什么?

最佳答案

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

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

您可以通过将选项写入 [client] 部分来在 MySQL 客户端中声明您自己的 session 的首选 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]仅适用于mysqlmysqldump等MySQL工具。它不会影响您自己的任何应用程序,例如您使用 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 查询,这样它们就不会导致错误。

关于MySQL 忽略我在 ~/.my.conf 中设置的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54947669/

相关文章:

php - 通过 session 获取登录用户的详细信息

php - php/sql 登录脚本故障排除

php - 计数 SQL 语法 COUNT(value) 多列

mysql - 无法在mysql上创建触发器

mysql - 在子选择 IN() 中使用 SELECT 结果

mysql - Laravel 5 中使用查询生成器进行复杂查询(具有三级子查询)?

mysql - 除以第二个查询

mysql - 如何在我的 MySQL 存储过程中使用事务?

mysql时间戳列

php - 正确处理外来字符/表情符号