# 列出数据库

在本教程中，您将学习如何使用MySQL `SHOW DATABASES`命令列出MySQL数据库服务器中的所有数据库。

## 使用MySQL SHOW DATABASES

要列出MySQL服务器主机上的所有数据库，请使用`SHOW DATABASES`命令，如下所示：

```sql
SHOW DATABASES;
```

例如，要列出本地MySQL数据库服务器中的所有数据库，请首先登录到数据库服务器，如下所示：

```sql
C:\Users\Administrator>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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`命令：

```sql
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crmdb              |
| mysql              |
| newdb              |
| performance_schema |
| testdb             |
| yiibaidb           |
| yiibaidb_backup    |
+--------------------+
8 rows in set
```

`SHOW SCHEMAS`命令是`SHOW DATABASES`的同义词，因此以下命令将返回与上述相同的结果：

```sql
mysql> SHOW SCHEMAS;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crmdb              |
| mysql              |
| newdb              |
| performance_schema |
| testdb             |
| yiibaidb           |
| yiibaidb_backup    |
+--------------------+
8 rows in set
```

如果要查询与特定模式匹配的数据库，请使用[LIKE](http://www.yiibai.com/mysql/like.html)子句，如下所示：

```sql
SHOW DATABASES LIKE pattern;
```

例如，以下语句返回以字符串“`schema`”结尾的数据库;

```sql
mysql> SHOW DATABASES LIKE '%schema';
+--------------------+
| Database (%schema) |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set
```

重要的是要注意，如果MySQL数据库服务器以`-skip-show-database`启动，则除非具有`SHOW DATABASES`权限，否则不能使用`SHOW DATABASES`语句。

## 从information\_schema查询数据库数据

如果`LIKE`子句中的条件不足，可以直接从`information_schema`数据库中的`schemata`表查询数据库信息。

例如，以下查询返回与`SHOW DATABASES`命令相同的结果。

```sql
SELECT schema_name 
FROM information_schema.schemata;
```

以下[SELECT](http://www.yiibai.com/mysql/select-statement-query-data.html)语句返回名称以’`schema`‘或’`db`‘结尾的数据库。

```sql
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name LIKE '%schema' OR 
      schema_name LIKE '%db';
```

它返回以下结果集：

```sql
mysql> SELECT schema_name
FROM information_schema.schemata
WHERE schema_name LIKE '%schema' OR 
      schema_name LIKE '%db';
+--------------------+
| schema_name        |
+--------------------+
| information_schema |
| crmdb              |
| newdb              |
| performance_schema |
| testdb             |
| yiibaidb           |
+--------------------+
6 rows in set
```

在本教程中，您已经学习了如何使用`SHOW DATABASES`命令显示MySQL服务器中的所有数据库，或者从`information_schema`数据库中的`schemata`表进行查询。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang-book.gitbook.io/mysql/di-ba-zhang/lie-chu-shu-ju-ku.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
