How to Connect to MySQL Server

Summary: in this tutorial, you will learn how to connect to a MySQL Server or MySQL database using MySQL command-line client and MySQL Workbench.

After the installation of the MySQL server and its related tools, it’s time to connect the MySQL server. You can connect MySQL server using any client tool such as MySQL command-line client and MySQL Workbench.

Connect to MySQL Server using MySQL command-line client

MySQL command-line client is a popular client program by which you can connect to the MySQL server and interact with the MySQL database.

In Windows, you can go to the start menu and can find the MySQL command-line client under the MySQL folder. Alternatively, you can navigate to the bin folder of the MySQL installation directory and invoke mysql application as below.

How to Connect to MySQL Server

mysql

In case the mysql program is already in the PATH, you can simply invoke it using mysql command.

To connect to the MySQL server, you can use the following command:

mysql -u root -p

It will prompt for the root password for the MySQL server. You need to provide the root credential correctly here and the MySQL server will connect as follows.

Enter password: ********

After providing the correct password, if everything is fine it will connect to the MySQL server as below.

mysql>

How to Connect to MySQL Server

You can verify if the MySQL command-line client successfully connected to the MySQL server or not by issuing a simple query. You can list the current databases present in the MySQL database using the SHOW DATABASES statement.

mysql> show databases;

The above statement gives the following output:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
6 rows in set (0.25 sec)

Connect to MySQL Server using MySQL Workbench

MySQL Workbench is GUI based tool by which you can connect to the MySQL server and interact with MySQL. You can connect to the MySQL database using MySQL Workbench using the following steps:

Step 1: Lunch MySQL Workbench either from the start menu or from the shortcut that you have created.

How to Connect to MySQL Server

You can connect to the MySQL Server by two methods. 1) By navigating Database -> Connect to Database; 2) By clicking the + button that locates next to the MySQL Connections.

Now click the + button next to the MySQL Connections to continue.

Step 2: After clicking the + button the below window will appear.

How to Connect to MySQL Server

Here, enter the connection name as per your choice. I have given localhost. By default, the username is root. If you use a different user account, you can change it in the Username textbox.

Step 3: Next, click the Store in Vault... button to enter the password for the user. A window will display. You enter the current password and click the OK button.

How to Connect to MySQL Server

Step 4: Click the Test Connection button to test if the connection to the MySQL Server is successful or not. Then click the OK button if the connection is established successfully.

How to Connect to MySQL Server

Step 5: Click the OK button to save the connection.

How to Connect to MySQL Server

Step 6: Click the newly created connection under MySQL Connections to connect to the MySQL Server:

How to Connect to MySQL Server

Step 7: MySQL Workbench display with the current schemas and a pane for entering queries:

How to Connect to MySQL Server

In this tutorial, you have learned how to connect to the MySQL Server using mysql command-line client and MySQL Workbench.