简体   繁体   English

如何在我的 Mac 上永久设置 SQL_MODE

[英]How can set SQL_MODE permanently on my mac

I'm install MySql on my OsX 10.11 El Capitan.我在我的 OsX 10.11 El Capitan 上安装 MySql。 If I try to start my query I have a strange error if my query have Group By.如果我尝试开始我的查询,如果我的查询有分组依据,我会遇到一个奇怪的错误。

So if I try to execute this query from Terminal:因此,如果我尝试从终端执行此查询:

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

I can execute my query never problem.我可以执行我的查询没问题。 Now if I try to re-start my macbook, I have the some problem and I can re-execute the query.现在,如果我尝试重新启动我的 macbook,我遇到了一些问题,我可以重新执行查询。

There is any way to fix this sql_mode permanently?有什么方法可以永久修复此 sql_mode?

I have tro to open "etc/mysql/my.cnf" file but there isn't this file on my mac.我无法打开“etc/mysql/my.cnf”文件,但我的 mac 上没有这个文件。 How can I apply this change?我如何应用此更改?

You could set my.cnf and add your sql_mode config你可以设置 my.cnf 并添加你的 sql_mode 配置

  [mysqld]
  sql_mode=ONLY_FULL_GROUP_BY

By default, the OS X installation does not use a my.cnf, and MySQL just uses the default values.默认情况下,OS X 安装不使用 my.cnf,而 MySQL 只使用默认值。 To set up your own my.cnf, you could just create a file straight in /etc.要设置您自己的 my.cnf,您可以直接在 /etc 中创建一个文件。

If you're using MAMP PRO it's worth noting this from the guide:如果您使用的是MAMP PRO ,请注意指南中的这一点:

You cannot edit your my.cnf file directly.您不能直接编辑 my.cnf 文件。 You must use the MAMP PRO interface to edit your my.cnf file.您必须使用 MAMP PRO 界面来编辑您的 my.cnf 文件。 In the menu go to File > Edit Template > MySQL > my.cnf.在菜单中转到文件 > 编辑模板 > MySQL > my.cnf。

Then you can add the sql_mode you want under the [mysqld] heading.然后你可以在 [mysqld] 标题下添加你想要的 sql_mode。 eg:例如:

[mysqld]
sql_mode=ONLY_FULL_GROUP_BY

This is what worked for me.这对我有用。

If you want to turn ONLY_FULL_GROUP_BY off, you need to check first the value of the sql_mode variable:如果你想关闭 ONLY_FULL_GROUP_BY,你需要先检查 sql_mode 变量的值:

(default values) (默认值)

 mysql> show variables like 'sql_mode'\G
 *************************** 1. row ***************************
 Variable_name: sql_mode
    Value: 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

1 row in set (0,01 sec)

Edit MySQL config file my.conf.编辑 MySQL 配置文件 my.conf。 Usually in /etc/my.cnf like scaisEdge said.通常在 /etc/my.cnf 就像 scaisEdge 所说的那样。 but, you must write:但是,你必须写:

[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

(all values without ONLY_FULL_GROUP_BY - you can copy/paste this line) (没有 ONLY_FULL_GROUP_BY 的所有值 - 您可以复制/粘贴此行)

Restart your MySQL, and enjoy.重启你的 MySQL,享受吧。

the right way to do that by following these steps遵循这些步骤的正确方法

  1. Quit MAMP.退出 MAMP。

  2. Run following command in the Terminal:在终端中运行以下命令:

    touch /Applications/MAMP/conf/my.cnf && open -t /Applications/MAMP/conf/my.cnf touch /Applications/MAMP/conf/my.cnf && open -t /Applications/MAMP/conf/my.cnf

  3. When this file is empty, then add the following below.当此文件为空时,然后在下面添加以下内容。 If the file is not empty, then simply add the line sql_mode="" right after the line [mysqld]如果文件不为空,则只需在行 [mysqld] 之后添加行 sql_mode=""

    [mysqld] sql_mode="" [mysqld] sql_mode=""

  4. Save the config file and close the text editor;保存配置文件并关闭文本编辑器; restart MAMP and start the servers.重新启动 MAMP 并启动服务器。

reference: https://mampsupportforum.com/forums/latest/mamp-mamp-pro-disable-mysql-strict-mode参考: https ://mampsupportforum.com/forums/latest/mamp-mamp-pro-disable-mysql-strict-mode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM