I have some MySQL databases. I have no previous experience with MySQL. How can we list existing databases with bash?MySQL and MariaDB are using the same code base and tools. So we can use the following instructions to list MariaDB too.
Check Database Service
First we will check if the MySQL or MariaDB database service is running properly with the systemctl command like below.
$ systemctl status mysql

As we can see from systemctl
output the database service is working properly.
Login To MySQL Interactive Shell
In order to list databases, we need to connect and login to MySQL interactive shell. We will use mysql
tool by providing the user root
$ mysql -u root

Now we will use show databases
command to list existing databases. ;
is important which means end of the command.
> show databases;

We can see that there is 5 databases in this MySQL Server.
List Without Entering MySQL Shell
Entering MySQL database server and running show database
command may a trivial task. We can directly run show database
command from the Linux bash shell like below by using -e
option.
$ sudo mysql -u root -e "show databases;"

List Without Entering MySQL Shell From Remote Database Server
If we want to list databases from a remote database server we need to provide the remote system hostname or IP address. In this example, we will use the option -h
which is a short form of host
. We will connect remote server 192.168.142.144.
$ mysql -u root -h 192.168.142.144 -e "show databases;"