I'm trying to enable remote access to MySQL over port 3306 but having no luck. I've been given root access to a production machine so that I can grab files and databases, work on them locally, and put modified versions back.
My access allows me to SSH into the box, and it allows to FTP over SSH. I'm now trying to get MySQL access over SSH. MySQL is running (listening locally) and accessible on the command line and can access it no problem.
I realise that I could just dump the database, recover the file locally and perform my modification. However, in the future I will probably need to change some stuff, and I'd prefer not to have to do that on the command line, as its quicker through a GUI. Or would be if I could connect like I want to.
Using
-s 192.168.100.0/24
means that you only allow access to port 3306 remotely for IP ranges matching 192.168.100.0/24, which is a private internal network. Is this really what you intend to do? Otherwise this is the problem why remote connections from other IPs doesn't work.You also don't usually need the outgoing rule.
If this doesn't help, please provide a little bit more information on where you're testing from, IPs/interfaces of the machine, what happens and perhaps a full output of
iptables -vnL
for us?Edit 1:
Based on more information it shows that the example used as template was misunderstood, you have to remove the source IP range (because you want to allow everyone remotely). Just type this, and only this, and it should work:
If you want to include your external IP it has to be the destination IP, such as:
-s
, as in source, is only used if you want to limit access from specific IP ranges.Edit 2:
Doesn't seem to be an iptables question at all, but more of a mySQL one. To allow mySQL to listen to remote connections at all, make sure to configure it to listen to your external address. Edit
/etc/mysql/my.cnf
and check for thebind-address
statement and change it to:Either you chose to replace xx.xx.xx.xx with your external IP address, or you can set it to listen to
0.0.0.0
which means it will listen to all interfaces.After that the question for you is how to setup the entire firewall for your server in the first place. Either you manually block all specific ports that no one else should be able to get to, or you have to set a default policy to reject traffic and then manually open port by port (as your initial question indicated) for services you want to allow. Be VERY careful with this though, if its a remote machine its very easy to lock yourself out if you put things the wrong order or the wrong way.
EDIT: For the instructions below if you are using Windows then there are two things to keep in mind:
1) Change
localhost
to127.0.0.1
since Windows doesn't have that set up automatically in%SystemRoot%\system32\drivers\etc\hosts
.2) You can use the little-known plink.exe command-line tool from the PuTTY suite of tools; it uses the same syntax for options as the
ssh
command so if you replacessh
in the examples below withplink.exe
it should all work.You'll want to use an SSH tunnel to forward a local port on your client to the mysql port on the server. You can do that with:
The options to ssh mean:
Using
-f -N
also means that you will have forked an ssh process into the background rather than staying connected to the remote host the way you usually would when logged into a remote shell. If you want to tear down the tunnel you can kill the ssh process on your client like this:Of course in this case
11145
is the PID of the ssh process which will be different each time you start a new ssh process to open a tunnel.Also, this assume that you do not have the mysql server also running on your client. If so you'll need to change the local port that you bind to like this:
The port
3333
is arbitrary; you can pick any free port number that your client has.The
-A
switch to iptables adds the new rules to then end of the chain. It's likely that you have an earlier rule that is denying access and with iptables the first match wins. Try using the-I
switch to insert the rule at the beginning of the chain.If that doesn't work please show us the output of
iptables -L -v -n
as an edit to your question.Your default policy for both INPUT and OUTPUT is ACCEPT. You have not defined any rules to drop or reject packets. This means your firewall is not blocking any connections.
The problem is somehere else.
Use
netstat -tan | grep LISTEN
to make sure you are running MySQL and that it is listening.If that works, there is probably another firewall somewhere that is blocking the connection.
I found an answer to my own question (I should have clarified that I was using Windows 7 locally, and linux remotely).
Anyway, the answer was to forward a port using PuTTy, as per the other answerer - but this is for Windows users.
In the PuTTy, set up an SSH connection as normal but then go to settings for tunnels, under SSH, choose an arbitrary port (I used 5555) locally to forward to 127.0.0.1:3306
Open the SSH session and login as normal.
Then in your MySQL client, set the host address to 127.0.0.1 and the port to your selected one (5555).
Hence the MySQL Client is connecting to your local machine, which in turn is being forwarded over SSH to 3306 on the remote machine and allowing the connection.
BIG NOTE: We actually had this set up using the term localhost (as you do) instead of 127.0.0.1, somewhere between PuTTy and the MySQL Client, this didn't resolve - so it seems you must use the loopback address!
Thank you so much! I was trying all sorts of crap to be able to use MySQL Workbench remotely to work on the tables in my CentOS MySQL implementation. Basically the only solution was to open up port 3306 to everybody (bad for security).
We already use Putty for secure command line access to our server and had no idea we could set up a tunnel like this. Basically we can use our existing secure SSH connection to connect MySQL Workbench with our server. Thank you ! ! !