Joao Coelho Joao Coelho - 4 months ago 10
SQL Question

How do I uniquely identify a database in MySql?

I'm in an environment where in order to connect to the SQL database, I first need to create a tunnel that redirects the local port to a remote machine.
I connect to a different db server, depending on the port number in the tunnel.

In other words: I always connect to MySql with the same parameters to localhost. The tunnel created beforehand is what defines the server I actually connect to.

There are several MySql servers, all with the same schema (for testing purposes) and I want to know which actual database I'm connected to (my script doesn't have access to the tunnel parameters to know).

When we create a new db, is there a GUID or something that gets attributed to it? Perhaps something I can retrieve with a query to

information_schema

Answer

No, the schema name is the identifier.

You may want to set server_id uniquely on each of your MySQL instances. This is an integer in the range 1 through 232-1 that you configure in your MySQL instance's my.cnf file.

http://dev.mysql.com/doc/refman/5.7/en/replication-options.html#option_mysqld_server-id

Usually server_id is used to tag changes in the binary log, so replica sets don't replay changes they've already seen.

But you can read the server_id in any SQL query:

SELECT @@server_id;

And based on its value, you will know which MySQL instance you're on, assuming you have set every instance's server_id according to some design so you'll know.