Codeigniter\Database\Exceptions\DatabaseException 无法连接到数据库

Codeigniter\Database\Exceptions\DatabaseException Unable to connect to database

提问人:bagasdprs 提问时间:10/24/2023 最后编辑:Your Common Sensebagasdprs 更新时间:10/24/2023 访问量:470

问:

我的 ci 4 项目有问题。我想迁移数据库,但无法连接到数据库。我需要解决方案来解决这个问题。 这是我的问题: Bagazz@Bagazz MINGW64 /c/xampp/htdocs/portfolio-ci4apps $ php 火花迁移

CodeIgniter v4.4.2 命令行工具 - 服务器时间:2023-10-24 07:34:54 UTC+00:00

正在运行所有新的迁移...

[CodeIgniter\Database\Exceptions\DatabaseException]

无法连接到数据库。 主连接 [MySQLi]:用户“@”localhost“的访问被拒绝(使用密码:NO)

在 SYSTEMPATH\Database\BaseConnection.php:428

回溯: 1 SYSTEMPATH\Database\BaseConnection.php:575 CodeIgniter\Database\BaseConnection()->initialize()

2 系统路径\数据库\BaseConnection.php:1410 CodeIgniter\Database\BaseConnection()->query('SHOW TABLES FROM ')

3 系统路径\数据库\BaseConnection.php:1429 CodeIgniter\Database\BaseConnection()->listTables()

4 系统路径\数据库\迁移运行者,.php:764 CodeIgniter\Database\BaseConnection()->tableExists('迁移')

5 系统路径\数据库\迁移运行者,.php:168 CodeIgniter\Database\MigrationRunner()->ensureTable()

6 系统路径\命令\数据库\迁移.php:84 CodeIgniter\Database\MigrationRunner()->latest(null)

7 SYSTEMPATH\CLI\命令.php:65 CodeIgniter\Commands\Database\Migrate()->run([])

8 系统路径\CLI\控制台.php:46 CodeIgniter\CLI\Commands()->run('迁移', [])

9 根路径\火花:102 CodeIgniter\CLI\Console()->run()

这是我的.env

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

#--------------------------------------------------------------------
# APP
#--------------------------------------------------------------------

app.indexPage = ''
# app.baseURL = ''
# If you have trouble with `.`, you could also use `_`.
# app_baseURL = ''
# app.forceGlobalSecureRequests = false
# app.CSPEnabled = false

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.tests.hostname = localhost
database.tests.database = portfolio_app.db
database.tests.username = root
database.tests.password =
database.tests.DBDriver = MySQLi
database.tests.DBPrefix =
database.tests.port = 3306

# database.tests.hostname = localhost
# database.tests.database = ci4_test
# database.tests.username = root
# database.tests.password = root
# database.tests.DBDriver = MySQLi
# database.tests.DBPrefix =
# database.tests.port = 3306

我需要解决方案,请...

代码点火器-4

评论


答:

0赞 steven7mwesigwa 10/24/2023 #1

您正在使用主要用于环境中的 PHPUnit 数据库测试的数据库组。但是,在当前配置文件中,您已将其设置为 。testsCI_ENVIRONMENT=testing.envCI_ENVIRONMENT = development

如果您没有运行任何 PHPUnit 数据库测试,并且您当前正在开发环境中工作,请改用数据库组在文件中配置数据库设置。即:.envdefault

database.default.hostname = ...
database.default.database = ...
database.default.username = ...
database.default.password = ...
database.default.DBDriver = ...
database.default.DBPrefix = ...
database.default.port = ...

摘自 app/Config/Database.php

/**
     * This database connection is used when
     * running PHPUnit database tests.
     */
    public array $tests = [
        'DSN'         => '',
        'hostname'    => '127.0.0.1',
        'username'    => '',
        'password'    => '',
        // ...
    ];

    public function __construct()
    {
        parent::__construct();

        // Ensure that we always set the database group to 'tests' if
        // we are currently running an automated test suite, so that
        // we don't overwrite live data on accident.
        if (ENVIRONMENT === 'testing') {
            $this->defaultGroup = 'tests';
        }
    }
}