On some Linux distributions, port 3306 is open after you install MySQL. This article describes two methods for restricting or blocking access to port 3306 on a semi-managed server.
If you do not need to access MySQL from an external computer, you can disable MySQL networking.
Follow the appropriate procedure below for your Linux distribution.
To disable MySQL networking on CentOS and Fedora, follow these steps:
#skip-networking
Delete the # sign at the beginning of the line so the line looks like the following:
skip-networking
Type the following command to restart the MySQL service:
service mysqld restart
Port 3306 is now closed on the server.
By default, MySQL on Debian and Ubuntu is configured to only use the localhost interface (IP address 127.0.0.1) for networking. This means that port 3306 is closed to external connections. To confirm that this is the configuration on your server, follow these steps:
bind-address = 127.0.0.1
To restart the MySQL service, type the following command:
service mysql restart
Port 3306 is now closed on the server.
You can use iptables to create firewall rules that restrict access to port 3306. The advantage of this method is that you can selectively grant or deny access to port 3306 based on IP addresses or other criteria.
For example, to block external access to port 3306 completely, type the following command:
iptables -A INPUT -p tcp --dport 3306 -j DROP
Similarly, to grant access to a specific IP address and block all others, type the following commands. Replace xxx.xxx.xxx.xxx with the IP address for which you want to grant access:
iptables -A INPUT -p tcp --dport 3306 -s xxx.xxx.xxx.xxx -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j DROP
You can grant MySQL access to additional IP addresses by inserting rules in the INPUT chain before the DROP rule. For example:
iptables -I INPUT 1 -p tcp --dport 3306 -s xxx.xxx.xxx.xxx -j ACCEPT