php 如何连接到另一台服务器上的数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1763519/
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
how to connect to database on another server
提问by Ankit Sachan
Could I have my php scripts on server A and connect to the MySQL database on server B?
我可以在服务器 A 上安装我的 php 脚本并连接到服务器 B 上的 MySQL 数据库吗?
If yes, how it would be done? Thanks in advance
如果是,将如何进行?提前致谢
采纳答案by Ankit Sachan
its simple all thise above techniques are quite complicated
它的简单所有这些上面的技术都非常复杂
suppose you have database on server B and website on server A(say it has IP 192.234.12.1)
假设您在服务器 B 上有数据库,在服务器 A 上有网站(假设它有 IP 192.234.12.1)
on cpanel whitelist the IP of server B
在 cpanel 白名单服务器 B 的 IP
and create a new user having sufficient privileges in database (say this user is test)
并在数据库中创建一个具有足够权限的新用户(假设该用户是测试用户)
then create this user as [email protected]
然后将此用户创建为 [email protected]
回答by Francisco Aquino
Yes.
是的。
The same way you access the localhost on the same server, you change the database host to the external one. This is more a configuration issue, you need to grant your database user remote access to your MySQL, you also need to make sure your firewall allows connections on the MySQL port.
与访问同一服务器上的 localhost 的方式相同,将数据库主机更改为外部主机。这更多是一个配置问题,您需要授予您的数据库用户远程访问您的 MySQL,您还需要确保您的防火墙允许 MySQL 端口上的连接。
Example on Debian: http://www.debianhelp.co.uk/remotemysql.htm
Debian 示例:http: //www.debianhelp.co.uk/remotemysql.htm
回答by Rahul Mehra
Yes it can be done.
是的,它可以做到。
Find out the IP address of the server A where your scripts will be uploaded. Do not forget to change the localhost to the ip address of the Server B in mysql_connect() or mysqli_connect() method.
找出将上传脚本的服务器 A 的 IP 地址。不要忘记在 mysql_connect() 或 mysqli_connect() 方法中将 localhost 更改为 Server B 的 ip 地址。
Now go the control panel of the Server B where your Database is.
现在转到您的数据库所在的服务器 B 的控制面板。
In the control panel's Homepage go the databases section and click the Remote MYSQL option.
在控制面板的主页中,转到数据库部分,然后单击远程 MYSQL 选项。
Then add the Ip address of the Server A and click on add host.
然后添加服务器A的IP地址并点击添加主机。
Now you can access to the database in Server B while your scripts are running in Server A. Mind you the fetched result will be slow cause it is getting data from database that is located on another server.
现在您可以在您的脚本在服务器 A 中运行时访问服务器 B 中的数据库。请注意,获取的结果会很慢,因为它正在从位于另一台服务器上的数据库获取数据。
Your welcome
别客气
回答by johannes
Just don't the hostname of the other box for the connection. Details depend on the extension you're using:
只是不要连接另一个框的主机名。详细信息取决于您使用的扩展程序:
$mysql = mysql_connect($host, $user, $pass);
$mysqli = new mysqli($host, $user, $password, $schema);
$pdo = new PDO("mysql:host=$host", $user, $pass);
Make sure that the user is allowed to access by the MySQL server (CREATE USER) and check that there's no firewall in the way.
确保 MySQL 服务器 ( CREATE USER)允许用户访问,并检查是否没有防火墙。
回答by Atul Upadhyay
Its a perfect solution for connecting another database from other servers.
它是从其他服务器连接另一个数据库的完美解决方案。
$dbserverName = "191.238.0.2";
$dbUsername = "lauranco_L2L";
$dbPassword = "SL92TIW5T96L";
$dbName = "lauranco_siteBits";
回答by Harrison O
I was having similar challenges but here is what work for me: To connect to server B from server A, First, you need to allow remote MySQL access hosts in cPanel (Server B), Home -> Databases -> Remote MySQL and also whitelist the IP in the firewall (That is IP Address of B server). Then the following php db connection should work.
我遇到了类似的挑战,但这里对我有用:要从服务器 A 连接到服务器 B,首先,您需要允许 cPanel(服务器 B)中的远程 MySQL 访问主机,主页 -> 数据库 -> 远程 MySQL 以及白名单防火墙中的IP(即B服务器的IP地址)。然后下面的 php db 连接应该可以工作。
$db_connect = mysqli_connect("serverB.com", "dbuser", "dbpassword", "dbname");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}else{
//successful connection
echo "Yes, successful";
}
回答by PeterMmm
回答by AJ.
Have a look here:
看看这里:
http://us2.php.net/manual/en/function.mysql-connect.php
http://us2.php.net/manual/en/function.mysql-connect.php
You can either pass in the server hostname as an argument, or configure in php.ini.
您可以将服务器主机名作为参数传递,也可以在 php.ini 中进行配置。

