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

时间:2022-12-09 19:19:20

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:

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

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

SQLSTATE [HY000] [1045]拒绝访问用户'my_app'@'localhost'(使用密码: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.

我读了config / app.default.php文件。它说有一个数据库my_app和另一个数据库test_myapp与一些用户。我在xampp的phymyadmin中找不到这些数据库。我应该手动创建命名数据库和用户吗?我认为CakePHP应该自动创建它们。或者我应该给出我喜欢的数据库等名称并创建它们。我正在使用xampp和Windows 7,这对CakePHP来说是一个新手。

2 个解决方案

#1


4  

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:

Cake不会为您创建数据库或数据库用户。您需要自己创建它们,然后将这些数据库凭据匹配到db配置文件中。 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,
    ]
],

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.

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

#2


1  

See in Mysql.php:

在Mysql.php中看到:

class Mysql extends Driver {

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

然后在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,

...],

In phpMyAdmin create cake database. It's all.

在phpMyAdmin中创建cake数据库。这就是全部。

#1


4  

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:

Cake不会为您创建数据库或数据库用户。您需要自己创建它们,然后将这些数据库凭据匹配到db配置文件中。 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,
    ]
],

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.

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

#2


1  

See in Mysql.php:

在Mysql.php中看到:

class Mysql extends Driver {

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

然后在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,

...],

In phpMyAdmin create cake database. It's all.

在phpMyAdmin中创建cake数据库。这就是全部。