Database Connection Error (Access denied)

Hello,

I’ḿ using CAKE 4.0, and I edited my app.php as follow:

‘default’ => [
‘className’ => ‘Cake\Database\Connection’,
‘driver’ => ‘Cake\Database\Driver\Mysql’,
‘persistent’ => false,
‘timezone’ => ‘UTC’,
‘host’ => ‘localhost’,
‘username’ => ‘user_here’,
‘password’ => ‘password_here’,
‘database’ => ‘db_here’,
‘enconding’ => ‘utf8’
],

But I get this error:

CakePHP is NOT able to connect to the database.
Connection to Mysql could not be established: SQLSTATE[HY000] [1045] Access denied for user ‘my_app’@‘localhost’ (using password: YES)

I don’t know why I’ḿ getting this error, very strange. I’m using the same DB (and the same configurantion in app.php) in another Cake project, and it looks fine.

Thanks

Do you have a config/app_local.php? If so, you should set the database connection there.

1 Like

Thanks a lot!! You solved my problem.

Hi all,

I have the same problem.
Configuration:
OS: Windows 7
using WAMP Server 3.2.0
PHP 7.4
MySql 8.0.18

CakePHP 4 installed using composer

Following the getting started guide i set up in MySql a database ‘cake_cms’
Added a user ‘cakephp’ with password ‘pswdcakephp’ granting him all privileges (I used PhpMyAdmin).

I edited the app.php and app_local.php files as follow (but i kept the original copies)


‘Datasources’ => [
‘default’ => [
//‘host’ => ‘localhost’,
/*
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//‘port’ => ‘non_standard_port_number’,

		'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
		
		'host' => 'localhost',
        'username' => 'cakephp',
        'password' => 'pswdcakephp',
        'database' => 'cake_cms',
        // Comment out the line below if you are using PostgreSQL
        'encoding' => 'utf8mb4',
]

launched the developer server from command prompt and let it running

opening the page localhost:8765 green cookhats on all features but the database connection. The error is the same as @cavatoni reports:

CakePHP is NOT able to connect to the database.
Connection to Mysql could not be established: SQLSTATE[HY000] [1045] Access denied for user ‘cakephp’@‘localhost’ (using password: YES)

I also tried to edit just app.php or app_local.php but with the same error.
Each time I edited the files i also stopped restarted the developer server, cleared browser cache and closed it.
Also tried to restart WAMP.

I have noticed in WAMP control panel that MariaDB has a green mark on it (as if default) but MySql does not.
PHPInfo() works fine

I don’t know where the problem may be. Are there any checks I could do to narrow down the issue?

Thank you for your attention.

After some further checking I managed to solve this.

First of all I needed to set the user’s host name to localhost (i did not notice before was %). Changed it using PHPMyAdmin

Secondly I changed the port number in app_local.php

  • CakePHP will use the default DB port based on the driver selected
    * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
    * the following line and set the port accordingly
    */
    ‘port’ => ‘3308’,

The port number is clearly stated in WAMP Server console, just open MySql menu.

After that i restarted the developer server and reloaded the page.

Now the CakePHP 4 welcoming screen shows it is able to connect to the database. :star_struck: