Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set a global variable on Mysql

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:

set global var=17;
set @global.var=17;
set @@var=17;

Can anyone help me?

Thanks in advance.

ps: I have the SUPER privilege.

like image 432
leonardo_palma Avatar asked Nov 21 '13 02:11

leonardo_palma


People also ask

What is @@ global in MySQL?

A reference to a system variable in an expression as @@ var_name (with @@ rather than @@GLOBAL. or @@SESSION. ) returns the session value if it exists and the global value otherwise. This differs from SET @@ var_name = expr , which always refers to the session value.

How do I set system variables in MySQL?

System variables can be set at server startup using options on the command line or in an option file. Most of them can be changed dynamically while the server is running by means of the SET statement, which enables you to modify operation of the server without having to stop and restart it.

How do you create a global variable in a database?

A user-defined schema global variable is created by using the CREATE VARIABLE SQL statement. A user-defined module global variable is created using the ADD VARIABLE or PUBLISH VARIABLE option of the ALTER MODULE SQL statement.

What is super privilege in MySQL?

SUPER can be used to terminate other sessions or change how the server operates. Privileges granted for the mysql system database itself can be used to change passwords and other access privilege information: Passwords are stored encrypted, so a malicious user cannot simply read them to know the plain text password.


2 Answers

The variable name var does not reference a valid system variable. The GLOBAL and SESSION keywords in the SET statement are used for specifying the scope when setting MySQL system variables, not MySQL user variables.

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/5.5/en/set-statement.html

like image 61
spencer7593 Avatar answered Sep 29 '22 10:09

spencer7593


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.

like image 29
Alois Heimer Avatar answered Sep 29 '22 11:09

Alois Heimer