How to list all mySQL databases, tables and MySQL users on Linux Server

How to list all mySQL databases on a Linux server


 
The below commands are very useful to find out the database name, database user name and tables of the database if you have lost those details. You must have server root access to list all databases on the linux server. The commands in this article will work only on localhost, you must use “-h serverIP” option with all commands if you are connecting remotely to mySQL server. Example :

mysql -u root -h 192.168.1.5

In the above command you must replace “192.168.1.5” with your server IP address to connect remotely to mySQL. Remote mySQL users must modify the below commands accordingly.

Download and install “putty” software on your PC to log into linux server via SSH.

1. Log into linux server via SSH as ‘root’ user

2. Just type “mysql” and hit enter to login to mySQL server

mysql

Try the below command if you have set mySQL root password on the server

mysql -u root -p

The above command will ask for MySQL root password and you must enter the mysql root password to login to mySQL server. Your mySQL root password might be stored in “/root/.my.cnf” file or you can reset it if you have forgotten the password.
 

[root@server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1091
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

 
mysql -u root -p’mysqlrootpassword’

Use the above command to login to mySQL server and it will not prompt for password.
 
Login mySQL prompt
 
3. Now you are in mysql prompt. Run the below command to list all databases.

Command : show databases;

“show databases” command will list all the databases managed by the server.

 

mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| cphulkd |
| eximstats |
| mysql |
| performance_schema |
+——————–+
5 rows in set (0.00 sec)

mysql>

 
In the above server information_schema, cphulkd, eximstats etc are name of the databases.
 
show mysql databases command
 

You can also use the below command to show all mySQL databases on the server.

mysql -e ‘show databases;’
 

[root@server ~]# mysql -e ‘show databases;’
+——————–+
| Database |
+——————–+
| information_schema |
| joomla1 |
| wordpress |
| mysql |
| performance_schema |
+——————–+

 
Use the below command if you have set mySQL root password.

mysql -u root -p -e ‘show databases;’

OR

mysql -u root -p’password’ -e ‘show databases;’
 

[root@server ~]# mysql -u root -p’password’ -e ‘show databases;’
Warning: Using a password on the command line interface can be insecure.
+——————–+
| Database |
+——————–+
| information_schema |
| joomla12 |
| wordpress |
| mysql |
| performance_schema |
+——————–+

 


How to list all mySQL users on a linux server

 
The above steps are for listing the databases on the server. Do the below steps to list all database users on the server.

1. Log into sever via SSH as ‘root’

2. Type “mysql” and press enter to login to mySQL server

3. Run the command “SELECT User FROM mysql.user;” to list all mySQL users on a linux server

command : SELECT User FROM mysql.user;
 

[root@server ~]# mysql -u root -p’password’

mysql> SELECT User FROM mysql.user;
+————+
| User |
+————+
| root |
| efewfwrerw |
| root |
| cphulkd |
| root |
| efewfwrerw |
| root |
+————+
10 rows in set (0.00 sec)

mysql>

 
Show mySQL user command
 
OR use the below command

mysql -e ‘SELECT User FROM mysql.user;’

Try the below command if you have set mySQL root password.
 

[root@server ~]# mysql -u root -p’password’ -e ‘SELECT User FROM mysql.user;’
Warning: Using a password on the command line interface can be insecure.
+————+
| User |
+————+
| root |
| cphulkd |
| efewfwrerw |
| eximstats |
| root |
+————+

 


How to list all tables in a MySQL database

 1. Login to linux server as ‘root’

2. Type “mysql” to log into mySQL server

3. Run “use dataaasename;” to select/access the database

Example :

mysql> use eximstats;
Database changed

Run the below command to list all tables in a database

4. “show tables;” command will list all tables in database.

Show tables command will show all the tables in the database you have selected.

Example :
 

[root@server ~]# mysql -u root -p’password’
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| cphulkd |
| eximstats |
| mysql |
| performance_schema |
+——————–+
5 rows in set (0.00 sec)

mysql> use eximstats;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+———————+
| Tables_in_eximstats |
+———————+
| defers |
| failures |
| sends |
| smtp |
+———————+
4 rows in set (0.00 sec)

mysql>

 
show tables command mySQL