How to display all of the databases in MySQL

MySQL is the most popular open source relational database management systems. This tutorial describes how to display all MySQL or MariaDB database server via the command line.

Display MySQL database

The most common way to obtain a list of MySQL database is to use the mysqlclient to connect to MySQL server and run the SHOW DATABASEScommand.

Use the following command to access the MySQL server, MySQL user and enter your password when prompted:

mysql -u user -p

If you have not set a password for the MySQL user, you can omit the -pswitch.

Execute the following command in the MySQL shell:

SHOW DATABASES;

This command will print a list of all database users have rights. The output will look like this:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| opencart           |
+--------------------+
2 rows in set (0.00 sec)

SHOW SCHEMASIs another command can be used to list the database, it is SHOW DATABASESsynonymous with the command:

SHOW SCHEMAS;

Using the output SHOW DATABASESto output the same when the command:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| opencart           |
+--------------------+
2 rows in set (0.00 sec)

Display all MySQL database

To list all the databases on the MySQL server, you need to have access to all the database as a user login, root user by default is to have permission to view all databases. It can be used SHOW DATABASESto list all the databases.

Logged in as root MySQL:

mysql -u root -p

Run SHOW DATABASESthe command:

SHOW DATABASES;

You will see a list of all databases on the MySQL server:

How to display all of the databases in MySQL

+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| database_name      |
| mysql              |
| opencart           |
| wordpress          |
| performance_schema |
| sys                |
+--------------------+

7 rows in set (0.00 sec)

Filter results

If the output of the filter in accordance with a command according to a specific mode, the operation SHOW DATABASESusing the time LIKEclause.

SHOW DATABASES LIKE pattern;

For example, the following statement will return the name to "open" the beginning of all databases:

SHOW DATABASES LIKE 'open%';
+--------------------+
| Database           |
+--------------------+
| opencart           |
+--------------------+
1 rows in set (0.00 sec)

Percent sign ( %) represents zero, one, or more characters.

If you want a more sophisticated searches from information_schemathe database schemataaccording to the conditions lookup table.

The following statement will provide you with a list of all databases in order to "open" or "word" starting with:

SELECT schema_name
FROM information_schema.schemata
WHERE schema_name LIKE 'open%' OR
schema_name LIKE 'word%';
+--------------------+
| Database           |
+--------------------+
| opencart           |
| wordpress          |
+--------------------+
2 rows in set (0.00 sec)

Display MySQL database from the command line

To obtain a list of databases in MySQL shell without logging in, you can use the mysqlcommand with the -eoption (on behalf execute), it can also be used mysqlshowto display database and table information command.

When you want to use the shell script uses MySQL database, which is particularly useful.

Run the following command in the terminal to display a list of all databases:

mysql -u user -p -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| opencart           |
+--------------------+

The following is an mysqlshowexample command:

mysqlshow -u user -p

The output will be the same on a command output.

If you want to filter the output, you can use the grep command.

in conclusion

You have learned how to obtain a list of all MySQL server databases.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159898.htm