简体   繁体   English

MySQL取消绑定在php上失败,但在phpmyadmin中失败

[英]MySQL aunthentication fails on php but not in phpmyadmin

I am developing a PHP website with MySQL access. 我正在开发具有MySQL访问权限的PHP网站。 My hosting site is shared and I created a DB from the phpmyadmin that is installed on the shared hosting server. 我的托管站点是共享的,我从安装在共享托管服务器上的phpmyadmin创建了一个数据库。 I created 2 users, one with administrative privileges, the other having only read/write privileges. 我创建了2个用户,一个具有管理特权,另一个仅具有读/写特权。 I can connect with phpmyadmin using both accounts successfully but when I tried to access mysql using the same accounts and using the same credentials, php gives me a: 我可以使用两个帐户成功地与phpmyadmin连接,但是当我尝试使用相同的帐户和相同的凭据访问mysql时,php给了我一个:

Database connection failed: Access denied for user 'myusername'@'localhost' (using password: YES)

Which is very weird. 这很奇怪。 PHPMyAdmin says that the server is localhost so that means I am using the correct server parameter to my mysql_connect function in php. PHPMyAdmin表示服务器是本地主机,因此这意味着我在php中的mysql_connect函数中使用了正确的服务器参数。

Does anyone have any idea what went wrong? 有谁知道出什么问题了吗? Could it be possible that mysql is only enabled for PHPMyAdmin (which is absurd)? 是否有可能仅对PHPMyAdmin启用mysql(这很荒谬)?

Thank you very much and I'll appreciate the help. 非常感谢您,我将不胜感激。 :) :)

Here is a sample code (username and password are replaced for security purpose): 这是示例代码(出于安全考虑,已替换用户名和密码):

defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "host_admin");
defined('DB_PASS')   ? null : define("DB_PASS", "mysuperpassword");
defined('DB_NAME')   ? null : define("DB_NAME", "mydb_inhostingserver");

...

$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);

if (!$this->connection) {
    die("Database connection failed: " . mysql_error());
} else {
    $db_select = mysql_select_db(DB_NAME, $this->connection);

    if (!$db_select) {
    die("Database selection failed: " . mysql_error());
    }
}

... ...

Database connection failed: Access denied for user 'host_admin'@'localhost' (using password: YES)

I also echoed the 4 constants and their values are correct! 我还回显了4个常量,它们的值正确! I'm really lost here... 我真的在这里迷路了...

Additional Information: 附加信息:

  • The FTP address is: ftp.thewebsiteimtalkingabout.com FTP地址是:ftp.thewebsiteimtalkingabout.com
  • The PhpMyAdmin address is: mysql.thewebsiteimtalkingabout.com PhpMyAdmin地址为:mysql.thewebsiteimtalkingabout.com

So would this mean that they my code and MySQL's phpmnyadmin is running on the same host? 那么这是否意味着他们我的代码和MySQL的phpmnyadmin在同一主机上运行?

Answer: 回答:

Ok so I was testing the theory that they are in a different server although their domain's are the same. 好的,所以我在测试理论,尽管它们的域相同,但它们位于不同的服务器中。 I extracted their respective ipaddress and found that they are different. 我提取了它们各自的ipaddress,发现它们是不同的。 I then replaced the 'localhost' parameter to the ipaddress of where the phpmyadmin resides then boom! 然后,我将'localhost'参数替换为phpmyadmin所在的ipaddress,然后开始运行! It's now connected. 现在已连接。 Thank you very much for the replies everyone! 非常感谢大家的答复!

Eepot - that doesnt mean that your application sits on the same server as the mySQL database. Eepot-这并不意味着您的应用程序与mySQL数据库位于同一服务器上。

For instance, on my host, the database it on another server - but phpmyadmin is ALSO on THAT server. 例如,在我的主机上,将其数据库存储在另一台服务器上-但phpmyadmin也在该服务器上。 This means that "phpmyadmin" is reporting the server as "localhost" - but my application needs to connect to the server using the full server name. 这意味着“ phpmyadmin”将服务器报告为“ localhost”,但是我的应用程序需要使用完整的服务器名称连接到服务器。

You need to ask your hosting provider for the details to connect to your mySQL server. 您需要向托管服务提供商询问有关连接到mySQL服务器的详细信息。

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

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