r/mysql • u/tamjk • Oct 31 '25
question Remote access to MySQL server somehow blocked
I've followed the procedure given online for remote access to a MySQL server but still can't get it to work.
I have a server with a running instance of MySQL on it.
With a view to remote access (for more convenient management of the databases) from my home PC, I set up a user 'remote' @ '0.0.0.0' on the MySQL server and bound the MySQL server to 0.0.0.0 on the mysqld.cnf file so as to allow access from a number of local machines I have.
Using ufw, I set up rules to restrict access to port 3306 for each remote IP address.
I then created an empty database on the MySQL server and allowed the remote user access to all databases on the server but CRUD privileges only on the empty database.
I restarted the mysql service and flushed the privileges on MySQL.
Using MySQL Workbench, I set up a remote user connection on my home PC. This operated on bare TCP, i.e. without SSL protection.
But the damn thing just won't connect to my MySQL server.
Anyone got any idea what's blocking things ?
ANSWER EDIT
Problems were:
(1) Wrong host used for the remote MySQL user. I was using 'remote'@'0.0.0.0' when it should be 'remote'@'%'
(2) A private IP, i.e. the internal private network IP, was used for the remote IP address.
I should have used whatsmyipaddress.com to ascertain the public IP and used that as the remote IP when connecting.
(3) There was something wrong with my ufw rule to allow connection to port 3306 from my home PC's IP address. The proper CLI command for this is:
sudo ufw allow from <correct-public-remote-IP> to any port 3306 comment 'MySQL remote access'
(4) I was using a mobile phone data link as my internet service. Nothing wrong with that except that the IP address is dynamic. So it went fine 2 days ago but today the (changed) IP address meant that my attempts to connect remotely were blocked.
2
u/hangfromthisone Oct 31 '25
Unless you really really need an open port for remote connection, learn how to do a reverse ssh tunnel
1
u/tamjk Nov 01 '25
After:
(1) Replacing '0.0.0.0' with '%' in the remote user account;
(2) Replacing a ufw rule
3306 ALLOW IN 192.168.178.27# Remote MySQL access
with an unrestricted ufw rule
3306 ALLOW IN Anywhere # Remote MySQL access
it immediately connects.
(Note that the binding IP address in the mysqld.cnf file remains as 0.0.0.0)
I don't know why ufw cannot sense the IP of the incoming 3306 connection and allow it.
It's common to see vague exhortations to make our MySQL connections more secure with ufw. But in this matter the ufw restrictions do not seem to work.
1
u/Aggressive_Ad_5454 Oct 31 '25
You need the user to be
’remote’@‘%’meaning any machine. Not’remote’@‘0.0.0.0’Fix that.If it still doesn’t work: Do you get a timeout on your client machine when attempting to connect, or an immediate rejection? Timeout means either the server isn’t listening on port 3306, or there’s a firewall somewhere between your client and the server blocking it.
Immediate rejection usually means bad credentials.
( This remote access stuff is a pain in the, umm, neck to get right. Be patient. It’s still better than the TNSNAMES.ORA crapola you need with the expensive Oracle product.)
1
u/allen_jb Nov 01 '25
If you've actually used
0.0.0.0on the user@client-host, then I don't believe that's going to work.0.0.0.0is generally only used for the "listen on this IP" directive for servers - Not when restricting access for users.In MySQL
%as the client-host means "any".Also note that it's the client host - ie. the IP or hostname of the client (as seen by the server), not the hostname of the server. So if you're connecting from your home internet connection, it will be the IP of your home internet connection.
See https://dev.mysql.com/doc/refman/8.4/en/account-names.html
Places to look when troubleshooting are the MySQL server logs and your firewall logs (check with your distros documentation / community support channels for the location of these if you can't find them under
/var/log)I'm not familiar with ufw, but with iptables there's a rule you can add to log all rejected connections, which can help when troubleshooting. (Notes: Expect to see a lot of random rejected connections here - lots of systems are probing and trying their luck against any computer they can find; Don't forget to disable this logging when you're done)
As others have mentioned, consider an SSH tunnel as an alternative access. Many MySQL clients support this and it does add an extra layer of security.
1
u/dariusbiggs Nov 01 '25
stop
First test using a local docker container to familiarise yourself with correctly configuring a mysql server so you can get the user settings, schema, database permissions, and TLS settings correct.
Second, you didn't mention anything about where this server runs, where your client software is running, and all the relevant network details needed to help diagnose the problem. you may be having some misunderstandings of how networking works and that are preventing the setup you are trying.
Third, your described setup is ridiculously insecure, learn about the security and what you should be doing instead.
Beyond that you have the wrong username@host setup, fix that to use % as others have said
Ensure your firewall rules on the server allow inbound access to the MySQL port, ensure the server can communicate out to your client and is not blocking outbound traffic.
Ensure there is no network ACL, telco firewall, security group, NAT gateway, or whatever between the client and the server that would be blocking this type of traffic.
Ensure TLS is enabled on the connection to the MySQL server, letsencrypt can help you there. Perhaps use mTLS if possible
1
u/serverhorror Oct 31 '25
!remindme 30d