Database connection: [1045] Access denied for user
- Norton Canes
- Member | 5
Trying to connect to a MYSQL database:
database:
dsn: 'mysql:host=localhost;dbname=DB_NAME'
user: root
password: DB_PASSWORD
Gives error:
SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)
Why am I getting this error? Is it something to do with the empty single quotes? Is the username not being passed?
Should I use ‘root’ as the username as in the Nette documentation, or substitute it with the actual db username? (I have done, the error is not resolved). I know the database is functional as I'm connecting to it elsewhere with a PHP PDO class.
Here's the Tracy output:
251:
252:
253: public function createServiceContainer(): Nette\DI\Container
254: {
255: return $this;
256: }
257:
258:
259: public function createServiceDatabase__default__connection(): Nette\Database\Connection
260: {
261: $service = new Nette\Database\Connection('mysql:host=127.0.0.1;dbname=test', null, null, null);
262: $this->getService('tracy.blueScreen')->addPanel('Nette\Bridges\DatabaseTracy\ConnectionPanel::renderException');
263: Nette\Database\Helpers::createDebugPanel($service, true, 'default');
264: return $service;
265: }
- nightfish
- Member | 517
Norton Canes wrote:
Gives error:SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)
It would seem that parameters did not get passed from config file to DI
container. To which file did you put your DB configuration? Is that config file
included in your application (either by includes:
– see docs –
in config.neon
or by addConfig()
in
bootstrap.php)?
- Norton Canes
- Member | 5
I've literally been following the instructions in the Blog tutorial: https://doc.nette.org/…rt/home-page
I've created a table in an existing database and filled it with content.
I've added
database:
dsn: 'mysql:host=localhost;dbname=DB_NAME'
user: root
password: DB_PASSWORD
to the app/config/config.neon file
And this to app/presenters/HomepagePresenter.php:
<?php
namespace App\Presenters;
use Nette;
class HomepagePresenter extends Nette\Application\UI\Presenter
{
/** @var Nette\Database\Context */
private $database;
public function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}
public function renderDefault()
{
$this->template->posts = $this->database->table('posts')
->order('created_at DESC')
->limit(5);
}
}
The tutorial doesn't say to do anything else, so I haven't added any includes or anything in bootstrap.php.
OK, now I've added
database:
dsn: 'mysql:host=localhost;dbname=DB_NAME'
user: root
password: DB_PASSWORD
to app/config/config.local.neon, and the error message is:
SQLSTATE[HY000] [1045] Access denied for user ‘root’@'localhost' (using password: YES)
So at least the user name is being passed… but still no connection.
- nightfish
- Member | 517
Norton Canes wrote:
SQLSTATE[HY000] [1045] Access denied for user ‘root’@'localhost' (using password: YES)
This error message means that your database is not configured to allow
connections of user root
with whatever password you specified in
your config file. Best option would be to change credentials in your
config.local.neon
to match those of an existing user in your MySQL
database. If the error message remains the same (mentioning
root@localhost
) even after making changes to
config.local.neon
, delete the contents of temp/cache
folder (as it contains cached configurations, which do not get automatically
updated unless you are running in the development
mode).
- kevin.waterson@gmail.com
- Member | 81
Try this on your database (command line or other tool)
GRANT ALL on your_db.* TO nightfish@localhost IDENTIFIED BY 'secret_password';
Then, in your config file
database:
dsn: 'mysql:host=localhost;dbname=your_db'
user: nightfish
password: secret_password