简体   繁体   中英

php connecting to mysql on localhost, request from remote

I have a wamp server running on my local machine that is exposed to the outside world. I have several php scripts that allow an android app to access information contained in a mySQL database running on the same machine as the WAMP server. When tested from inside my local network everything works fine. However when I try to run the php scripts from outside my local network there appears to be an error connecting to mySQL.

connect.php

<?php
header('Access-Control-Allow-Origin: *'); 

/*** mysql hostname ***/
$hostname = '127.0.0.1';

/*** mysql username ***/
$username = 'chaosjr_user';

/*** mysql password ***/
$password = 'pass';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=chaos_jr", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

I'm almost certain it has something to do with the fact I'm referencing localhost from the outside world, but when I replace 127.0.0.1 with the IP address on my internal network or the external IP address it doesn't work from anywhere. Any suggestions?

EDIT: Just to clarify, I can get to my php scripts from the outside (I do have a static IP address) and get a response from them as long as that particular script does't have to query MySQL. For example I can connect to XXX.XXX.XXX.XXX/myPHPscript.php just fine and everything looks good. But menus that are supposed to populate with SQL data are empty.

You are correct - the address 127.0.0.1 is your local IP address and not the one you would connect with from the outside world.

If you have a static IP address, you'll need to use that. You may find things tricky if you don't have a fixed IP address, or if you have a firewall that blocks the port you are using.

You should still use 127.0.0.1 to connect to the DB since the PHP script and the DB runs in the same server as far as I understood. It doesn't care if your script is called from the outside, if the script and the DB are in the same server, 127.0.0.1 is the correct address.

If you do want your database to be available from outside your network (please be aware that your machine will need to be powered on at all times in order for the database to be available), you will need to configure a so called port forward on your router to your internal address. Then in your app, make sure to use your external IP address, your router will then reroute the request to your machine.

Also be aware that your MySQL will need to accept external connections, by default it binds to your localhost and will not listen to remote connections. This is because localhost is bound to a special loopback interface of your OS and is not actually bound to the network card in your computer. You will need to bind MySQL to the IP address that is bound to the network card, so it can communicate to the outside world.

In other words, what you want is possible, but takes many different steps to work, mainly network/access related and how to configure this depends on your setup. So there's no ready-to-go answer for this, it going to take some trial and error. But basically it comes down to these steps:

  1. Configure a port forward for your MySQL port (default: tcp 3306) on your router.
  2. Make sure that your MySQL server is bound to your "external" (in most LAN's this is a 192.168.0.x address) IP address, so not to localhost or 127.0.0.1 (this is a setting in the my.ini configuration file).
  3. Make sure your app attempts a database on your remote IP address (the one you got from your ISP, so not your 127.0.0.1 or 192.168.0.x address!).

For the last step, it's also important that your ISP has given you a static IP adress. Many ISP's use DHCP adresses, meaning every time your modem resets, your IP address can be different when it boots up again. You can verify this with your ISP. Always remember that hosting stuff in-house is usually not ideal, it's OK for micro-projects or startup projects, but consider off-site hosting if things get serious.

In your PHP code it should stay 127.0.0.1 because the PHP and the MySQL run on the same server, which is your localhost at that IP. But contacting it is a lot harder.

Okay, so your local host is running on your computer right, so this can be reached by contacting 127.0.0.1 from that same computer only.

If you try to contact it from another device, you have to look up the private IP address of the device that you want to contact. This can be something like 192.168.xx in most cases.

Now contacting this computer from outside your network is even harder because you will connect to your router first (via its public IP), and that router should forward you to your pc on a certain port (probably port 80). You have to make sure that you configured your router so it can forward to the correct computer in your network and to the correct port on that computer.

But wait, there's more! Most ISPs (internet service providers) change your public IP address every few days for security reasons, making it even harder to maintain the right reference to this computer you're trying to reach.

You can find your WAN IP (public IP) by visiting http://www.whatismyip.com and check your local IP by executing "ifconfig" in the shell, or "ipconfig" in the windows command prompt

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