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.
我有一些MySQL数据库。 我以前没有使用MySQL的经验。 我们如何使用bash列出现有数据库?MySQL和MariaDB使用相同的代码库和工具。 因此,我们也可以使用以下说明来列出MariaDB。
检查数据库服务 (Check Database Service)
First we will check if the MySQL or MariaDB database service is running properly with the systemctl command like below.
首先,我们将使用systemctl命令检查MySQL或MariaDB数据库服务是否正常运行,如下所示。
$ systemctl status mysql
![Check Database Service](https://i-blog.csdnimg.cn/blog_migrate/c67d754a494296bfbcd8c51f45d7895f.png)
As we can see from systemctl
output the database service is working properly.
从systemctl
输出中可以看到,数据库服务正常运行。
登录到MySQL Interactive Shell (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交互式shell。 我们将通过提供root
用户来使用mysql
工具
$ mysql -u root
![Login To MySQL Interactive Shell](https://i-blog.csdnimg.cn/blog_migrate/b76217c0879eff579ef227f1a2390349.png)
Now we will use show databases
command to list existing databases. ;
is important which means end of the command.
现在,我们将使用show databases
命令列出现有数据库。 ;
重要,这意味着命令结束。
> show databases;
![List Databases](https://i-blog.csdnimg.cn/blog_migrate/f2b07b6a8e035696f0309353999e7735.png)
We can see that there is 5 databases in this MySQL Server.
我们可以看到该MySQL服务器中有5个数据库。
列出而不输入MySQL Shell (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.
进入MySQL数据库服务器并运行show database
命令可能是一项琐碎的任务。 我们可以使用-e
选项从Linux bash shell直接运行show database
命令,如下所示。
$ sudo mysql -u root -e "show databases;"
![List Without Entering MySQL Shell](https://i-blog.csdnimg.cn/blog_migrate/c602b7156b0ddaaea04b864f202177c3.png)
列表而不从远程数据库服务器输入MySQL Shell (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.
如果要从远程数据库服务器列出数据库,则需要提供远程系统的主机名或IP地址。 在此示例中,我们将使用选项-h
,它是host
的简写形式。 我们将连接远程服务器192.168.142.144。
$ mysql -u root -h 192.168.142.144 -e "show databases;"