136

I am in a MySQL terminal session but I don't know what server I am connected to, or what database I am connected to.

Is there a MySQL command that will tell me the host, port, and username and database I am using now?

3
  • 6
    Port can be picked up using "show variables like 'port'" Aug 31, 2010 at 23:55
  • 2
    you will type "\s" to get the info of current database a
    – Shivam
    Dec 31, 2016 at 10:19
  • can mysql cli always show current db-name just like mariadb MySQL [sys]>
    – roamer
    Jun 30, 2020 at 4:11

3 Answers 3

195

There are MYSQL functions you can use. Like this one that resolves the user:

SELECT USER();

This will return something like root@localhost so you get the host and the user.

To get the current database run this statement:

SELECT DATABASE();

Other useful functions can be found here: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

0
149

You can use the status command in MySQL client.

mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.5.8, for Win32 (x86)

Connection id:          1
Current database:       test
Current user:           ODBC@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         5.5.8 MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    gbk
Conn.  characterset:    gbk
TCP port:               3306
Uptime:                 7 min 16 sec

Threads: 1  Questions: 21  Slow queries: 0  Opens: 33  Flush tables: 1  Open tables: 26  Queries per second avg: 0.48
--------------

mysql>
3
  • 1
    it is possible to output this data from a MySQL query via PHP?
    – Martin
    Oct 30, 2015 at 12:55
  • Which version are you using to run this command? I think you are being confused between mysql client and mysqladmin program. mysqladmin -u root -p status This is my output: Uptime: 79625 Threads: 3 Questions: 9903 Slow queries: 1 Opens: 106 Flush tables: 1 Open tables: 99 Queries per second avg: 0.124 It does not show the information of database being connected as well.
    – Tung
    Jan 17, 2016 at 9:43
  • If I executed this command on cmd of navicat, it gave me 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'status' at line 1 error. But If I run it on mysql client, it was ok. Oct 16, 2018 at 1:54
47

If you want to know the port number of your local host on which Mysql is running you can use this query on MySQL Command line client --

SHOW VARIABLES WHERE Variable_name = 'port';


mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

It will give you the port number on which MySQL is running.


If you want to know the hostname of your Mysql you can use this query on MySQL Command line client --

SHOW VARIABLES WHERE Variable_name = 'hostname';


mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| hostname          | Dell  |
+-------------------+-------+
1 row in set (0.00 sec)

It will give you the hostname for mysql.


If you want to know the username of your Mysql you can use this query on MySQL Command line client --

select user();   


mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

It will give you the username for mysql.

2
  • Or you could use Yousui's answer of mysql> status, which returns all of this in a single command.
    – a coder
    Jul 23, 2014 at 20:20
  • 2
    Yousui's answer does not include the hostname. Also, if you only wanted to return one of the specified values and not the whole list, these commands are very useful.
    – raphael75
    May 3, 2019 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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