Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SET a variable in mysql only once

Tags:

mysql

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?

like image 712
Jerry2 Avatar asked Jul 28 '17 11:07

Jerry2


1 Answers

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;
like image 186
fancyPants Avatar answered Nov 15 '22 09:11

fancyPants