无法在 Mysql 上设置全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20111096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot set a global variable on Mysql
提问by leonardo_palma
i'm using Mysql in localhost (in ubuntu and also in windows). I want to set a global variable, and I have tried in all ways but even though I get an "ok" message from mysql, then when I do the "select @var" it allways says "NULL". I've tried:
我在本地主机(在 ubuntu 和 Windows 中)中使用 Mysql。我想设置一个全局变量,我已经尝试了所有方法,但即使我从 mysql 收到“ok”消息,当我执行“select @var”时,它总是显示“NULL”。我试过了:
set global var=17;
set @global.var=17;
set @@var=17;
Can anyone help me?
谁能帮我?
Thanks in advance.
提前致谢。
ps: I have the SUPER privilege.
ps:我有超级特权。
回答by spencer7593
The variable name vardoes not reference a valid system variable. The GLOBALand SESSIONkeywords in the SETstatement are used for specifying the scope when setting MySQL system variables, not MySQL user variables.
变量名称var未引用有效的系统变量。语句中的GLOBAL和SESSION关键字SET用于指定设置MySQL系统变量时的作用域,而不是MySQL用户变量。
Try for example:
尝试例如:
SELECT @@global.net_read_timeout ;
SET GLOBAL net_read_timeout = 45 ;
SELECT @@global.net_read_timeout ;
http://dev.mysql.com/doc/refman/8.0/en/set-statement.html
http://dev.mysql.com/doc/refman/8.0/en/set-statement.html
回答by Alois Heimer
According to the MySQL 5.0 Reference Manual:
User-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client session are automatically freed when that client exits.
用户定义的变量是特定于会话的。也就是说,一个客户端定义的用户变量不能被其他客户端看到或使用。给定客户端会话的所有变量在该客户端退出时自动释放。
You could consider using an extension like MySQL Global User Variables UDF(old archived link)to use global (persistent shared) variables.
您可以考虑使用像MySQL 全局用户变量 UDF (旧存档链接)这样的扩展来使用全局(持久共享)变量。

