【问题标题】:MySQL ignoring the options I set in ~/.my.confMySQL 忽略了我在 ~/.my.conf 中设置的选项
【发布时间】:2019-03-01 15:28:37
【问题描述】:

我正在尝试在我的本地 ~.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


    【解决方案1】:

    您的个人~/.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] 仅适用于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 查询,以免它们导致错误。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-15
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多