Laravel 5 如何配置队列数据库驱动连接非默认数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35535920/
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 13:16:44  来源:igfitidea点击:

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

databaselaravellaravel-5queuelaravel-5.1

提问by userpal

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

在 Laravel 5.1 中,我们可以在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.

但是,它只会使用config/database.php.

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

如果我有 2 个数据库,mysql1本地主机中有 1 个默认数据库,mysql2远程服务器中有1 个数据库,并且队jobs列表在远程数据库中mysql2,我如何配置队列数据库驱动程序以使用远程mysql2数据库?请注意,主应用程序使用本地主机中的默认数据库。

回答by DraughtGlobe

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

您可以使用'connection'参数 inqueue.php设置正确的数据库连接(来自您在 中定义的连接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 ) .

注意:这不仅会从此连接读取作业(运行队列时),还会将它们写入此连接(当分派新作业时)。

回答by Jed Lynch

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.

这里的最佳答案对我不起作用,并不是说它不是与我不同的问题的最佳答案。我的问题是 Laravel 没有缓存我的配置设置。

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

进入文件 \config\queue.php 并更改默认驱动程序后...

'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...

在第 30 行左右,我看到了这个......

 '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.

运行 config:cache 命令会将 config.php 文件重写为当前的驱动程序设置。

回答by Ohgodwhy

You can set the $connectionvariable in the model. Note that this will only affect Eloquentqueries and will not work for the Fluid Query Builder.

您可以$connection在模型中设置变量。请注意,这只会影响Eloquent查询,不适用于 Fluid Query Builder。

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

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

这当然需要您namedconfig/database.php文件中具有第二个连接'database2' => [...].