Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Select a Database in MySQL from the Command Line

Log in to the MySQL client, list your databases and pick one with the USE command.

Databases2 min read5 screenshots

The short answer

USE YourDatabaseName;

Where MySQL runs

You may use a local server on Linux or Windows, or a remote server. The command is the same in both cases.

Before you start

On Windows, check that the MySQL service runs. Open Command Prompt as administrator and go to the MySQL bin folder:

sc query MySQL80
Checking that the MySQL service is running

(The service name depends on the version; MySQL80 is for 8.0.)

Log in

mysql -u YourUserName -p
Logging in to MySQL

Enter your password when asked.

List the databases

SHOW DATABASES;
Listing the databases

Select a database

USE YourDatabaseName;

Example:

USE world;
Selecting the world database

MySQL answers Database changed. From now on, queries run on that database. Check the current one:

SELECT DATABASE();

Select a database when you log in

You can choose the host, the port and the database in one command:

mysql -h ServerNameOrIP -P PortNumber -u UserName -p -D DatabaseName

Example:

mysql -h localhost -P 3306 -u Amin -p -D world
Connecting to a specific server and database

Tips

  • Names on Linux are case-sensitive for databases and tables.
  • If you get Unknown database, check the spelling with SHOW DATABASES;.
  • If you get Access denied, the user has no rights on that database.

Quick recap

  • mysql -u user -p, then SHOW DATABASES; and USE name;.