What is the default port of MySQL server

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

MySQL is an open-source relational database management system that is widely used for web applications, data warehousing, and logistics. It is known for its reliability, robustness, and compatibility with numerous programming languages. One fundamental aspect of networking with MySQL is understanding its default port settings, which are critical when configuring firewalls, setting up connections, and troubleshooting.

Understanding Ports and MySQL Connectivity

Ports are communication endpoints that are crucial for network communication between applications and services. When a service like MySQL is started, it listens for connections on its respective port. MySQL’s default port is 3306. This means any application that wants to communicate with the MySQL service needs to be directed to this port unless it has been manually changed.

Checking the Default MySQL Port

You can check the port that MySQL is using by querying the global variables. Here is an SQL query that will help you identify the currently set port:

SHOW GLOBAL VARIABLES LIKE 'port';

This SQL statement should return a table displaying the port variable and its value, which typically should be 3306, unless it’s been customized.

Networking Basics with MySQL

When a MySQL server starts up, it listens on the designated port, which defaults to 3306. To connect to the MySQL server using the default port, you don’t have to specify the port number if you’re using client tools that assume the default. For example:

mysql -u username -p

The above command lets you log in to the MySQL server as ‘username’. MySQL assumes that the server is listening on the default port. If MySQL is configured to use another port, you would specify it with the ‘-P’ option:

mysql -u username -p -P 1234

In this case, we have set ‘1234’ as the custom port number.

Connecting to MySQL Programmatically

When connecting to the MySQL database from an application, you specify the port as part of the connection string. Below are examples in various programming languages:

PHP

$conn = new mysqli('localhost', 'username', 'password', 'database', 3306);

If the port is the default (3306), specifying it is optional.

Python

import mysql.connector

conn = mysql.connector.connect(
    host='localhost',
    database='exampledb',
    user='username',
    password='password',
    port=3306
)

Similarly in Python, if the port is other than 3306 you must specify it, but it’s optional for the default port.

Java

String url = "jdbc:mysql://localhost:3306/exampledb";
String user = "username";
String password = "password";

Connection conn = DriverManager.getConnection(url, user, password);

In Java’s JDBC URL, the port number is concatenated to the host.

Changing MySQL’s Default Port

If for some reason the MySQL server needs to operate on a different port, the port setting can be changed in the main configuration file:

On Windows

[mysqld]
port = 1234

The above lines are to be placed in the ‘my.ini’ file commonly located within the MySQL server directory or sometimes in the Windows directory.

On Unix/Linux

[mysqld]
port = 1234

On Unix or Linux systems, it’s often located at ‘/etc/mysql/my.cnf’ or ‘/etc/my.cnf’. After making changes, the MySQL server must be restarted.

Advanced MySQL Networking

MySQL can also be bound to a specific network interface using the ‘bind-address’ option, which is often paired with the port configuration. In a configuration file, it would look like this:

[mysqld]
bind-address = 192.168.1.5
port = 3306

Note that by changing the bind-address, you can instruct MySQL to listen only on a certain network interface.

Security Considerations

Knowing and optionally changing the MySQL port can be important for security reasons. It’s common to change the default port to a non-standard one to obscure the service from automated scans. It is also crucial to properly configure firewalls to allow traffic on the MySQL port, like so:

Using iptables on Linux

iptables -A INPUT -i eth0 -p tcp --dport 3306 -j ACCEPT

This command allows incoming connections on port 3306.

Conclusion

In conclusion, the default port for the MySQL server is 3306. This value is integral for setting up proper database connectivity, whether configuring server settings or client applications. While the default port is adequate for most scenarios, understanding how to check and change it is a valuable skill for any database administrator or developer concerned with customization and security.