Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 How to configure the Queue database driver to connect to a non-default database?

In Laravel 5.1, we can set the Queue connection configurations in config/queue.php.

QUEUE_DRIVER=database

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'expire' => 60,
    ],

However, it will only use the default database connection in config/database.php.

If I have 2 database, 1 default database mysql1 in localhost, and 1 database mysql2 in a remote server, and the Queue jobs table is in the remote database mysql2, how can I configure the Queue database driver to use the remote mysql2 database? Please note that the main App is using the default database in localhost.

like image 367
userpal Avatar asked Feb 21 '16 12:02

userpal


People also ask

How does laravel connect to external database?

In . env file you can set DB_CONNECTION with your database name and applicable databases are given in /config/database. php which are (SQLite, MySQL, pgSQL, SQLSRV) after that just type your username, password, and database name and you can use that database with port number.

How do I change the default database in laravel?

To change its default database type, edit the file config/database. php: Search for 'default' =>env('DB_CONNECTION', 'mysql') Change it to whatever required like 'default' =>env('DB_CONNECTION', 'sqlite')

What is queue driver in laravel?

Laravel Queues Queue Driver Configuration A queue driver is the handler for managing how to run a queued job, identifying whether the jobs succeeded or failed, and trying the job again if configured to do so.


3 Answers

You can use the 'connection' parameter in queue.php to set the correct database connection ( from the ones you've defined in database.php ).

'database' => [
    'connection' => 'mysql2',
    'driver' => 'database',
    'table' => 'jobs',
    'queue' => 'default',
    'expire' => 60,
], 

I was looking for the same thing and found it in the source code.

NOTE: This will not only read the jobs from this connection ( when running the queue ), but also write them to this connection ( when dispatching a new Job ) .

like image 169
DraughtGlobe Avatar answered Oct 08 '22 14:10

DraughtGlobe


The best answer here did not work for me, not to say it isn't the best answer for a different issue than mine. My issue was that Laravel did not cache my config settings.

After going into file \config\queue.php and changing the default driver...

'default' => env('QUEUE_DRIVER', 'database'),

The queue was still running on the sync driver.

I then checked the file...

    \bootstrap\cache\config.php

Around line 30 I saw this...

 'queue' => 
array (
'default' => 'sync', 

...but to connect to the database, it should be...

 'queue' => 
array (
'default' => 'database',

This resolved the issue...

php artisan config:cache

Running the config:cache commmand rewrites the config.php file to the current driver settings.

like image 31
Jed Lynch Avatar answered Oct 08 '22 13:10

Jed Lynch


You can set the $connection variable in the model. Note that this will only affect Eloquent queries and will not work for the Fluid Query Builder.

class Jobs extends Eloquent {
    protected $connection = "database2"     
}

This would of course require you to have a 2nd named connection in your config/database.php file that is 'database2' => [...].

like image 1
Ohgodwhy Avatar answered Oct 08 '22 12:10

Ohgodwhy