Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically call a query for every new connection in MySQL?

Is it possible to set or initialize a query in MySQL for every new connection ?

Whenever a connection is created, it will call a procedure or a select query automatically ?

Any script or config option for this?

like image 685
Aman Aggarwal Avatar asked Jun 28 '16 06:06

Aman Aggarwal


2 Answers

You may want to check 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. For example, each client session begins by default with autocommit mode enabled.

http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_init_connect

like image 53
Pavel Katiushyn Avatar answered Nov 15 '22 19:11

Pavel Katiushyn


If you have not only mysql-client tool, but additional any application layer, there is more jedi-style :) You can create simple decorator for your connector's class that will execute some queries when instance created (in constructor)/connect method invoked.

Hope it helps

like image 33
D.Samchuk Avatar answered Nov 15 '22 21:11

D.Samchuk