-1

I have a mySQL database hosted locally using WAMP, I went through all the procedures to access it remotely and seem to have succeeded in that I can put the IP address into my phone, see WAMP and access phpmyAdmin etc.

My issue is that I want to test the database connection using the script below from my website (ultimately I want to use a WP plugin to get info off the DB and then close it).

However I am not fully sure what the parameters would be.

Apache server is on port 80 and mysql is on port 3306. I have setup port forwarding on those ports and tested them using http://www.canyouseeme.org/

Therefore would my host be myIP (using What is myIP) : 80 or 3306.

I have tried both and they fail. Would my user name be root or Root@Host

Like I say ultimately I want to test this, connect to the database and then use the parameters in a WordPress plugin to copy data from my locally hosted DB to my site's db.

Please advise.

Thank you.

<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}
Simon Vorwerk
  • 11
  • 1
  • 3
  • 1
    Why are you using the long-deprecated `mysql_` code library? It was discontinued many years ago and removed entirely in PHP7. No new code should be written using this library. It leaves you potentially vulnerable to SQL injection attacks (due to the lack of parameterised query support) and potentially other unpatched vulnerabilities. Switch to using `mysqli` or `PDO` as soon as possible, and then learn how to write parameterised queries to protect your data from malicious input. See http://bobby-tables.com for a simple explanation of the risks and some sample PHP code to write queries safely. – ADyson Sep 11 '18 at 12:42
  • Anyway, that's a separate issue. Regarding the question...can you be a bit clearer? The code is running on your hosted webserver and wants to connect to your local database via port forwaring, is that right? And what exact error do you get when your connection attempt fails? calling mysql_error() after the attempt fails will tell you. Have you configured mysql itself (and the relevant user account) to listen for remote connections as well as setting up the port forwarding? – ADyson Sep 11 '18 at 12:45
  • P.S. If you want to connect to mysql then you use port 3306, no matter where you're connecting from. Port 80 is for HTTP traffic. Username would just be root (although personally I wouldn't allow anyone the chance to connect as root remotely, instead create a new account which has the privileges required for this task and is configured for remote access). – ADyson Sep 11 '18 at 12:45
  • note that you have to configure mysql to accept connections from different ip. Usually it is configured to allow only local connections – Lelio Faieta Sep 11 '18 at 12:49
  • Thank you all. In phpMyAdmin I have set root host to all "%". – Simon Vorwerk Sep 11 '18 at 12:53
  • Sorry was not finished commenting... The above script is copied from the web, I don't know much about the deprecation issues. – Simon Vorwerk Sep 11 '18 at 12:54
  • I have set the port to 3306 and the error I get is "Unable to Connect to '102.182.226.157:3306'" – Simon Vorwerk Sep 11 '18 at 12:54
  • would the dbname just be my dbname - not need to add @ or .sql or anything? – Simon Vorwerk Sep 11 '18 at 12:56
  • Essentially what I want to do is this - use a wordpress plugin that expects database parameters from another site - in this case the parameters would be where my local database is stored. So I am trying to make sure the parameters are correct so that the plugin will work. – Simon Vorwerk Sep 11 '18 at 13:05
  • " the error I get is "Unable to Connect to '102.182.226.157:3306'""....that's just the error your PHP is making up. "Unable to Connect to" is a completely useless message. As you can see, it's hard-coded by the PHP script and doesn't convey any actual information about the problem. As I said previously, use mysql_error() to tell you what's actually happening. Change your code line to `$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost' - Error: ".mysql_error());` and see if you get better info. – ADyson Sep 11 '18 at 16:33
  • Also, if you want to check if it's port forwarding which is the problem, try connecting to your mysql server from another machine within your network (e.g. using MySQL Workbench, or by running this PHP script from there) - that should tell you if remote connections are allowed by the mysql server itself or not. Obviously you'll have to use the local network IP (e.g. 192.168.0.2 or something like that) rather than the internet-facing IP. – ADyson Sep 11 '18 at 16:34
  • P.S. Re the deprecation issues, please read http://www.php.net/manual/en/function.mysql-connect.php. You should **not** be using this particular extension really. Once you've got your connection working, if you're going to re-use this code then I strongly urge you to re-write this code using a modern library. Whoever left that ancient script online for you to find is doing you a disservice, they should take it down really. – ADyson Sep 11 '18 at 16:35
  • Thank you very much, I will check out your suggestions. – Simon Vorwerk Sep 11 '18 at 16:47

1 Answers1

-2

Make sure your router is allowing the port forwarding. If the (ip address) xxx.xxx.xxx.xxx:80 is not showing anything then, ports are not being forwarded properly. Here are some check you could make to see where the problem lies:

See if it's working on the local machine at 127.0.0.1 (most likely it will work in your situation).

Check on other devices on your network such as using your smartphone go to your machines local IP address and see if that works.

If both of the above work, then you need to make sure your router is port forwarding the correct IP Address and Ports.

ByWaleed
  • 395
  • 4
  • 18
  • I have tested my port forwarding using http://www.canyouseeme.org/ with success so I assume port forwarding has worked. Also in my router config the port forwards to the computer where my DB is hosted. – Simon Vorwerk Sep 11 '18 at 12:58