覆盖 artisan migrate 命令的默认 Laravel 数据库配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26469370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:18:24  来源:igfitidea点击:

Overriding Default Laravel database configuration for artisan migrate commands

phplaravelmigrationartisan

提问by Mark Baker

For my database (MySQL), I have two user accounts, one (mydbuser) for general application access with select/inset/update/delete permissions on all tables, the other (mydbadmin) with privilege to manage tables, etc

对于我的数据库 (MySQL),我有两个用户帐户,一个 ( mydbuser) 用于对所有表具有选择/插入/更新/删除权限的一般应用程序访问,另一个 ( mydbadmin) 具有管理表等的特权

CREATE USER 'mydbadmin'@'%' IDENTIFIED BY 'ultrasecret password';
GRANT ALL ON mydb.* TO 'mydbadmin'@'%';

CREATE USER 'mydbuser'@'%' IDENTIFIED BY 'not quite so secure password';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'mydbuser'@'%';

My Laravel app is configured to use the mydbuseruser, while mydbadminis currently used for manually setting up the tables, etc.

我的 Laravel 应用程序配置为使用mydbuser用户,而mydbadmin当前用于手动设置表等。

Now, I want to start using Artisan migrationsand seedingto handle creating the database for new instances of the application, but with the standard application configuration, migrateonly has access to the lesser-privileged account, so can't actually create/drop the tables at all.

现在,我想开始使用 Artisanmigrationsseeding为应用程序的新实例创建数据库,但使用标准应用程序配置,migrate只能访问权限较低的帐户,因此根本无法创建/删除表.

Is there a way of overriding the database user/password configured in database.php when I run

当我运行时,有没有办法覆盖在 database.php 中配置的数据库用户/密码

php artisan migrate:install

and other migrate commands? Some command line switch perhaps, that would allow me to specify the mydbadminuser and password? Or even just to point to a different database configuration setting?

和其他迁移命令?也许某些命令行开关可以让我指定mydbadmin用户和密码?或者甚至只是指向不同的数据库配置设置?

回答by Marcin Nabia?ek

You may use:

您可以使用:

php artisan migrate:install --database=seconddb

but you need to have defined this seconddbconnection in your database.phpconfig file. Of course running this command you will only create migrationstable, nothing more, so if you want to make standard migration you need to use:

但是您需要seconddbdatabase.php配置文件中定义此连接。当然运行这个命令你只会创建migrations表,仅此而已,所以如果你想进行标准迁移,你需要使用:

php artisan migrate --database=seconddb

and for seeding:

和播种:

php artisan db:seed --database=seconddb

Example database.phpfile configuration:

示例database.php文件配置:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'maindb',
    'username'  => 'root',
    'password'  => 'pass',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

'seconddb' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'otherdb',
    'username'  => 'user',
    'password'  => 'otherpass',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

And in case you need other information about paramters, you can always run --helpargument for example:

如果您需要有关参数的其他信息,您始终可以运行--help参数,例如:

php artisan migrate:install --help
php artisan migrate --help

回答by Mason8r

Would adding a new DB in the settings file help? Perhaps:

在设置文件中添加新数据库有帮助吗?也许:

    # Slightly different database connection
    'mydbadmin' => array(
        'driver'    => 'mysql',
        'host'      => 'host1',
        'database'  => 'mydb',
        'username'  => 'mydbadmin',
        'password'  => 'ultrasecret password'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

then specify that database within the CLI, e.g.

然后在 CLI 中指定该数据库,例如

php artisan migrate:install --database['mydbadmin']