MySQL列出所有数据库

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

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

SHOW DATABASES;

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

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命令:

mysql> SHOW DATABASES;

+--------------------+

| Database |

+--------------------+

| information_schema |

| crmdb |

| mysql |

| newdb |

| performance_schema |

| testdb |

| zaixiandb |

| zaixiandb_backup |

+--------------------+

8 rows in set

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

mysql> SHOW SCHEMAS;

+--------------------+

| Database |

+--------------------+

| information_schema |

| crmdb |

| mysql |

| newdb |

| performance_schema |

| testdb |

| zaixiandb |

| zaixiandb_backup |

+--------------------+

8 rows in set

如果要查询与特定模式匹配的数据库,请使用LIKE子句,如下所示:

SHOW DATABASES LIKE pattern;

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

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命令相同的结果。

SELECT schema_name

FROM information_schema.schemata;

以下SELECT语句返回名称以’schema‘或’db‘结尾的数据库。

SELECT schema_name

FROM information_schema.schemata

WHERE schema_name LIKE '%schema' OR

schema_name LIKE '%db';

它返回以下结果集:

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 |

| zaixiandb |

+--------------------+

6 rows in set

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

上一篇:

MySQL管理

下一篇:

MySQL全文搜索