简体   繁体   中英

Website Connection to Local MySQL Server

I have recently configured a MySQL server on my Windows home computer. In addition, I have also created a website using 0fees.net-- a free hosting provider which comes with a vista panel that has various services including PHP support, FTP file-hosting, its own MySQL server etc.

For that website, I have created a "login" PHP script in order for people to login to my webpage. However, instead of reading from the MySQL database given to me on the cPanel on 0fees.net, I want the PHP logon script to read directly from the MySQL server that I have configured on my home computer. To do this, I have taken several steps:

1) Configured MySQL using the MySQL Server Instance Configuration Wizard

In that configuration, I have enabled TCP/IP Networking on port 3306 (and enabled a firewall exception for that port) and have enabled root access from remote machines.

2) On the MySQL command-line-client, I have granted remote access capabilities to other units by using:

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';

3) I have forwarded port 3306 to my router (and have used various port checkers to confirm that it is open)

4) I have created the login script called "login.php" which lies in my site's.htdocs directory and that process an HTML login form on the homepage of my website. It attempts (and fails) to connect to my local MySQL server. The segment of interest of that php script is:

    $host="my_external_ip:3306"; // External IP of my computer and port to listen on
    $username="root"; // Username specified in the GRANT command-line-client command
    $password="password"; // Password specified in the GRANT command-line-client command
    $db_name="logon"; // MySQL database name
    $tbl_name="accounts"; // Table name within the database

    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die(mysql_error());
    mysql_select_db("$db_name")or die("cannot select DB");

When attempting to connect to the MySQL sever, I get an error message that states:

Can't connect to MySQL server on 'my_external_ip' (4).

It should also be noted that I am successfully able to connect to my local MySQL server remotely from other machines which are on other internet networks (tested with Navicat) using this same external IP address on port 3306. Any ideas as to why my website cannot also connect?

Thanks in advance for any insights.

Since I can tell that your server is definitely running (and visible from the internets in general), there are a few possibilities:

  1. You forgot to run FLUSH PRIVILEGES
  2. Your username or password are wrong
  3. Your free host provider is blocking the connection

My money is on #3, there are a lot of incentives to do that.

Well I will let you in on a little trick I learned, all you got to do is go into wamp>mysql>my.ini and replace all 3306 with 3305 then save and then apply this to your firewall and port forward 3305 . Then when you are connecting remotely, simply do

mysql_connect("ip adress:3305", "username", "password");

I've tested this and it works because the web hosting sites block incoming traffic from port 3306.

@Chris mentioned that your provider might be blocking outbound requests on port 3306. Here's a quick way to check if that's the case and get around it:

  1. Configure your router to port forward from port 80 on the router to port 3306 on your box. This will allow you to connect to MySQL from outside your network on port 80 instead of port 3306.

  2. Change your PHP code to connect to your IP on port 80 instead of 3306.

Your hosting provider might block outbound port 3306, but the almost certainly wouldn't block outbound port 80 requests (lots of people using CURL or web services and such).

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