Connect to MySQL Server

Created with Sketch.

Connect to MySQL Server

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

Once you have the MySQL Server installed, you can connect to it using any client program such as mysql command-line client and MySQL workbench.

Connect to MySQL Using mysql command-line client

mysql is a command-line client program that allows you to interact with MySQL in the interactive and non-interactive mode.

The mysql command-line client is typically located in the bin directory of the MySQL’s installation folder.

To invoke the mysql program, you just simply navigate to the bin directory of the MySQL’s installation folder and type:

mysql

Code language: SQL (Structured Query Language) (sql)

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

To connect to the MySQL Server, you use this command:

shell>mysql -u root -p

Code language: SQL (Structured Query Language) (sql)

-u root means that you connect to the MySQL Server using the user account root.

-p instructs mysql to prompt for a password.

You type the password for the user account root and press Enter:

Enter password: ********

Code language: SQL (Structured Query Language) (sql)

If everything is OK, you will connect to the MySQL Server with the following command:

mysql>

Code language: SQL (Structured Query Language) (sql)

To display the databases in the current server, you use the SHOW DATABASES statement:

mysql> show databases;

Code language: SQL (Structured Query Language) (sql)

Here is the output:

+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)

Code language: SQL (Structured Query Language) (sql)

Connect to MySQL Using MySQL Workbench

Step 1. Launch the MySQL Workbench.

You can connect to a MySQL Server using the Database > Connect to Database… menu or click the + button that locates next to the MySQL Connections.

Just click the + button in next to the MySQL Connections to continue.

Step 2. Enter the connection name e.g., Localhost. You can name it whatever makes sense to you. By default, the username is root. If you use a different user account, you can change it in the Username textbox.

Step 3. Click the Store in Vault ... button to enter the password for the provided user account. A window will display. You enter the password and click the OK button.

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.

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

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *