How to List all Database Name in MySQL Database Server

Posted on

This article is an article written to list all of databases’name in MySQL Database Server. The main purpose is to enlist all of the databases’ name and only the name of the databases exist in MySQL Database Server. First of all, the most important thing is to be able to connect to the MySQL Database itself. The article which can be used as a reference exists in the article titled ‘How to Access MySQL Database Server via Command Line’ in this link . So, after connecting to the database, either it is done locally or remotely, the connection process if it is successfully carried out, it will brought the user whom connect to the database to the MySQL Command Console as shown below :

user@hostname:~$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, 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> 

But the above connection is just a test or a trial which is used to prove that the connection to MySQL Database intended can be made. The execution of the command itself is not being executed in the MySQL Command Console. The execution command is actually done in the bash prompt as shown below :

user@hostname:~$ mysql -uroot -p -e 'SHOW DATABASES' > /home/user/database-name.txt
Enter password: 
user@hostname:~$

Desription : 
mysql : the command tool which in this case 'mysql'
-uroot : additional parameter to specify user which is used to connect to MySQL Database Server, in this case 'root'.
-p : additional parameter to specify the password to be filled interactively which is associated with the password of user 'root'.
-e : additional parameter to specify the statement to be executed. in this case, there is an extra argument after -e which is the statement executed and in this case 'SHOW DATABASES'. 
> : the redirect output sign, which is going to pass through any output generated from the command specified before the '>' sign
/home/user/database-name.txt : the file specified as the output of the command generated before '>'. 

The above command execution means execute ‘SHOW DATABASE’ in MySQL Database Server by using ‘root’ with the password specified interactively. The output of statement execution will be passed to a file location in ‘/home/user/database-name.txt’. To prove whether the execution of the command is carried out successfully, just open the file named ‘database-name.txt’ or any name used for the file. In the above case, the name is ‘database-name.txt’ and it is located in ‘/home/user’.

user@hostname:~$ vim /home/user/database-name.txt

Below is the content’s example :

Database
information_schema
mysql
performance_schema
sys

Leave a Reply