Explain Codes LogoExplain Codes Logo

Show MySQL host via SQL Command

sql
mysql
sql-commands
database-troubleshooting
Anton ShumikhinbyAnton Shumikhin·Nov 19, 2024
TLDR

Fetch the MySQL server host swiftly with:

SELECT @@hostname;

This will net you the server's hostname. For the Complete Server Address, granted @@domain is set:

SELECT CONCAT_WS('.', @@hostname, @@domain) AS server_address;

Yet, this may not always yield a fully qualified name - it's subject to your server's configuration.

More than meets the eye: various ways to fetch host details

Digging further into MySQL server host identification, besides @@hostname, there are other interesting variables and commands you can tap into.

To gather all server host-related variables, use:

SHOW VARIABLES WHERE Variable_name LIKE '%host%';

This sweeps up other variables like bind_address and report_host, which come in handy for troubleshooting or verifying the configuration.

Connection origins: who's been knocking on my door?

With information_schema.processlist, you can see from where connections to your database are made. Here's the trick:

SELECT host FROM information_schema.processlist;

This is akin to taking a real-time snapshot of everyone connected to your MySQL server, representing them whether they are logging in from localhost, a named host, or an IP address.

Contextualizing your MySQL session

Discover your own user and host context through:

SELECT USER();

This unveils information in the format username@hostname, helpful for dusting off connection and access privileges mystery. To strip the username leaving only the host:

SELECT SUBSTRING_INDEX(USER(), '@', -1);

Alter the -1 to 1 to only get the username, and leave the host behind.

The no-so-secret 'resolveip' tool: transforming hostnames to IPs

resolveip—a MySQL tool outside raw SQL—is your secret weapon to turn hostnames into IPs.

Mind you, network lookup isn't teleportation—be patient!

Peeking through the keyhole with 'SHOW PROCESSLIST'

The SHOW PROCESSLIST command lifts the lid on various connection details, including the host or IP of each process:

SHOW PROCESSLIST; -- it's like your server saying, "These are the folks I'm chatting with!"

It's almost like getting a VIP access to the server's private consultations.