Restricting where MySQL or MariaDB listens reduces accidental exposure of the database service while still allowing controlled access from a private network, VPN, or SSH-tunneled workflow.
The mysqld server binds a TCP socket (commonly port 3306) based on values loaded from its configuration files. The bind-address directive under the [mysqld] section determines whether the daemon accepts TCP connections only from localhost, only from a specific host IP, or (when explicitly configured) from broader network scopes.
The bind-address value must be an IP address assigned to the host (IPv4 or IPv6), not an interface name like eth0. Moving beyond localhost affects exposure and routing, but does not grant access by itself; firewall policy and database account host permissions must also allow the connection, and the service must be restarted to apply the listener change.
Setting bind-address to 0.0.0.0 exposes the database on all IPv4 interfaces. Use this only when host firewall rules and database account restrictions are already in place, or the service may become internet-facing.
$ sudo docker exec sg-mysql ip --brief address show scope global | rg '^eth0' eth0 UP 192.0.2.40/24
$ sudo docker exec sg-mysql ss --listening --numeric --tcp --processes | rg ':3306 ' LISTEN 0 151 192.0.2.40:3306 0.0.0.0:*
No output usually means the service is stopped, TCP listening is disabled via skip-networking, or the port is different.
$ sudo docker exec sg-mysql grep --recursive --line-number --extended-regexp '^[[:space:]]*bind-address' /etc/mysql /etc/mysql/conf.d/bind-address.cnf:2:bind-address = 192.0.2.40
MariaDB commonly uses /etc/mysql/mariadb.conf.d/50-server.cnf for server settings on Debian/Ubuntu-style systems.
$ sudoedit /etc/mysql/conf.d/bind-address.cnf
[mysqld] bind-address = 192.0.2.40
Use 127.0.0.1 to accept local-only connections, or set a specific private IP for controlled remote access.
$ sudo docker exec sg-mysql grep --recursive --line-number --extended-regexp '^[[:space:]]*bind-address' /etc/mysql /etc/mysql/conf.d/bind-address.cnf:2:bind-address = 192.0.2.40
If multiple lines appear, the last-loaded file can override earlier values; comment out duplicates to keep the effective listener unambiguous.
$ sudo docker restart sg-mysql
sg-mysql
On non-container hosts, restart the systemd unit for MySQL or MariaDB.
$ sudo docker exec sg-mysql ss --listening --numeric --tcp --processes | rg ':3306 ' LISTEN 0 151 192.0.2.40:3306 0.0.0.0:*
$ mysql --host=192.0.2.40 --port=3306 --user=appuser --password --protocol=TCP --execute 'SELECT 1;' 1 1
If the TCP connection opens but authentication fails, review user host permissions (for example, 'appuser'@'192.0.2.%').