Yii2 为首次迁移 table 创建设置 MySQL 用户?

Yii2 set MySQL user for first time migration table creation?

我的问题与首次执行 yii migrate/up --interactive=0.
时自动创建 migration 历史记录 table 有关 我能够覆盖迁移本身的数据库访问凭据。 (见下面的例子)

对于我的 Web 应用程序,我有一个用户 default 具有以下 GRANTs:SELECT, INSERT 对于迁移,我想使用我的 admin 用户,他有额外的 GRANT 用于 DDL

执行迁移时 Yii2 想要创建 migration table 与 default 用户,这会导致 permission denied 错误。

如何为 migration table 创建设置数据库用户?

我的迁移脚本:

<?php

use yii\db\Migration;

/**
 * Handles adding columns to table `{{%Tablename}}`.
 */
class m210907_145507_add_json_column_to_Tablename_table extends Migration
{
    public function init()
    {
        $this->db = new \yii\db\Connection([
            'dsn' => 'mysql:host=mysql;dbname=dbname',
            'username' => 'admin',
            'password' => 'itasecrettoeverybody',
            'charset' => 'utf8',
            'enableSchemaCache' => true,
            'schemaCacheDuration' => 60,
            'schemaCache' => 'cache',
        ]);
        parent::init();
    }
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->addColumn('{{%Tablename}}', 'json', $this->json());
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropColumn('{{%Tablename}}', 'json');
    }
}

错误:

Yii Migration Tool (based on Yii v2.0.43)

Creating migration history table "migration"...
Exception 'yii\db\Exception' with message 'SQLSTATE[42000]: Syntax error or access violation: 1142 CREATE command denied to user 'default'@'172.27.0.4' for table 'migration'
The SQL being executed was: CREATE TABLE `migration` (
        `version` varchar(180) NOT NULL PRIMARY KEY,
        `apply_time` int(11)
)'

in /app/vendor/yiisoft/yii2/db/Schema.php:678

Error Info:
Array
(
    [0] => 42000
    [1] => 1142
    [2] => CREATE command denied to user 'default'@'172.27.0.4' for table 'migration'
)

Caused by: Exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1142 CREATE command denied to user 'default'@'172.27.0.4' for table 'migration''

in /app/vendor/yiisoft/yii2/db/Command.php:1302

Stack trace:
#0 /app/vendor/yiisoft/yii2/db/Command.php(1302): PDOStatement->execute()
#1 /app/vendor/yiisoft/yii2/db/Command.php(1102): yii\db\Command->internalExecute()
#2 /app/vendor/yiisoft/yii2/console/controllers/MigrateController.php(273): yii\db\Command->execute()
#3 /app/vendor/yiisoft/yii2/console/controllers/MigrateController.php(212): yii\console\controllers\MigrateController->createMigrationHistoryTable()
#4 /app/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(908): yii\console\controllers\MigrateController->getMigrationHistory()
#5 /app/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(183): yii\console\controllers\BaseMigrateController->getNewMigrations()
#6 [internal function]: yii\console\controllers\BaseMigrateController->actionUp()
#7 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array()
#8 /app/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams()
#9 /app/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction()
#10 /app/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction()
#11 /app/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction()
#12 /app/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction()
#13 /app/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest()
#14 /app/yii(20): yii\base\Application->run()
#15 {main}

迁移 运行 来自控制台。

这意味着,加载 console.php 配置文件而不是您的 main.php 配置。因此,一种也会影响您可能拥有的任何其他控制台命令的方法是更改​​ console 配置的 components 部分中的 db 配置。

config\console.php:

<?php

$config = [
    // ...
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => [], // PLACE HERE CUSTOM ADMIN CREDENTIALS
        // ...
    ],
    
];

现在您的迁移将 运行 使用控制台配置中定义的凭据。

如果您碰巧有其他控制台命令修改数据库中的数据并且您不想也更改它们的用户,那么您可以添加另一个数据库组件并使用管理员凭据将其命名为“dbAdmin”。

<?php

$config = [
    // ...
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => [], // REGULAR DB connection
        'dbAdmin' => [], // PLACE HERE CUSTOM ADMIN CREDENTIALS
        // ...
    ],
    
];

然后检查:

https://www.yiiframework.com/doc/guide/2.0/en/db-migrations#using-command-line-options

指定在运行迁移时使用的数据库连接。

yii migrate --db=dbAdmin