cakephp 错误。连接到数据库...不是...:用户 'my_app'@'localhost' 的访问被拒绝(使用密码:是)

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

我正在尝试启动 CakePHP。我制作了书签并通过命令 bin\cake server 进行了测试。它显示一个错误,因为无法建立与数据库的连接:

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

我阅读了 config/app.default.php 文件。它说有一个数据库 my_app 和另一个数据库 test_myapp 有一些用户。我在 xampp 的 phymyadmin 中找不到这些数据库。我应该手动创建指定的数据库和用户吗?我认为 CakePHP 应该自动创建这些。或者我应该给出我喜欢的数据库名称等,并使用 xampp 和 windows 7 创建 same.I' 并且对 CakePHP 很陌生。

Cake 不会为您创建数据库或数据库用户。您需要自己创建它们,然后将这些数据库凭据匹配到数据库配置文件中。 app/Config/app.php 文件中的数据源应类似于:

'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,
    ]
],

在此示例中,您必须创建一个名为:cake_database_name 的数据库,然后添加一个名为 my_db_user 的数据库用户 密码为 my_db_user_password.

参见Mysql.php:

class Mysql 扩展驱动程序 {

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' => [],
];

然后在app.php中写

            '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,

...],

在 phpMyAdmin 中 创建蛋糕数据库。 都这样了

错误:

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

原因:

No user id: "my_app" with password: "secret" [or any other] set in config/app.php file.

解决方案:

In case of WAMP, go to phpmyadmin page, then we can select cake_cms database -> privilage-> create user id and password, same as in config file config/app.php, refresh the http://localhost:8765/ page. it should now show

CakePHP is able to connect to the database.

使用您的数据库连接参数编辑 Config/app_local.php 文件。您将找到主机、用户名、密码、数据库名称。

不要使用数据库连接参数编辑 config/app.php

你的问题现在就解决了

CakePHP4.x

  • 编辑 Config/app_local.php 文件而不是 Config/app.php 与您的数据库连接参数,即主机、用户名、密码、数据库名称。这将解决问题。

  • 确保首先创建相同的数据库并为您在 app_local.php 文件中写入的同一用户提供足够的权限。

如果您使用的是 wamp,请确保端口在数据源中取消注释并且 'port' => '3308'

我通过检查 phpmyadmin 顶部的端口解决了这个问题
并取消提交 app_local.php

中的行
'port' => '3308',

CakePHP3.9.3,app_local.php,'用户名`=>'root','password'=>'','database' => 'db name'