YII2 Elasticsearch 数据库配置

问题描述 投票:0回答:2

我已经安装了 yii2 和 Elasticsearch、Logstash、Kibana。除了与 YII2 的交互之外,设置运行良好。

以下是

./config/db.php

的内容
<?php
return [
    'components' => [
    'db' => [
    'class' => 'yii\elasticsearch\Connection',
            'user' => 'admin',
            'password' => 'admin',
            'nodes' => [ ['http_address' => '127.0.0.1:9200'],
            // configure more hosts if you have a cluster
        ],
    ],
]
   ];

此配置不起作用,以下是错误的第一行。

无效配置 – yii ase\InvalidConfigException

“db”组件的配置必须包含“class” 元素。

  1. 在 /var/www/hostings/yiiphp/tools/vendor/yiisoft/yii2/di/ServiceLocator.php

......

我找不到相关信息来解决这个问题。我做错了什么?

php elasticsearch yii2
2个回答
0
投票

file

./config/db.php
应包含在主配置文件中,在您的情况下名为
web
(看来您正在使用基本应用程序模板) 换句话说 - 查看文件
./config/web.php
然后找到该行:
'db' => ...
并将其替换为
'db' => require(__DIR__ . '/db.php'),
最后,你的
db.php
应该是这样的:

<?php
return [
    'class' => 'yii\elasticsearch\Connection',
    'user' => 'admin',
    'password' => 'admin',
    'nodes' => [ ['http_address' => '127.0.0.1:9200'] ]
];

对我来说这也很奇怪,你想使用

elastic
组件作为数据库组件,但这当然取决于你。


0
投票

您可以在配置文件中使用以下代码: '

elasticsearch' => [
            'class' => yii\elasticsearch\Connection::class,
            'auth' => ['username' => 'elastic', 'password' => 'changeme'],
            'nodes' => [
                ['http_address' => 'localhost:9200'],
                // configure more hosts if you have a cluster
            ],
            // set autodetectCluster to false if you don't want to auto detect nodes
            'autodetectCluster' => false,
            'dslVersion' => 7, // default is 5
        ],
© www.soinside.com 2019 - 2024. All rights reserved.