MySQL Connection

11 Aug 2025 | 8 min read

A connection is a computer science facility that allows the user to connect with the database server software. A user can connect with the database server, whether on the same machine or remote locations. Therefore, if we want to work with the database server to send commands and receive answers in the form of a result set, we need connections. In this article, we are going to learn how we can connect to MySQL Server in various ways.

Understanding Connection Strings

An essential component that enables an application to establish a connection with a MySQL database is a MySQL connection string. As a collection of guidelines, it instructs the application on the location of the database how to access it and which database to use. The hostname port username password and database name are usually included in a connection string. An IP address domain name or localhost can all be used to identify the server that is hosting MySQL. MySQL uses a port as its communication endpoint the default port is 3306. A user's password and username allow them to safely access the database. Once connected the database name instructs the system which database to use.

For instance, PHP uses PDO:

After receiving this string, the MySQL driver establishes a connection. Function and security are guaranteed when connection strings are configured correctly particularly when deploying apps or establishing connections to distant servers.

MySQL Connection Types

MySQL provides various ways to connect with the database server. Once we have installed the MySQL server, we can connect it using any of the client programs that are listed below:

  1. Command-line client
  2. MySQL Workbench
  3. PHP Script.

MySQL Server Connection Using command-line client

MySQL command-line client program provides interaction with the database server in an interactive and non-interactive mode. We can see this program in the bin directory of the MySQL's installation folder. We can open the MySQL command prompt by navigating to the bin directory of the MySQL's installation folder and type:

If we find the MySQL program in the PATH, we can use the below command to connect to the MySQL Server:

In the syntax, the -u root indicates that we will connect to the MySQL server using the root user account and -p instructs MySQL to ask for a password.

Next, we need to type the password for the root user account and press Enter. If everything is correct, it should give the screen as follows:

MySQL Connection

This screen indicates that we have successfully connected with the MySQL database server, where we can send commands and receive answers in the form of a result set.

Suppose we want to display all databases available in the current server; we can use the command as follows:

It will give the below output:

MySQL Connection

If you want to disconnect the opened MySQL database server, you need to use the exit command.

Connect to Database Server Using MySQL Workbench

We can connect to the MySQL database server in workbench by using the following steps:

Step 1: Launch the MySQL Workbench. We should get the following screen:

MySQL Connection

Step 2: Navigate to the menu bar, click on the 'Database' and choose Connect to Database option or press the CTRL+U command. We can also connect with the database server by just clicking the plus (+) button located next to the MySQL Connections. See the below image:

MySQL Connection

Step 3: After choosing any of the options, we will get the below screen:

MySQL Connection

Step 4: Fill the box to create a connection, such as connection name and username, whatever you want. By default, the username is the root, but we can also change it with a different username in the Username textbox. After filling all boxes, click the Store in Vault ... button to write the password for the given user account.

MySQL Connection

Step 5: We will get a new window to write the password and click the OK button.

MySQL Connection

Step 6: After entering all the details, click on the Test Connection to test the database connectivity is successful or not. If the connection is successful, click on the OK button.

MySQL Connection

Step 7: Again, click on the OK button for saving connection setup. After finishing all the setup, we can see this connection under MySQL Connections for connecting to the MySQL database server. See the below output where we have Localhost3 connection name:

MySQL Connection

Step 8: Now, we can click this newly created connection that displays the current schemas and a pane for entering queries:

MySQL Connection

Connect to MySQL Server Using PHP Script

The simplest way to connect with the MySQL database server using the PHP script is to use the mysql_connect() function. This function needs five parameters and returns the MySQL link identifier when the connection becomes successful. If the connection is failed, it returns FALSE.

Syntax

The following is the syntax for MySQL connection using PHP Script:

Let us explain the mysql_connect() function parameters:

Server: It is the name of a host that runs the database server. By default, its value will be lcalhost:3306.

User: It is the name of a user who accesses the database. If we will not specify this field, it assumes the default value that will be the name of a user that owns the server process.

Password: It is the password of a user whose database you are going to access. If we will not specify this field, it assumes the default value that will be an empty password.

New_link: If we make a second call with the same arguments in the mysql_connect() function, MySQL does not establish a new connection. Instead, we will get the identifier of the already opened database connection.

Client_flags: This parameter contains a combination of the below constants:

  • MYSQL_CLIENT_SSL: It uses SSL encryption.
  • MYSQL_CLIENT_COMPRESS: It uses a compression protocol.
  • MYSQL_CLIENT_IGNORE_SPACE: It provides space after function names.
  • MYSQL_CLIENT_INTERACTIVE: It provides a timeout before closing the connection.

If we want to disconnect from the MySQL database server, we can use another PHP function named mysql_close(). It accepts only a single parameter that will be a connection returned by the mysql_connect() function. Its syntax is given below:

If we do not specify any resource, MySQL will close the last opened database. This function returns true when the connection is closed successfully. Otherwise returns a FALSE value.

Example

The following example explain how to connect to a MySQL server using PHP Script:

Managing and Maintaining Connections

  • In MySQL-based applications performance and stability depend on effective connection management. When several people or processes use the database its crucial to control the opening maintaining and closing of connections.
  • Reusing current database connections rather than making new ones every time is accomplished through the popular technique known as connection pooling.Response time is improved and overhead is decreased. A lot of frameworks and libraries have connection pooling built in.
  • The amount of time that the server or client waits before terminating inactive or unresponsive connections is controlled by the timeout settings. By ending inactive connections important MySQL parameters like wait_timeout and interactive_timeout prevent resource waste.
  • The maximum connections specify how many clients can connect to the database at once. The MySQL configurations max_connections parameter establishes this cap. If this threshold is reached subsequent connection attempts will be rejected until the current ones are released.
  • Finally, persistent vs. non-persistent connections describes whether a connection is re-established each time or remains open throughout several requests (persistent). Although persistent connections can boost efficiency they must be carefully controlled to prevent resource depletion.

When these factors are properly managed MySQL-based systems are guaranteed to be robust efficient and scalable.

Summary

Relational databases require MySQL connections to be established and maintained. Understanding connection string timeouts and authentication is crucial whether connecting locally or remotely via a command-line client or a programming language. In addition to enhancing performance proper connection handling guarantees your applications security and stability. Best practices that can assist you in maintaining a dependable and effective connection with your MySQL server include optimizing resource usage and limiting access through secure connections.

FAQs

1. What is the default port for MySQL connections?

MySQL connects to connections on port 3306 automatically. Usually, the MySQL server monitors this port for incoming TCP connections. If the server is running on a custom port you must include the new port in your connection string for security or configuration reasons. It might also be required to specifically open this port in network settings because firewalls or cloud services might automatically block it.

2. Why am I getting the error "Access denied for user"?

The error Access denied for user usually indicates that my credentials or permissions don't match. An incorrect username or password or an unauthorized host attempting to connect are potential causes of the problem. Because MySQL uses both the host and the username to identify users a correct username might still be rejected if the host is blocked. Make sure you are connecting from an IP address that is allowed and that the user has been given the required permissions by using the GRANT command.

3. How can I safely establish a remote connection to a MySQL server?

SSL/TLS encryption is advised. SSL certificates should be set up on the MySQL server and the client in order to safeguard private information during transmission. Rather than using root accounts for remote access use firewall rules or MySQL GRANT statements to restrict connections to particular IP addresses. Another safe remote MySQL access technique that does not require MySQL port blocking is SSH tunneling.

4. Describe the significance of connection pooling.

One method for preserving a range of reusable database connections is connection pooling. Applications pull a connection from the pool and then return it saving resources compared to creating a new connection and closing it for each request. Performance is enhanced particularly in heavily trafficked web applications. Many frameworks and drivers such as Javas JDBC or PHPs PDO come with built-in pooling mechanisms that let developers change things like idle time and pool size.

5. How do MySQL persistent connections differ from those that are not?

Because it can handle multiple requests simultaneously a persistent connection reduces maintenance expenses. They may increase server resources if not managed properly despite the fact that they boost performance in applications with high traffic. Resource management is safer when a new non-persistent connection is established for each request even though performance may be slightly impacted in busy settings.


Next TopicMySQL Workbench