Cannot set a global variable on MySQL
Asked Answered
W

3

14

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.

Wadesworth answered 21/11, 2013 at 2:5 Comment(2)
What? The "$" simbol is for variables in Php, not in MySQL.Wadesworth
we can't create user-defined variables in global/session scope.Reinareinald
R
23

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

Rimola answered 21/11, 2013 at 2:56 Comment(4)
So, is not possible to set and store permanently a variable with a any name you choose?Wadesworth
That is correct. the SET GLOBAL is for modifying predefined MySQL system variables. It's not used to set user defined variables. I don't have an explanation for MySQL responding with an "OK" message, rather than emitting an error. Perhaps there isn't a standard SQLSTATE that accurately represents the error, maybe it was just easier to return a success code; or, maybe it was a design decision regarding future upgrades, and some degree of backwards compatibility for deprecated and removed system variables.Rimola
So say SET GLOBAL SQL_MODE = '' is the same as SET @@SQL_MODE = '' right? So the @'s kinda replace the global keyword if I'm not wrong, thanks.Beat
@Thielicious: not quite. @@sql_mode is shorthand for @@session.sql_mode. The sql_mode system variable can be set at the GLOBAL and SESSION level. The session variable inherits the value of the corresponding global variable when the session is started. Some MySQL system variables are only global; while others, like sql_mode, are both global and session variables.Rimola
A
2

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.

Altitude answered 22/4, 2015 at 9:11 Comment(0)
G
1

On MySQL, you cannot create custom global or session system variables but can change existed global or session system variables as shown below:

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. User-defined variables exist only in the current session so they cannot be seen by other sessions unless you use performance_schema.user_variables_by_thread:

SET @first_name = 'John', @last_name = 'Smith';
Gibrian answered 10/9, 2022 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.