Здравствуйте. Не получается PDO подключение к базе данных mysql. Пароль и логин верные 100%. Пробовал убирать пароль и создавать нового пользователя, не помогло, как мне сказали, проблема с правами пользователя. Надеюсь поможете решить проблему. Текст ошибки: Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) in C:\OpenServer\domains\test\index.php:21 Stack trace: #0 C:\OpenServer\domains\test\index.php(21): PDO->__construct() #1 C:\OpenServer\domains\test\index.php(9): Database->connect() #2 C:\OpenServer\domains\test\index.php(49): Database->__construct() #3 {main} thrown in C:\OpenServer\domains\test\index.php on line 21 код файла index.php Код (Text): <?php Class Database{ private $link; public function __construct() { $this->connect(); } private function connect(){ $config = require_once 'config.php'; $dsn = 'mysql:host='.$config['host'].';dbname='.$config['db_name'].';charset='.$config['charset']; $this->link = new PDO($dsn, $config['username'], $config['password']); return $this; } public function execute($sql){ $sth = $this->link->prepare($sql); $sth->execute(); return $sth; } public function query($sql){ $exe = $this->execute($sql); $result = $exe->fetchAll(PDO::FETCH_ASSOC); if ($result === false){ return []; } return $result; } } $db = new Database(); Код файла config.php Код (Text): <?php return[ 'host' => 'localhost', 'db_name' => 'test_pdo', 'username' => 'root', 'password' => '', 'charset' => 'utf8' ];
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.51-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. --- Добавлено --- Добавил пароль в phpmyadmin Ошибка в браузере: Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) in C:\OpenServer\domains\test\index.php:21 Stack trace: #0 C:\OpenServer\domains\test\index.php(21): PDO->__construct() #1 C:\OpenServer\domains\test\index.php(9): Database->connect() #2 C:\OpenServer\domains\test\index.php(49): Database->__construct() #3 {main} thrown in C:\OpenServer\domains\test\index.php on line 21
при чем тут phpmyAdmin? пароль пользователю надо добавить .. ну допустим что вы через phpmyadmin добавили.... а в конфиге то его тоже прописали?
Решение оказалось простым, нужно было вместо localhost указать 127.0.0.1:3000, чтобы бд подключалась из опенсервера.