简体   繁体   English

php访问远程数据库

[英]php access to remote database

Help! 救命!

I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to an MySql database HOST2. 我在HOST1上有一个PHP(PHP 5.2.5)脚本,试图连接到MySql数据库HOST2。 Both hosts are in Shared Host environments controlled through CPanel. 两台主机都在通过CPanel控制的共享主机环境中。

HOST2 is set to allow remote database connections from HOST1. HOST2设置为允许来自HOST1的远程数据库连接。

The PHP connect I'm using is:- $h2 = IPADDRESS; 我正在使用的PHP连接是:-$ h2 = IPADDRESS; $dbu = DBUSER; $ dbu = DBUSER; $dbp = DBPASS; $ dbp = DBPASS;

$DBlink = mysql_connect($h2, $dbu, $dbp);

This always fails with:- 这总是失败:

Access denied for user '<dbusername>'@'***SOMESTRING***' (using password: YES)

nb: SOMESTRING looks like it could be something to do with the shared host environment. nb: SOMESTRING看起来可能与共享主机环境有关。

Any ideas??? 有任何想法吗???

BTW: I can make remote connections to HOST2 from my laptop using OpenOffice via ODBC, and SQLyog. 顺便说一句:我可以通过ODBC和SQLyog使用OpenOffice从笔记本电脑建立到HOST2的远程连接。 The SQLyog and ODBC settings are exactly the same as the PHP script is trying to use. SQLyog和ODBC设置与PHP脚本尝试使用的设置完全相同。

somestring is probably the reverse-lookup for your web-server. somestring可能是您的Web服务器的反向查找。

Can you modify privileges from your cPanel? 您可以从cPanel修改权限吗? Have you done anything to allow access from your workstation (ODBC)? 您是否做了任何允许从工作站(ODBC)访问的操作?

The error-message seems to indicate that you have network-access to the mysql-server, but not privileges for your username from that specific host. 错误消息似乎表明您可以通过网络访问mysql服务器,但没有来自该特定主机的用户名特权。

If you're allowed to grant privileges for your database, invoking: 如果允许您授予数据库特权,请调用:

GRANT SELECT ON database.* TO username@ip.address.of.host1 IDENTIFIED BY 'password' GRANT SELECT ON database。* TO username@ip.address.of.host1由'password'标识

might work for you. 可能为您工作。 I just wrote this out of my head, you might want to doublecheck the syntax in mysql-docs. 我只是在脑海里写下来,您可能想仔细检查mysql-docs中的语法。

  • Have you read the MySQL documentation on Causes of Access denied Errors ? 您是否已阅读有关访问原因拒绝错误的MySQL文档?

  • Have you contacted support for your hosting provider? 您是否已联系托管服务提供商的支持? They should have access to troubleshoot the database connection. 他们应该有权对数据库连接进行故障排除。 People on the internet do not have access. 互联网上的人无法访问。

  • Do you need to specify the database name? 您需要指定数据库名称吗? Your account might have access to connect only to a specific database. 您的帐户可能仅具有连接到特定数据库的权限。 The mysql_connect() function does not allow you do specify the database, but new mysqli() does. mysql_connect()函数不允许您指定数据库,但是new mysqli()可以。 I'm not sure if this is relevant -- it might allow you to connect but give you errors when you try to query tables that aren't in your database. 我不确定这是否相关-可能会允许您连接,但在尝试查询数据库中未包含的表时会出现错误。

  • Are you sure you're using the right password? 您确定使用正确的密码吗? MySQL allows each account to have a different password per client host. MySQL允许每个帐户在每个客户端主机上使用不同的密码。 Admittedly, this is not a common configuration, but it's possible. 诚然,这不是常见的配置,但有可能。 Your hosting provider should be able to tell you. 您的托管服务提供商应该能够告诉您。

Just some ideas: 只是一些想法:

  • HOST1 does not have remote access to HOST2 (shared host is disallowing) HOST1没有对HOST2的远程访问(不允许共享主机)
  • MySQL account does not have access from HOST1 (IP address specified on account creation, or wildcard) MySQL帐户无法访问HOST1(在帐户创建或通配符指定的IP地址)

Edit: 编辑:

In response to your comment, I meant that HOST1 cannot get to the MySQL port on HOST2. 回应您的评论,我的意思是HOST1无法访问HOST2上的MySQL端口。 Web services will work, of course, because port 80 is open to the public. 当然,Web服务将起作用,因为端口80向公众开放。 As another user pointed out though, you are getting a response, so you are reaching it. 但是,正如另一位用户指出的那样,您正在获得响应,因此您正在获得响应。 I would try specifying the DB, and double checking the account creation command you ran. 我将尝试指定数据库,并仔细检查您运行的帐户创建命令。

For the second piece, I meant this: http://dev.mysql.com/doc/refman/5.0/en/adding-users.html 对于第二部分,我的意思是: http : //dev.mysql.com/doc/refman/5.0/en/adding-users.html

You can specify what host the username can connect from. 您可以指定用户名可以连接的主机。 If it isn't set to HOST2's IP or the wildcard, HOST2 can't log in with those credentials. 如果未将其设置为HOST2的IP或通配符,则HOST2无法使用这些凭据登录。

您应该尝试使用主机名和端口,例如$ h2 = IPADDRESS:3307;。

The error message means that you can contact the mySql server, but the user you are trying to log in as, does not have access. 该错误消息表示您可以联系mySql服务器,但是您尝试登录的用户没有访问权限。

Either the user does not have access at all, or it has access locally, but not from the host you are connecting from. 用户根本没有访问权限,或者该用户只能在本地访问,但不能从您所连接的主机访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM