使用 PHP 连接到远程 MySQL 服务器
我正在尝试使用以下代码从本地计算机虚拟主机连接到远程 MySQL 服务器:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
我的问题是我无法在本地连接,收到错误:
无法连接到“xxx.xxx.xxx.xxx”上的 MySQL 服务器 (10060)
当我将相同的 PHP 文件上传到服务器时,情况并非如此。我可以毫无问题地查询数据库。
我也无法通过命令行连接,但我可以访问 cPanel,这排除了我的 IP 被意外禁止的可能性。
我的本地服务器运行 PHP 5.2.9,远程服务器运行 5.2.12
I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
My problem is that I am unable to connect locally, receiving the error:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
My local server is running PHP 5.2.9, the remote server 5.2.12
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
%
(任何主机)连接的用户(请参阅 manual 了解详细信息)当前问题是第一个问题,但在您解决它之后您可能会得到第二个。
%
(any host) (see manual for details)The current problem is the first one, but right after you resolve it you will likely get the second one.
使用PHP连接远程MySQL服务器非常容易,您所要做的就是:
在远程服务器中创建一个MySQL用户。
向用户授予完全权限。
使用 PHP 代码连接到服务器(下面给出示例)
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
我刚刚解决了这样的问题。
我了解到的是:
my.cnf
并在[ 下设置
bind-address = your.mysql.server.address
mysqld]检查它是否正在运行
创建一个以 % 作为域的用户(usr 或其他任何内容)并授予她对相关数据库的访问权限。
打开端口 3306 的防火墙(您可以使用 iptables。确保为每个人打开端口,或者如果您处于严格的安全状态,则只允许客户端地址)
您现在应该能够从客户端服务器 php 脚本连接 mysql 服务器。
I just solved this kind of a problem.
What I've learned is:
my.cnf
and set thebind-address = your.mysql.server.address
under[mysqld]
check if it's running
create a user (usr or anything) with % as domain and grant her access to the database in question.
open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)
you should be able to now connect mysql server form your client server php script.
这可能不是帖子问题的答案。但这可能对和我面临同样情况的人有所帮助:
客户端有两张网卡,一张无线网卡和一张普通网卡。
ping 服务器可以成功。但是
telnet serverAddress 3306
会失败。并且会抱怨
当尝试连接到服务器时, 我禁止了正常的网络适配器。
并尝试了
telnet serverAddress 3306
它可以工作。然后当连接到MySQL服务器时它可以工作。This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:
The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However
telnet serverAddress 3306
would fail.And would complain
when try to connect to server.So I forbidden the normal network adapters.
And tried
telnet serverAddress 3306
it works.And then it work when connect to MySQL server.