Change Listening Port of MySQL or MariaDB Server
Category : How-to
The MySQL and MariaDB server both use a file called my.cnf for parameters that are used to configure the server. This is where the port number and, if you use it, the local socket can be configured. The default port number for both MySQL and MariaDB is 3306 but you can change it as required.
A local socket is the prefered method of connecting to a database as it removes much of the overhead of creating a TCP connection and transferring data. This comes with the limitation that it can only be used if the application accessing the database is on the same machine. In larger or highly available systems this may not be possible.
A TCP connection is the only option of connecting to your MySQL or MariaDB database from a remote machine. It incurs a small penalty over a local socket and therefore slightly higher latencies. MySQL server and MariaDB can be configured to use a local socket, TCP connections or both.
We’ll be editing the my.cnf file for the following sections. Open the file in your favourite editor.
vi /etc/mysql/my.cnf
Configuring local socket use
The socket option indicates the filesystem path to the location of the socket you’d like to use. Specify a filesystem path, usually /var/run/mysql/mysqld.sock and the socket will be created when the server next starts. Remove or comment (#) the line to disable socket access.
socket = /var/run/mysqld/mysqld.sock
Restart the server for the changes to take effect.
service mysql restart
Setting or changing the TCP port
The port option sets the MySQL or MariaDB server port number that will be used when listening for TCP/ IP connections. The default port number is 3306 but you can change it as required. Use the port option with the bind option to control the interface where the port will be listening. Use 0.0.0.0 to listen on all IP addresses on the host, or specify a single one directly to listen on a single interface. Omit both of these options to disable TCP/ IP connections.
port = 1234 bind = 10.10.10.10
Restart the server for the changes to take effect.
service mysql restart
2 Comments
pierre mitham
16-Jun-2018 at 2:39 pmnote that the “bind” and “port” options don’t work for mariadb version 10.1.23
I’ve made the above edits, restarted the service and just get an error “unknown variable bind”
TB
18-Jun-2022 at 4:28 pmUsing the author’s instructions to configure using sockets rather than worked for my instance of mariadb-10.6.7-150400.1.4.x86_64.