MySQL is a very popular opensource database. MySQL is created by MySQL AB a Swedish company and then bought by Sun. Sun is bought by Oracle and MySQL is currently owned by Oracle. MySQL is very popular in the opensource community. In this post, we will look at how to list databases in MySQL.
MySQL是一个非常流行的开源数据库。 MySQL由瑞典公司MySQL AB创建,然后被Sun收购。 Sun被Oracle收购,MySQL目前归Oracle所有。 MySQL在开源社区中非常流行。 在本文中,我们将研究如何在MySQL中列出数据库。
检查MySQL服务状态 (Check MySQL Service Status)
We need to be sure that MySQL daemon is running we will check with following systemctl command. The MySQL service name is the same mysql
.
我们需要确保MySQL守护程序正在运行,我们将使用以下systemctl命令进行检查。 MySQL服务名称与mysql
相同。
$ systemctl status mysql
![Check MySQL Service Status](https://i-blog.csdnimg.cn/blog_migrate/7b326404a829aa3ea19ab37103b97413.png)
连接MySQL服务器(Connect MySQL Server)
We will use MySQL server tools to connect MySQL daemon. mysql is the most used command-line tool to connect and manage a MySQL server. We will connect server by running mysql command with sudo which means with root privileges.
我们将使用MySQL服务器工具连接MySQL守护程序。 mysql是连接和管理MySQL服务器的最常用的命令行工具。 我们将通过使用sudo运行mysql命令来连接服务器,这意味着具有root特权。
$ sudo mysql -u root
![Connect MySQL Server](https://i-blog.csdnimg.cn/blog_migrate/4babe6183719aa33819f6cbfd7c43f52.png)
Another way to connect MySQL daemon is by providing a password with -p
parameter
连接MySQL守护程序的另一种方法是通过-p
提供密码 参数
$ sudo mysql -u root -p
![Connect MySQL Server](https://i-blog.csdnimg.cn/blog_migrate/ab09bc798d52512500c6f91d0b74ba2d.png)
MySQL列表/显示数据库(MySQL List/Show Databases)
We will usage MySQL command show databases
to list databases. Using semicolon at the end of the command is important otherwise the command will fail.
我们将使用MySQL命令show databases
来列出数据库。 在命令末尾使用分号非常重要,否则命令将失败。
mysql> show databases;
![MySQL List/Show Databases](https://i-blog.csdnimg.cn/blog_migrate/5f42ee6b6c680269ca5827e6c124f9e9.png)
从Bash列出数据库而无需进入MySQL Shell(List Databases From Bash without Entering MySQL Shell)
We can list MySQL databases without entering the MySQL tool command line through bash. We will use a similar command to connect MySQL database but providing show database command as a parameter. We will use MySQL show databases.
我们可以列出MySQL数据库,而无需通过bash输入MySQL工具命令行。 我们将使用类似的命令连接MySQL数据库,但提供show database命令作为参数。 我们将使用MySQL show数据库。
$ sudo mysql -u root -e 'show databases;'
![List Databases From Bash without Entering MySQL Shell](https://i-blog.csdnimg.cn/blog_migrate/e94191038410a00fdf9c6aaa0dceb4f3.png)
We have issued MySQL command with -e parameter. show databases; runs as a MySQL connect in the MySQL shell.
我们已经发布了带有-e参数MySQL命令。 显示数据库; 在MySQL Shell中作为MySQL连接运行。
$ sudo mysql -u root -h 192.168.122.211 -e 'show databases;'
Here is everything is similar to the previous example except hostname which is provided as -h 192.168.122.211
. We provide hostname as the remote host, not localhost. To connect remote host firewall must give access to the port and MySQL daemon should be listening specified IP’s interface.
除了提供为-h 192.168.122.211
主机名之外,这一切都与前面的示例相似。 我们提供主机名作为远程主机,而不是localhost。 要连接远程主机,防火墙必须提供对端口的访问权限,而MySQL守护程序应侦听指定IP的接口。