Overriding default Laravel database configuration for wizard migration commands
For my database (MySQL), I have two user accounts, one ( mydbuser
) for application sharing with select / insert / update / delete permissions for all tables, another ( mydbadmin
) with privilege to manage the table, etc.
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 application is configured for user use mydbuser
and mydbadmin
currently used to manually set up tables, etc.
Now I want to start using Artisan migrations
and seeding
to handle creating a database for new instances of the application, but with the default configuration of the application, migrate
it only has access to the less privileged account, so cannot create / drop tables at all.
Is there a way to override the database user / password configured in the database.php file at startup
php artisan migrate:install
and other transfer commands? Perhaps there are several command line commands that will allow me to provide a user and password mydbadmin
? Or even just point to a different database configuration parameter?
source to share
You can use:
php artisan migrate:install --database=seconddb
but you need to define this connection seconddb
in the config file database.php
. Of course, running this command will create a table migrations
, but no more, so if you want to do a standard migration you need to use:
php artisan migrate --database=seconddb
and for sowing:
php artisan db:seed --database=seconddb
Example database.php
File Configuration:
'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 if you need other information about the parameters, you can always run the argument --help
, for example:
php artisan migrate:install --help
php artisan migrate --help
source to share
Will a new DB be added in the settings file help? Maybe:
# 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 point that database in CLI like
php artisan migrate:install --database['mydbadmin']
source to share