The default port that MySQL database server is running under Linux and Unix is 3306/TCP. In order to change the default MySQL/MariaDB database port in Linux, open MySQL server configuration file for editing by issuing the below command.

# vi /etc/my.cnf.d/server.cnf                   [On CentOS/RHEL]
# vi /etc/mysql/mariadb.conf.d/50-server.cnf    [On Debian/Ubuntu] 

Search for the line stat starts with [mysqld] and place the following port directive under [mysqld] statement, as shown in the below file excerpts. Replace the port variable accordingly

port = 12345

After you’ve added the new MySQL/MariaDB port, save and close the configuration file.

If you use SELinux, you should install the following package under CentOS 7 in order to apply the required SELinux rules to allow the database to bind on the new port.

# yum install policycoreutils-python

Next, add the below SELinux rule to bind MySQL socket on the new port and restart the database daemon to apply changes, by issuing the following commands. Again, replace MySQL port variable to match your own port number.


# semanage port -a -t mysqld_port_t -p tcp 12345
# systemctl restart mariadb

On Debian/Ubuntu

# systemctl restart mysql

In order to verify if the port configuration for MySQL/MariaDB database server has been successfully applied, issue netstat or ss command and filter the results via grep command in order to easily identify the new MySQL port.

# ss -tlpn | grep mysql
# netstat -tlpn | grep mysql