Support >
  About cybersecurity >
  Professional procedures for modifying and troubleshooting MySQL port configurations

Professional procedures for modifying and troubleshooting MySQL port configurations

Time : 2026-05-11 11:11:34
Edit : DNS.COM

MySQL database services use port 3306 as the standard listening port by default. However, in real-world production environments, changing the default port is a common and necessary operation. The main applicable scenarios fall into three categories: First, avoiding port conflicts. When multiple MySQL instances need to run on the same server, or when port 3306 is already occupied by other services, a different port number must be configured for each MySQL instance. Second, security hardening. Migrating the database port from the default port to a custom port can reduce the probability of targeted attacks by automated scanning tools and reduce the known risk surface exposed to the public network. Third, meeting enterprise compliance requirements. Many enterprises' internal security protocols explicitly require that critical database services not use the default port to comply with port management standards.

MySQL port configuration is divided into two types: temporary changes and permanent changes. These two types differ fundamentally in their effectiveness and applicable scenarios.

Temporary configuration: Specifying the port number when starting the MySQL service via command-line parameters. Execute the command:

mysqld --port=newport_number

This will start the service and listen on the specified port. This method requires no modification to any configuration files and is simple to operate. However, its core limitation is that the changes are only effective in the current session; the default 3306 port configuration will be restored after the MySQL service restarts. Therefore, temporary configurations are only suitable for single-use testing or debugging scenarios and are not suitable for production deployments.

Permanent Configuration: This involves modifying the MySQL configuration file and restarting the service to make the port setting effective permanently. This method requires modifying the core MySQL configuration fileeither `/etc/my.cnf` or `/etc/mysql/my.cnf` on Linux and `C:\ProgramData\MySQL\MySQL Server X.X\my.ini` on Windows. If the actual path to the configuration file cannot be located, you can manually confirm the location of the configuration file loaded when MySQL starts by executing the command `mysqld --help --verbose | grep "Default options"` in the terminal.

Verification Methods for Successful Port Configuration

After configuration, you need to verify whether the port changes have taken effect through a combination of methods.

Method 1: Query the port internally within MySQL. Log in to the MySQL command-line client and execute the command `SHOW VARIABLES LIKE 'port';` or `SHOW GLOBAL VARIABLES LIKE 'port';`. This will return the port value that the current MySQL instance is actually listening on.

Method 2: Use system network commands to check the listening status. In the Linux terminal, execute the command `sudo netstat -tulnp | grep mysql` to view the port information that the MySQL process is actually listening on. You can also directly check a specific port by executing `sudo netstat -tuln | grep :3307` to see if the new port is in a LISTEN state.

Method 3: Confirm via process parameters. Execute the command `ps -ef | grep mysqld` to check if the MySQL process's startup parameters include `--port=new port` or other port-specific information. This method also applies to MySQL instances started with temporary configuration.

After changing the MySQL port, simply completing the server-side configuration is insufficient. The following accompanying configurations must be completed simultaneously; otherwise, the connection will fail.

Client connection parameter update. All applications connecting to the MySQL database must update the port number in their connection string to the new port. When connecting via command line, use `mysql -h server_address -P new_port_number -u username -p`, where `-P` is uppercase and specifies the port; `-p` is lowercase and is the password parameter. These two should not be confused. At the code level, for example, the JDBC connection string needs to be changed from `jdbc:mysql://localhost:3306/dbname` to `jdbc:mysql://localhost:3307/dbname`. Note that if the connection address uses `localhost`, the MySQL client may prioritize using the Unix socket for local connection and ignore the `-P` port parameter. To force a TCP connection, replace `localhost` with a specific IP address such as `127.0.0.1`.

Firewall port access. The new port must be added to the firewall's allowed rules; otherwise, connection requests from external machines will be directly blocked by the operating system. For Linux+Ubuntu systems using UFW:

sudo ufw allow 3307/tcp

For CentOS/RHEL systems using firewalld:

sudo firewall-cmd --add-port=3307/tcp --permanent && sudo firewall-cmd --reload

For Windows systems, execute as administrator:

netsh advfirewall firewall add rule name="MySQL New Port" dir=in action=allow protocol=TCP localport=3307

SELinux policy configuration (only applicable to Linux distributions with SELinux enabled). SELinux by default only allows port contexts of type `mysql_port_t` to bind to 3306. If MySQL changes to use a non-standard port (such as 3307), SELinux will prevent MySQL from binding to that port. The solution is to execute:

sudo semanage port -a -t mysql_port_t -p tcp 3307

If the system prompts:

semanage: command not found

You need to install the `policycoreutils-python-utils` package first.

Cloud platform security group configuration. When using cloud servers, in addition to the operating system firewall, the inbound rules of the security group in the cloud vendor's console also need to add TCP allow policies for the new port. This configuration is often overlooked by users and is one of the common reasons for remote connection failures.

Check the `bind-address` parameter. The `bind-address` parameter in the MySQL configuration file determines the network interface that the server listens on. If this parameter is set to `127.0.0.1`, MySQL only listens on the local loopback interface, and any external connection requests cannot reach this service, regardless of firewall configuration. To enable remote access, `bind-address` needs to be set to `0.0.0.0` or a specific internal IP address of the server.

Running multiple MySQL instances on the same physical server is a very practical scenario, such as isolating databases for different tenants or deploying test and pre-release environments on the same host. The core principle of multi-instance deployment is to assign a unique port number to each MySQL instance and configure independent configuration files, data directories (`datadir`), socket files, and error log (`log-error`) paths for each instance. Each configuration file sets its own port parameters; for example, instance 1's configuration file is `/etc/my1.cnf`, containing `port=3306`; instance 2's configuration file is `/etc/my2.cnf`, containing `port=3307`, and so on.

If connection fails after port modification, troubleshoot using the following diagnostic path: First, confirm if the MySQL service is running normally by executing `systemctl status mysql` to check the service status. Second, confirm the port listening status by executing `netstat -tuln | grep port_number` to check if the new port is in the LISTEN state. If the configuration is correct but there is no listening, it may be that MySQL failed to start; check the error log to confirm the cause. Then, check the firewall rules, SELinux/AppArmor to ensure the new port is allowed, the TCP port policy in the cloud security group rules, the `bind-address` configuration to allow remote access, and the correctness of the port parameters specified for client connections.

Regarding new port selection, it is recommended to prioritize port numbers in the range of 1024-49151. These ports are registered or commonly used user ports, relatively stable, and less susceptible to dynamic allocation. Avoid port numbers bound to specific applications (such as port 8080, widely used by web containers like Tomcat) to reduce potential port conflict risks.

Configuring MySQL ports is essentially adjusting basic parameters at the database network layer. Permanent configuration is achieved by editing the configuration file and restarting the service. After modification, client connection parameters, firewall rules, and cloud security group policies must be updated synchronously. If SELinux is enabled on the system, the new port must also be included in the `mysql_port_t` domain type context; otherwise, even if the firewall allows it, SELinux will still block the binding operation. For production deployments, it is recommended to combine IP whitelisting and SSH tunneling to access the database to achieve defense in depth. A systematic configuration and verification process can ensure the correctness of MySQL port changes and the continuous stable operation of the database service.

DNS Luna
DNS Amy
DNS NOC
Title
Email Address
Type
Information
Code
Submit