简体   繁体   English

Yii2无法连接到MongoDB集群

[英]Yii2 Can't connect to the MongoDB cluster

I try to find out what's wrong on https://github.com/yiisoft/yii2-mongodb , but someone just keeps me the link to docs and official chat. 我尝试在https://github.com/yiisoft/yii2-mongodb上找出问题所在,但有人将我链接到文档和官方聊天室。 There is no any answer how to connect to the cluster. 没有任何答案如何连接到群集。 Please help me to solve this issue, I try a lot and spent last 2 days to solve it, but can't understand why it doesn't connect. 请帮助我解决此问题,我尝试了很多,并花了最后两天的时间来解决它,但不明白为什么它无法连接。

I try to configure yii2-mongodb on my project, installed it using composer, then add 我尝试在我的项目上配置yii2-mongodb,使用composer安装它,然后添加

in config/web.php 在config / web.php中

'mongodb' => [
    'class' => '\yii\mongodb\Connection',
    'dsn' => 'mongodb://user:password@insta1-shard-00-00-s0vxe.mongodb.net:27017,insta1-shard-00-01-s0vxe.mongodb.net:27017,insta1-shard-00-02-s0vxe.mongodb.net:27017/database?ssl=true&replicaSet=Insta1-shard-0&authSource=admin',
]

and use this model: 并使用此模型:

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\mongodb\Query;

/**
 * BlogContent is the model behind the contact form.
 */
class BlogContent extends Model
{

    public function getPostList($page) {
        $query = new Query();
        // compose the query
        $rows = $query->select(['name', 'price', 'title'])
            ->from('services')->all();
    }

}

it should return me all documents from the collection, but it returns to me this stack: 它应该返回集合中的所有文档,但是会返回此堆栈:

yii\base\UnknownMethodException: Calling unknown method: yii\mongodb\QueryBuilder::build() in /var/www/service/vendor/yiisoft/yii2/base/Object.php:220
Stack trace:
#0 /var/www/service/vendor/yiisoft/yii2/db/Query.php(126): yii\base\Object->__call('build', Array)
#1 /var/www/service/vendor/yiisoft/yii2/db/Query.php(213): yii\db\Query->createCommand(Object(yii\mongodb\Connection))
#2 /var/www/service/models/BlogContent.php(21): yii\db\Query->all()
#3 /var/www/service/controllers/BlogController.php(35): app\models\BlogContent->getPostList(1)
#4 [internal function]: app\controllers\BlogController->actionIndex(1)
#5 /var/www/service/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#6 /var/www/service/vendor/yiisoft/yii2/base/Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#7 /var/www/service/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('index', Array)
#8 /var/www/service/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('blog/index', Array)
#9 /var/www/service/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#10 /var/www/service/web/index.php(12): yii\base\Application->run()
#11 {main}

Additional info: 附加信息:

Yii version | Yii版本| ~2.0.5 〜2.0.5

Yii MongoDB version | Yii MongoDB版本| ~2.1.0 〜2.1.0

MongoDB server version | MongoDB服务器版本| MongoDB Atlas MongoDB地图集

PHP version | PHP版本| 7.0 7.0

Operating system | 作业系统| Ubuntu 16.04 Ubuntu 16.04

I solve this first issue by passing a $db connection to query as an optional argument, but it can't connect anyway. 我通过将$ db连接作为可选参数传递给查询来解决第一个问题,但是无论如何它都无法连接。 Also, I test connection to MongoDB on my local machine - for a single database it works as need. 另外,我在本地计算机上测试到MongoDB的连接-对于需要运行的单个数据库。

问题是因为我的php-mongodb与php7发生冲突,所以在我更新后,所有这些都开始按需工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM