cakephp error. Connection to database ...not ...: Access denied for user 'my_app'@'localhost' (using password: YES)

19,655

Solution 1

Cake will not create the database or database user for you. You need to create them yourself and then match these database credentials into the db config file. Your datasource in app/Config/app.php file should look something similar to:

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'my_db_user',
        'password' => 'my_db_user_password',
        'database' => 'cake_database_name',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
    ]
],

In this example you would have to create a database named: cake_database_name, and then add a database user named my_db_user with the password of my_db_user_password.

Solution 2

  • Edit Config/app_local.php file instead of Config/app.php with your database connection parameters, i.e. host, username, password, database name. This will solve the problem.

  • Be sure to create same database first and providing enough privileges to same user you're writing in app_local.php file.

Solution 3

See in Mysql.php:

class Mysql extends Driver {

use MysqlDialectTrait;
use PDODriverTrait;

/**
 * Base configuration settings for MySQL driver
 *
 * @var array
 */
protected $_baseConfig = [
    'persistent' => true,
    'host' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'cake',
    'port' => '3306',
    'flags' => [],
    'encoding' => 'utf8',
    'timezone' => null,
    'init' => [],
];

Then in app.php write

            'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        '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',
        'username' => 'root',
        'password' => '', 
        'database' => 'cake',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,

...],

In phpMyAdmin create cake database. It's all.

Solution 4

Edit Config/app_local.php file with your database connection parameters. You will find host, username, password, database name their.

don't edit config/app.php with database connection parameters.

Your problem will be now solved

CakePHP 4.x

Share:
19,655
Ajay Dhar
Author by

Ajay Dhar

Updated on June 04, 2022

Comments

  • Ajay Dhar
    Ajay Dhar almost 2 years

    I am trying to start CakePHP. I made bookmarker and tested by command bin\cake server. It showed one error as connection to database could not be established:

    SQLSTATE[HY000] [1045] Access denied for user 'my_app'@'localhost' (using password: YES).

    I read the config/app.default.php file. It says there is a database my_app and another database test_myapp with some users. I can not find these databases in phymyadmin in xampp. Am I supposed to create the named databases and users manually? I thought CakePHP should create these automatically. Or should I give names of databases etc. which I like and create the same.I'm using xampp with windows 7 and am very new to CakePHP.

  • Ajay Dhar
    Ajay Dhar almost 9 years
    Thanks a lot McWayWeb, specially for the detailed way you answered. You make it very simple. You judged my level of ability accurately and gave a simple explaination!
  • Ajay Dhar
    Ajay Dhar almost 9 years
    I added a database for cake and another for test. I created a user also with a few priviliages. I changed the values in config.app.php as per the 2 databases I created. Still bin\cake server command gives the error Access denied for user 'my_app'@'localhost' (using password: YES). It should not even use the name 'my_app' since I changed it in config/app.php but it persists. Please help.
  • Ajay Dhar
    Ajay Dhar almost 9 years
    I also make 4 tables in the first database as advised by cake docs.
  • Ajay Dhar
    Ajay Dhar almost 9 years
    I got it now. I was editing the wrong app.php. I now edited the higher app.php and it is working nicely. It connects to the database. I am so happy. Thanks a lot.
  • toing_toing
    toing_toing over 8 years
    Which app.php file did you change?
  • Nico Haase
    Nico Haase over 3 years
    Please add some explanation to your answer such that others can learn from it