I got the following 2 commands from mysql website. I tried to change the default mysql port from 3306 to 4040 and back. But it did not work as expected.

Redirect to Proxy:

iptables -t nat -I PREROUTING -s ! 127.0.0.1 -p tcp --dport 3306 -j REDIRECT --to-ports 4040

Back to default:

iptables -t nat -D PREROUTING -s ! 127.0.0.1 -p tcp --dport 3306 -j REDIRECT --to-ports 4040

What am I missing here?

Update:

The change is being shown up in the status as follows:

# /etc/init.d/iptables status
Table: nat
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    REDIRECT   tcp  -- !127.0.0.1            0.0.0.0/0           tcp dpt:3306 redir ports 4040 
2    REDIRECT   tcp  -- !127.0.0.1            0.0.0.0/0           tcp dpt:3306 redir ports 4040 

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         
link|improve this question

Is there a reason why you don't just configure MySQL to listen on port 4040? A firewall redirect seems a bit overkill. The MySQL webpage you refer to is about configuring a MySQL proxy, not about changing the default port. – Tom Shaw May 30 '11 at 9:44
Because I do not want to restart mysql service. It is OK to restart iptables. The same page has a section called "simple logging" from where I have picked these 2 commands. – shantanuo May 30 '11 at 9:47
feedback

1 Answer

up vote 2 down vote accepted

For what you want to achieve, you have the ports the wrong way around. You want to redirect from 4040 to 3306. So try:

iptables -t nat -I PREROUTING -s ! 127.0.0.1 -p tcp --dport 4040 -j REDIRECT --to-ports 3306

You also need to make sure you remove the other incorrect rules as well.

P.S. If this is a production system that can't even afford downtime to change the listener port, you should not be trying iptables rules from the Internet! Test it on a test machine first.

link|improve this answer
How do I remove the rules set earlier? – shantanuo May 30 '11 at 9:56
@shantanuo: Use the -D command that you listed in your question. – Tom Shaw May 30 '11 at 10:01
>> Test it on a test machine first. # yes. ofcourse – shantanuo May 30 '11 at 10:13
@shantanuo: Sorry, I don't want to be responsible if someone running a nuclear power plant takes my advice and causes a meltdown :) – Tom Shaw May 30 '11 at 10:22
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.