Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting MySQL 3306 port to localhost with IPTABLES

Tags:

mysql

iptables

I am trying to restrict MySQL 3306 port on a linux machine from making any connections to anything other than localhost to prevent outside attacks. i have the following code, i am not sure if it's correct:

iptables -A INPUT -p tcp -s localhost --dport 3306 -j ACCEPT

iptables -A OUTPUT -p tcp -s localhost --dport 3306 -j ACCEPT

iptables -A INPUT -p tcp --dport 3306 -j DROP

iptables -A OUTPUT -p tcp --dport 3306 -j DROP

my other question is - is it correct to only give localhost access? this is a standard dedicated centos webserver with more than 30 domains on it.

like image 629
califmerchant Avatar asked Jun 12 '12 17:06

califmerchant


People also ask

How do I block MySQL port 3306?

Press Ctrl + F and write 3306 to find out which Application is using PORT 3306. After this, Go to Task Manager via Search Bar or by pressing CTRL + ALT + DEL . Then Under the Background Processes, find out mysqld.exe , right-click on it and you will find an option to close it, namely " End Task ".

How do I fix MySQL port 3306 already in use?

According to your error,some service has already used it. Depends on your OS,check who are using it. For example for netstat -lp | grep 3306 , you can close this service and restart your mysql or change to another port OR you an change mysql default port to listen in your my. cnf file.

Is MySQL port 3306 secure?

Is It Safe to Open Port 3306? In general, you should not open port 3306 as it can make your server vulnerable to attack. If you need to connect to your database remotely, there are more secure options than opening port 3306, such as using an SSH tunnel.


2 Answers

Why not just turn off networking with MySQL?

Add to my.cnf:

skip-networking

It's supposed to also give a negligible performance improvement by forcing connection through pipes, which skips over lots of tests used for the networking section. Please note you will need to use localhost, not 127.0.0.1, after the change.

like image 57
Will Morgan Avatar answered Sep 22 '22 07:09

Will Morgan


iptables -A INPUT -p tcp --dport 3306 -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

The above rule is for converting two lines into single one.

Answer to your second question:

If you do not want to provide mysql access from other than localhost, then it is perfect to configure this way. Simple. :-)

like image 45
Nikunj MAster Avatar answered Sep 26 '22 07:09

Nikunj MAster