windows 网站连接到本地 MySQL 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6349688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Website Connection to Local MySQL Server
提问by user797967
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.
我最近在我的 Windows 家用计算机上配置了一个 MySQL 服务器。此外,我还使用 0fees.net 创建了一个网站——一个免费的托管服务提供商,它带有一个 vista 面板,提供各种服务,包括 PHP 支持、FTP 文件托管、它自己的 MySQL 服务器等。
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:
对于该网站,我创建了一个“登录”PHP 脚本,以便人们登录我的网页。但是,我希望 PHP 登录脚本直接从我在家用计算机上配置的 MySQL 服务器读取,而不是从 0fees.net 上的 cPanel 上提供给我的 MySQL 数据库中读取。为此,我采取了几个步骤:
1) Configured MySQL using the MySQL Server Instance Configuration Wizard
1) 使用 MySQL 服务器实例配置向导配置 MySQL
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.
在该配置中,我在端口 3306 上启用了 TCP/IP 网络(并为该端口启用了防火墙例外)并启用了远程机器的 root 访问。
2) On the MySQL command-line-client, I have granted remote access capabilities to other units by using:
2) 在 MySQL 命令行客户端上,我已通过使用以下命令授予其他单元的远程访问功能:
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)
3)我已经将端口 3306 转发到我的路由器(并使用了各种端口检查器来确认它是开放的)
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:
4) 我创建了一个名为“login.php”的登录脚本,它位于我网站的 .htdocs 目录中,它处理我网站主页上的 HTML 登录表单。它尝试(但失败)连接到我的本地 MySQL 服务器。该 php 脚本感兴趣的部分是:
$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:
尝试连接到 MySQL 服务器时,我收到一条错误消息,指出:
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?
还应该注意的是,我能够使用端口 3306 上的相同外部 IP 地址从其他互联网网络上的其他机器(使用 Navicat 测试)成功地远程连接到我的本地 MySQL 服务器。有关为什么我的网站无法连接的任何想法还连接?
Thanks in advance for any insights.
提前感谢您的任何见解。
回答by Chris Eberle
Since I can tell that your server is definitely running (and visible from the internets in general), there are a few possibilities:
由于我可以确定您的服务器肯定正在运行(并且通常可以从 Internet 上看到),因此有几种可能性:
- You forgot to run
FLUSH PRIVILEGES
- Your username or password are wrong
- Your free host provider is blocking the connection
- 你忘了跑
FLUSH PRIVILEGES
- 您的用户名或密码错误
- 您的免费主机提供商阻止了连接
My money is on #3, there are a lot of incentives to do that.
我的钱在 #3 上,有很多激励措施可以做到这一点。
回答by Eric Petroelje
@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:
@Chris 提到您的提供商可能会阻止端口 3306 上的出站请求。这是检查是否是这种情况并绕过它的快速方法:
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.
Change your PHP code to connect to your IP on port 80 instead of 3306.
将您的路由器配置为从路由器上的端口 80 转发到您机器上的端口 3306。这将允许您通过端口 80 而不是端口 3306 从网络外部连接到 MySQL。
更改您的 PHP 代码以通过端口 80 而不是 3306 连接到您的 IP。
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).
您的托管服务提供商可能会阻止出站端口 3306,但几乎可以肯定不会阻止出站端口 80 请求(很多人使用 CURL 或 Web 服务等)。
回答by mark
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
好吧,我会让你了解我学到的一个小技巧,你所要做的就是进入wamp>mysql>my.ini
并替换所有3306
,3305
然后保存,然后将其应用到防火墙和端口转发3305
。然后当您远程连接时,只需执行
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.
我已经测试过它并且它有效,因为网络托管站点阻止来自端口 3306 的传入流量。