Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Default Laravel database configuration for artisan migrate commands

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

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 mydbuser user, while mydbadmin is currently used for manually setting up the tables, etc.

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

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

php artisan migrate:install

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

like image 512
Mark Baker Avatar asked Dec 25 '22 03:12

Mark Baker


2 Answers

You may use:

php artisan migrate:install --database=seconddb

but you need to have defined this seconddb connection in your database.php config file. Of course running this command you will only create migrations table, nothing more, so if you want to make standard migration you need to use:

php artisan migrate --database=seconddb

and for seeding:

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 in case you need other information about paramters, you can always run --help argument for example:

php artisan migrate:install --help
php artisan migrate --help
like image 120
Marcin Nabiałek Avatar answered Dec 27 '22 18:12

Marcin Nabiałek


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.

php artisan migrate:install --database['mydbadmin']
like image 27
Mason8r Avatar answered Dec 27 '22 16:12

Mason8r