gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:50:24 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com