使用 PHP 连接到远程 MySQL 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1935314/
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
Connecting to remote MySQL server using PHP
提问by BenTheDesigner
I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
我正在尝试使用以下代码从我的本地机器虚拟主机连接到远程 MySQL 服务器:
$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)
无法连接到“xxx.xxx.xxx.xxx”上的 MySQL 服务器 (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.
当我将相同的 PHP 文件上传到服务器时,情况并非如此。我可以毫无问题地查询数据库。
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.
我也无法通过命令行连接,但我可以访问 cPanel,这排除了我的 IP 被意外禁止的可能性。
My local server is running PHP 5.2.9, the remote server 5.2.12
我的本地服务器运行 PHP 5.2.9,远程服务器 5.2.12
回答by Bozho
- firewall of the server must be set-up to enable incomming connections on port 3306
- you must have a user in MySQL who is allowed to connect from
%(any host) (see manualfor details)
- 必须设置服务器的防火墙以启用端口 3306 上的传入连接
- 您必须在 MySQL 中有一个用户可以从
%(任何主机)进行连接(有关详细信息,请参阅手册)
The current problem is the first one, but right after you resolve it you will likely get the second one.
当前的问题是第一个,但在您解决它之后,您可能会遇到第二个。
回答by Ajit Kumar KV
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
使用 PHP 连接远程 MySQL 服务器非常容易,您需要做的是:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
在远程服务器上创建一个 MySQL 用户。
授予用户完全权限。
使用 PHP 代码连接到服务器(示例如下)
$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
回答by Debmalya Sinha
I just solved this kind of a problem. What I've learned is:
我刚刚解决了这种问题。我学到的是:
- you'll have to edit the
my.cnfand set thebind-address = your.mysql.server.addressunder[mysqld] - comment out skip-networking field
- restart mysqld
check if it's running
mysql -u root -h your.mysql.server.address –pcreate a user (usr or anything) with % as domain and grant her access to the database in question.
mysql> CREATE USER 'usr'@'%' IDENTIFIED BY 'some_pass'; mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'@'%' WITH GRANT OPTION;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)
- restart firewall/iptables
- 你必须编辑
my.cnf并设置bind-address = your.mysql.server.address下[mysqld] - 注释掉跳过网络字段
- 重启mysqld
检查它是否正在运行
mysql -u root -h your.mysql.server.address –p创建一个以 % 作为域的用户(usr 或任何东西),并授予她访问相关数据库的权限。
mysql> CREATE USER 'usr'@'%' IDENTIFIED BY 'some_pass'; mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'@'%' WITH GRANT OPTION;为端口 3306 打开防火墙(您可以使用 iptables。确保为任何人打开端口,或者如果您处于严密的安全状态,则只允许客户端地址)
- 重启防火墙/iptables
you should be able to now connect mysql server form your client server php script.
您现在应该能够从您的客户端服务器 php 脚本连接 mysql 服务器。
回答by Shihe Zhang
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 3306would fail.
And would complain
客户端有两张网卡,一张是无线网卡,一张是普通网卡。ping 到服务器可以成功,但是telnet serverAddress 3306会失败。并且会抱怨
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
无法连接到“xxx.xxx.xxx.xxx”上的 MySQL 服务器 (10060)
when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306it works.And then it work when connect to MySQL server.
当尝试连接到服务器时。所以我禁止正常的网络适配器。并尝试telnet serverAddress 3306它有效。然后它在连接到 MySQL 服务器时工作。

