Restricting MySQL port access
Ce contenu n’est pas encore disponible dans votre langue.
This guide explains two methods to restrict or block access to MySQL port 3306 on an unmanaged server.
Important: You must have root access to the server. This guide applies only to unmanaged products.
Method 1: Disable MySQL Networking
Section titled “Method 1: Disable MySQL Networking”If you do not need external access to MySQL, you can disable networking.
Even with networking disabled, you can still access MySQL securely from a remote computer using an SSH tunnel.
AlmaLinux and Fedora
Section titled “AlmaLinux and Fedora”- Log in to your server via SSH.
- Open the MySQL configuration file
/etc/my.cnfwith your preferred text editor. - Locate this line:
#skip-networkingNote: If the line does not exist, add it.
- Remove the
#so it reads:
skip-networking- Save the file and exit the editor.
- Restart MySQL:
service mysqld restartPort 3306 is now closed to external connections.
Debian and Ubuntu
Section titled “Debian and Ubuntu”- Log in to your server via SSH.
- Open
/etc/mysql/my.cnfwith your text editor. - Locate the
bind-addressline:
bind-address = 127.0.0.1- If
bind-addressis set to0.0.0.0or missing, MySQL is open to all interfaces. 127.0.0.1restricts connections to localhost only.
- Save any changes and exit.
- Restart MySQL:
service mysql restartPort 3306 is now restricted to local connections.
Method 2: Configure Firewall Rules
Section titled “Method 2: Configure Firewall Rules”You can use iptables to selectively block or allow access to port 3306.
- Block all external access:
iptables -A INPUT -p tcp --dport 3306 -j DROP- Allow a specific IP and block others (replace
xxx.xxx.xxx.xxxwith the allowed IP):
iptables -A INPUT -p tcp --dport 3306 -s xxx.xxx.xxx.xxx -j ACCEPTiptables -A INPUT -p tcp --dport 3306 -j DROPTo allow additional IPs, insert rules before the DROP rule:
iptables -I INPUT 1 -p tcp --dport 3306 -s xxx.xxx.xxx.xxx -j ACCEPTThis method provides granular control over which IPs can connect to MySQL.
Cette page vous a-t-elle aidé ?
Merci pour votre retour.