简体   繁体   中英

Connect to MySQL database over public ip address?

I'm using Xampp for this and have been able to get it to connect from my phone to the host using the hosts static ip 192.168.0.x but when I try to connect to the database though my public ip address it fails.

http://192.168.0.x/testdatabase/ works but http://xx.xx.xxx.xx/testdatabase/ (my public ip) doesn't, I have enabled port forwarding on port 3306 to the hosts static ip but it still fails and I have even tried adding :3306 to the end of the ip.

How can I connect to the sever through my public ip address?

I'd probably use SSH Tunneling.

Tunnel to your server using SSH, then with your local client, connect to localhost on whatever port you've set your tunnel to for MySQL.

On Windows you can use putty to do that. Or ssh on MacOS / Linux.

I won't make a full tuto on how to setup an SSH tunnel but you'll find tons of sources on the web.

The problem is not PHP, the problem is that you didnt specify the rights for a user to connect from another IP.

You grant user specific rights in the rightsmanagement. If you want, lets say, from 10.10.10.10 to your DB as user "foo", you have to allow it:

GRANT ALL ON mydatabase.* TO bar@'10.10.10.10' IDENTIFIED BY 'randompassword';

Otherwise MySQL will block the access to your DB.

You also could allow EVERY IP:

GRANT ALL ON mydatabase.* TO bar@* IDENTIFIED BY 'randompassword';

Sidenote: It is NOT good to allow access from other than internal network (or even localhost). Exposing your Server to the world wont do any good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM