php 致命错误:PDOException SQLSTATE[28000] [1045] 拒绝访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4896264/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:12:00  来源:igfitidea点击:

Fatal error: PDOException SQLSTATE[28000] [1045] Access denied

phppdo

提问by udexter

I'm trying to install a PHP application that was coded by another guy that I can't contact and he used PDO.

我正在尝试安装由另一个我无法联系的人编写的 PHP 应用程序,他使用了 PDO。

I'm getting a lot of problems to run this code. In some areas, MySQL connects well because PDO is not used (this shows that the username and password are right), but in others it throws this exception:

我在运行这段代码时遇到了很多问题。在某些区域,MySQL 连接良好,因为没有使用 PDO(这表明用户名和密码是正确的),但在其他区域它会抛出此异常:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost' (using password: NO)' in /var/www/x/include/SPDO.php:14 Stack trace: #0 /var/www/x/include/SPDO.php(14): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 /var/www/x/include/SPDO.php(22): SPDO->__construct() #2 /var/www/x/include/class_Projecte.php(55): SPDO::singleton() #3 /var/www/x/dades_proj_edit.php(116): Project->__construct() #4 {main} thrown in /var/www/x/include/SPDO.php on line 14

致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[28000] [1045] 用户 'www-data'@'localhost'(使用密码:NO)'在 /var/www/x/include/SPDO 中被拒绝访问。 php:14 堆栈跟踪:#0 /var/www/x/include/SPDO.php(14): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 /var/ www/x/include/SPDO.php(22): SPDO->__construct() #2 /var/www/x/include/class_Projecte.php(55): SPDO::singleton() #3 /var/www/ x/dades_proj_edit.php(116): Project->__construct() #4 {main} 在第 14 行的 /var/www/x/include/SPDO.php 中抛出

I don't understand why is trying to look the user www-data. I looked in Google and found a lot of problems with this error, but from a CMS like Magentoand nothing that can help me.

我不明白为什么要查看用户www-data。我查看了谷歌,发现这个错误有很多问题,但是来自像Magento这样的 CMS ,没有什么可以帮助我。

  • PHP version: PHP 5.3.3-7
  • PDO Driver for MySQL 5.1.49
  • Apache version: Apache/2.2.16 (Debian)
  • Apache modules: core mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_php5 mod_reqtimeout mod_setenvif mod_status
  • PHP 版本:PHP 5.3.3-7
  • MySQL 5.1.49 的 PDO 驱动程序
  • Apache 版本:Apache/2.2.16 (Debian)
  • Apache 模块:核心 mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_deflate mod_dir mod_env mod_mime modtime_negotiation

Update:

更新:

This are the two files used to connect to PDO:

这是用于连接到 PDO 的两个文件:

  • SPDO.php:http://pastebin.com/utCjresv
  • class_Config.php:http://pastebin.com/6CENLDf3

Start of SPDO.php:

SPDO.php 的开始:

<?php
class SPDO extends PDO
{
        private static $instance = null;
        private static $dbhost="localhost";

回答by James

www-data is the user under which your PHP scripts are being executed. I imagine it is what PDO will use by default if no username is provided for connecting to your database.

www-data 是执行 PHP 脚本的用户。我想如果没有提供用户名来连接到您的数据库,PDO 将默认使用它。

It seems that code you have been provided either cannot find the username and password from whatever settings method you use or the developer has not supplied a username and password in the call to PDO correctly.

您提供的代码似乎无法从您使用的任何设置方法中找到用户名和密码,或者开发人员没有在对 PDO 的调用中正确提供用户名和密码。

A temporary fix would be to edit the file "/var/www/x/include/SPDO.php". On line 14 you will find a call to the PDO constructor. Change the second parameter to your username and the third parameter to your password.

临时修复是编辑文件“/var/www/x/include/SPDO.php”。在第 14 行,您将找到对 PDO 构造函数的调用。将第二个参数更改为您的用户名,将第三个参数更改为您的密码。

回答by Distdev

/var/www/x/include/SPDO.php(14): PDO->_construct('mysql:host=;dbn...', NULL, NULL)

/var/www/x/include/SPDO.php(14): PDO->_construct('mysql:host=;dbn...', NULL, NULL)

The second and third arguments should be username and password for the database instead of NULLs.

第二个和第三个参数应该是数据库的用户名和密码,而不是 NULL。

I've checked your files and I've found that instantiation of the configuration is missed (or not included) - username, password, host, etc.

我检查了您的文件,发现配置的实例化丢失(或不包括) - 用户名、密码、主机等。

You should use the following code before instantiating of a PDO object:

在实例化 PDO 对象之前,您应该使用以下代码:

$config = Config::singleton();
$config->set('dbhost', 'your_hostname'); // Probably localhost
$config->set('dbname', 'your_database_name');
$config->set('dbuser', 'db_username');
$config->set('dbpass', 'db_user_password');