MongoDb No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'rootlocalhost']


今天在用TP6框架連接 MongoDB ,但是中途遇到了一個問題 :

No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'rootlocalhost']

,網上搜了一下,還是有不少人遇到這個問題的,好多說ipv6什么的,但是我個人測試之后發現好像不是這個原因,也可能是我遇到的問題不同吧。

 

大家可以定位到這個文件  vendor\topthink\think-orm\src\db\connector\Mongo.php 大概 158行 

原:

if (empty($config['dsn'])) {
  $config['dsn'] = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '');
}

上面是源代碼。

修改之后:
if (empty($config['dsn'])) {
$config['dsn'] = 'mongodb://' . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '');
}

 我這里主要是他配置MongoDB 連接地址的時候多拼接了一個 $config['username'] ,我是把他去掉了,然后就可以連接成功了,暫時不知道會不會影響到其他地方。希望能幫到大家!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM