Understanding MySQL Connections
After installing MySQL and learning about MySQL Server and client tools such as MySQL Workbench and the MySQL CLI, the next important concept is MySQL connections.
A MySQL connection is the process through which a client tool or application communicates with the MySQL Server. Without a connection, users cannot send SQL queries or retrieve data from the database.
Understanding how connections work helps beginners clearly see how applications interact with databases.
What is a MySQL Connection
A MySQL connection is a communication link established between a client application and the MySQL Server.
This connection allows the client to:
Send SQL queries to the server.
Retrieve query results
Modify database data
Perform database administration tasks
Every time a user runs a query in MySQL Workbench, MySQL CLI, or any application, a connection to the MySQL Server is required.
Components Required for a MySQL Connection
To establish a connection to a MySQL database, several parameters are required.
Host
The host specifies where the MySQL Server is located.
Examples:
localhost→ server running on the same machine192.168.1.10→ server running on another machine in the networka domain name for cloud databases
Username
The username identifies the MySQL user account that is trying to access the database.
Example:
root Different users can have different permissions depending on their roles.
Password
The password is used to authenticate the user.
It ensures that only authorised users can connect to the database.
Port Number
The port number identifies the communication channel used by MySQL Server.
The default MySQL port is:
3306 Unless configured differently, MySQL listens for connection requests on this port.
Database Name
Sometimes a specific database name is also provided when connecting.
This tells MySQL which database the user wants to work with after the connection is established.
Types of MySQL Connections
MySQL connections can be categorised based on where the client and server are located.
Local Connection
A local connection occurs when the client tool and MySQL Server are running on the same computer.
Example:
Host: localhost This is the most common type of connection used while learning MySQL.
Remote Connection
A remote connection occurs when the MySQL Server is located on another computer or server.
Example:
Host: 192.168.1.20 Remote connections are commonly used in:
cloud databases
production servers
web applications
Proper security and permissions are required to allow remote access.
Connecting to MySQL Using MySQL Workbench
MySQL Workbench provides an easy graphical way to connect to the MySQL Server.
Steps to Create a Connection
Open MySQL Workbench
Click New Connection
Enter the connection details:
Connection Name
Hostname
Username
Password
Port
Click Test Connection
If successful, click OK.
Once the connection is created, users can start writing and executing SQL queries.
Connecting to MySQL Using Command Line
The MySQL Command Line Interface can also be used to connect to the MySQL Server.
Example command:
mysql -h localhost -u root -p Explanation:
mysql→ MySQL command-line client-h→ host name-u→ username-p→ prompts for password
After entering the password, the connection will be established, and SQL queries can be executed.
What Happens When a Connection is Established
When a connection is created, the following steps occur:
The client tool sends a connection request to the MySQL Server.
The server verifies the username and password.
The server checks user permissions.
If authentication is successful, the server allows the connection.
The client can now send SQL queries to the server.
If authentication fails, the connection is rejected.
Multiple Connections to MySQL Server
One MySQL Server can support multiple simultaneous connections.
For example:
MySQL Workbench
MySQL CLI
A web application
A backend API
All of these can connect to the same MySQL Server at the same time.
The server manages these connections and ensures data consistency.
Closing a MySQL Connection
After completing database operations, it is good practice to close the connection.
In MySQL CLI, the connection can be closed using:
exit; Closing unused connections helps free system resources and improves server performance.
Key Points to Remember
A MySQL connection links a client tool with the MySQL Server.
Connection requires host, username, password, and port.
MySQL supports local and remote connections.
Multiple clients can connect to the same server simultaneously.
Connections allow users to execute SQL queries and manage databases