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:

Hi folks,

I also have the same problem, but not resolved by anything that worked for you guys.

Configuration
OS: Ubuntu Server 18.04.1
PHP 7.2
Using MariaDB 10.1.44

I installed CakePHP using composer following the installation instructions.

Because I am accessing the server only from LAN via SSL and SSH, I had to host the dev server at 0.0.0.0. Once I got the connection to the dev server working, it doesn’t show the welcome page, instead showing this page:

.

I have created a user called kata_debug and have given it all privileges to mysql. I am able to log into mysql with the user from terminal, but when Cake PHP attempts it the login attempt is denied. This seems to apply to whatever user I enter into the app.php or app_local.php config files. I have opened mysql to connections from all ip addresses with the command

bind_address = 0.0.0.0

in mysql.conf.d/mysqld.cnf to make sure that was not the issue.

Here is my datasource config:

'debug_kit' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        //'persistent' => false,
        //'host' => 'localhost',
        'username' => 'taka_debug',
        'password' => 'test',
        //'database' => 'adr2',
        //'encoding' => 'utf8mb4',
        //'timezone' => 'UTC',
        //'flags' => [],
        //'cacheMetadata' => true,
        //'log' => true,			
        //'quoteIdentifiers' => false,
    ],

All commented out lines produce the same error when un-commented in all combinations.

After spending 2 days on this, I am at a total loss, and on the verge of giving up. Perhaps CakePHP is not for me!

Your error ‘missing database connection’ is a bit different. The config you have posted shows you put the db credentials under the ‘debug-kit’ key in your app.php. You’re going to need to move that under:

[ 'DataSources' => ['defalt' =>[ //settings here ] ] 

Thanks dreamingmind. Yes, I had ‘default’ defined as well with the same settings.

I have now actually managed to resolve this issue by manually creating a database for use by the debug kit following the instructions here.

Ah! I’ve never wandered across that bit before, thanks