简体   繁体   English

无法在 MySQL 上设置全局变量

[英]Cannot set a global variable on MySQL

I'm using MySQL in localhost (in ubuntu and also in windows).我在本地主机中使用 MySQL (在 ubuntu 和 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".我想设置一个全局变量,我已经尝试了各种方法,但即使我从 mysql 收到一条“ok”消息,然后当我执行“select @var”时,它总是显示“NULL”。 I've tried:我试过了:

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:我有超级特权。

The variable name var does not reference a valid system variable. 变量名称var不引用有效的系统变量。 The GLOBAL and SESSION keywords in the SET statement are used for specifying the scope when setting MySQL system variables, not MySQL user variables. SET语句中的GLOBALSESSION关键字用于在设置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

http://dev.mysql.com/doc/refman/5.5/en/set-statement.html http://dev.mysql.com/doc/refman/5.5/en/set-statement.html

According to the MySQL 5.0 Reference Manual : 根据MySQL 5.0参考手册

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 (旧的归档链接)之类的扩展来使用全局(持久共享)变量。

On MySQL , you cannot create custom global or session system variables but can change existed global or session system variables as shown below:MySQL 上,您无法创建自定义全局或 session 系统变量,但可以更改现有的全局或 session 系统变量,如下所示:

SET GLOBAL max_connections = 1000;    -- Existed global system variable
SET SESSION sql_mode = 'TRADITIONAL'; -- Existed session system variable

And, you can create user-defined(custom) variables which are removed when you exit(log out) a session as shown below:而且,您可以创建用户定义(自定义)变量,当您退出(注销)session 时,这些变量将被删除,如下所示:

SET @first_name = 'John', @last_name = 'Smith';

User-defined variables are session specific.用户定义的变量是 session 特定的。 A user variable defined by one client cannot be seen or used by other clients.一个客户端定义的用户变量不能被其他客户端看到或使用。 (Exception: A user with access to the Performance Schema user_variables_by_thread table can see all user variables for all sessions.) All variables for a given client session are automatically freed when that client exits. (例外:有权访问性能模式 user_variables_by_thread表的用户可以查看所有会话的所有用户变量。)给定客户端 session 的所有变量在该客户端退出时自动释放。

User variable names are not case-sensitive.用户变量名不区分大小写。 Names have a maximum length of 64 characters.名称的最大长度为 64 个字符。

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

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