Cannot set a global variable on MySQL - 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.

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

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.

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';

Related

SET a variable in mysql only once

I am using classic ASP and MySQL (using PHP wouldn't change the point of the question).
I need to set a variable over my homepage:
SET block_encryption_mode = 'aes-256-cbc'
I can not set it as global variable as other users are using the server and may use the default block_encryption_mode.
I know I can use the statement using ASP/PHP on the beginning of each webpage, but that seems like using too much resources; every user will execute the SET statement on every page...
Is there a way to SET variable or execute some other SQL statement at the beggining on each session, like an onstart event like ASP has, maybe? Or how could I achieve my goal without executing the query for each user on every page I have?
You can use the init_connect variable.
A string to be executed by the server for each client that connects. The string consists of one or more SQL statements, separated by semicolon characters.
You can also distinguish the users with code like this:
IF (CURRENT_USER() = 'special_crypto_dude#localhost') THEN
SET SESSION block_encryption_mode = 'aes-256-cbc';
END IF;
It is safe to call SET on every page as it executes in orders of microseconds. There is literally no overhead calling SET on already open connection.
Unless you can apply this setting globally, I would not bother. Just set the block_encryption_mode (together with collation and timezone) directly after acquiring the database connection handle.

how to set sql mode variable as a local variable in mysql

What i want thing is, i want to set the sql_mode variable as a local variable, not as a session or global variable. Reason to do that is i want to disapear the change of sql mode variable after one of query was executed. Below session and global are worked well, but this is not the what i want. Global one is kept the sql mode as a empty one forever. Session one is kept the sql mode as a empty one until connection close. I want thing is, keep the sql mode until a quarry is executed only.
mysql> set global sql_mode='';
mysql> set session sql_mode='';
mysql query :-
SELECT tc_exe_grp_num,tcs.tc_tc_id,tcs.tcs_id
FROM tc_exe_res tcer
INNER JOIN tcs tcs
ON tcs.tcs_id = tcer.tcs_tcs_id
WHERE tcs.tc_tc_id='1'
AND tcs.tc_tc_id='1'
GROUP BY tc_exe_grp_num
ORDER BY tc_exe_grp_num ;
got the idea from this article
please help me.
##sql_mode is session variable, not a local variable.
It is possible to retrieve the current setting of sql_mode, and save it in a user-defined variable, and then later set sql_mode back to the original setting.
For example:
-- save current setting of sql_mode in user defined variable
-- change sql_mode to desired setting
SET #SAVE_sql_mode = ##sql_mode ;
SET ##sql_mode = 'NO_ENGINE_SUBSTITUTION' ;
-- subsequent operations run under new setting of sql_mode
SELECT '...';
-- set sql_mode back to saved setting
SET ##sql_mode = #SAVE_sql_mode ;
I couldn't find a direct answer for this, but there is a solution,
First set the "sql mode" as a empty one and after quarry was executed set the "sql mode" with what previously had values, try it in below way,
set session sql_mode='';
SELECT tc_exe_grp_num,tcs.tc_tc_id,tcs.tcs_id FROM tc_exe_res tcer INNER JOIN tcs tcs ON tcs.tcs_id = tcer.tcs_tcs_id WHERE tcs.tc_tc_id='1' AND tcs.tc_tc_id='1' group by tc_exe_grp_num ORDER BY tc_exe_grp_num ;
set session sql_mode='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';

super priviliege not set for master user in aws rds mysql

I have created an AWS RDS instance, I have created my master user with master password, and it is working/connecting fine.
But when I am going to create a function on that instance, it shows me the following error:
ERROR 1418: This function has none of DETERMINISTIC, NO SQL,
or READS SQL DATA in its declaration and binary logging is enabled
(you might want to use the less safe log_bin_trust_function_creator variable).
In my instance the variable log_bin_trust_function_creators shows OFF, and if I try to change the variable using SET GLOBAL log_bin_trust_function_creators = 1;, it gives me another error "Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation"
Set log_bin_trust_function_creators = 1 for Parameter group of the RDS instance.
Note: Default Parameter-Group is not editable. Create a new Parameter-Group and assign it to the RDS instance by modifying it from UI (AWS Admin Console) OR maybe using commands
DELIMITER $$
CREATE DEFINER=`DB_USERNAME_HERE`#`%` FUNCTION `GetDistance`(coordinate1 VARCHAR(120), coordinate2 VARCHAR(120)) RETURNS decimal(12,8)
READS SQL DATA
BEGIN
DECLARE distance DECIMAL(12,8);
/*Business logic goes here for the function*/
RETURN distance;
END $$
DELIMITER ;
Here, you have to replace DB_USERNAME_HERE with you RDS database username and function names according to you need.
Important thing is: DEFINER=`DB_USERNAME_HERE`#`%`
This was the problem I was facing after setting log_bin_trust_function_creators = 1 in parameter group. And it worked like a charm.
A better way is to apply your own parameter group, with log_bin_trust_function_creators set to true. (its false by default)
This happens when you try to create a procedure/function/view with a DEFINER that is not the current user.
To solve this remove or update the DEFINER clause.

mysql can't change a session variable group_concat_max_len

In MySQL help it says that "Setting a session variable requires no special privilege, but a client can change only its own session variables, not those of any other client."
I try to increase the size of group_concat_max_len like this:
SET ##group_concat_max_len = 9999;
In phpmyadmin, the response is positive: 'Your SQL query has been executed successfully'.
Then I check the value like this (in the same window, 2 seconds later):
SHOW SESSION VARIABLES;
And unfortunately, group_concat_max_len = 1024
I am not the admin of this MySQL server, but if changing session variable does not require special privilege, then it should work. On my localhost it works.
Is there any chance to set this variable or at least to know why it can't be changed?
In phpmyadmin it is not guaranteed, that 2 queries (even if they are separated only by a few seconds) go to the same session. So chances are, SET ##group_concat_max_len = 9999; went to one session, but SHOW SESSION VARIABLES; to another.
If you try from the mysql command line client, this will work as expected.

`#` Symbol in mysql variable names

I have started maintaining a bunch of mysql stored procs. Some variables (created with decalare statements) are accessed with the # symbol and others without it. Whats the difference
A variable with the # at the beginning is session variable. It exists until the session end.