I must be going nuts here, but I could swear I read about a configuration option in MySQL that will run the defined SQL statement on every successful connection to the database, with the cavet that it would not run for root or those with a certain privilege to prevent shutting the server down with a bad sql statement here.

Was I imagining things or do folks know of such a parameter?

I have dug though the MySQL documents and so far I'm not turning up with any success.

Note, this is NOT for the client supplied sql statements, but instead for a statement that the server itself runs regardless of who connects and for what task, and great for tracking who connects with what account if general_log was not enabled for some reason or special handling is required.


Found it.

The parameter is named init_connect and will run the defined sql for each connection, except for those with the super privilege. No result sets are produced by this though so selects are not recommended for this.

The big cavet I see here though is that access to the objects is based on the connecting user's permissions, which make sense.. though it does make an 'autorun' style stored procedure harder to implement as that procedure needs to be accessible to everyone, which could cause problems if it's written with the expectation to be executed only once per connection.

  • Interesting... what is a good use case for this?
    – dmikester1
    Jan 11 '19 at 22:39
  • It's init_command, not init_connect.
    – Barmar
    Jan 11 '19 at 22:39
  • dev.mysql.com/doc/refman/5.7/en/…
    – Barmar
    Jan 11 '19 at 22:39
  • 1
    @dmikester1 You can use it for things like setting the timezone.
    – Barmar
    Jan 11 '19 at 22:40
  • Oh, this is different from the mysql client init_command option.
    – Barmar
    Jan 11 '19 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.