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

问题描述 投票:1回答:2

我想在我的本地〜.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 |
+-----------------------------+

我做错了什么?

mysql options
2个回答
1
投票

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

在你的个人[mysqld]文件中为~/.my.cnf编写一个部分没有用,因为你的用户可能不是启动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]仅适用于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查询以使它们不会导致错误要好得多。


1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.