ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接 [英] ArgumentException: Invalid keyword 'mongodb+srv://test:test@HOST' in MongoDB + Unity2D - Live MongoDB not connecting

查看:49
本文介绍了ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在本地连接我的 Live MongoDB 数据库,但在我使用外部字符串而不是 localhost

I am trying to connect my Live MongoDB Database in local, but getting error while I use external string instead of localhost

我使用:

  • MongoDB 数据库 v4.0.x(使用 mongoDB 的免费集群)
  • Unity 2020.3.13

如果我使用 localhost,即使所有的插入、更新、删除查询都能正常工作,这段代码也能正常工作

If I use localhost, This code works fine even all insert, Update, Delete queries works perfect

using UnityEngine;
using UnityEngine.UI;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Collections;
using UnityEngine.Networking;
using MongoDB.Driver.Builders;
using UnityEngine.SceneManagement;
using System;
using Newtonsoft.Json ;

public class Login_Screen : MonoBehaviour {

public InputField usernameedit;
public InputField passwordone;
public Button loginButton;


  void start(){
    string connectionString = "mongodb://localhost:27017";
    var client = new MongoClient(connectionString);
    var server = client.GetServer();
    var database = server.GetDatabase("admin");
    var playercollection = database.GetCollection<BsonDocument>("users");
  }
}

但是我用于 LIVE 数据库调用的相同内容,然后导致以下错误.甚至连不上.

But Same thing I use for LIVE database calling, then causing below error. not even connect.

string connectionString = "mongodb+srv://testUsername:testPassword@testClustor.mongodb.net?retryWrites=true&w=majority";

错误信息如下:

ArgumentException: 无效的关键字 'mongodb+srv://testusername:testpassword@xyz.xxx.mongodb.net/admin1?w'.MongoDB.Driver.MongoConnectionStringBuilder.set_Item(System.String 关键字,System.Object 值)(位于 <6da29fc855c44d33ad78b3e27475ff27>:0) System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(System.String 值)(位于 <6da29fc855c44d33ad78b3e27475ff27>:0) MongoDB.Driver.MongoConnectionStringBuilder..ctor (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) MongoDB.Driver.MongoClient.ParseConnectionString (System.String connectionString) (at <6da29fc85c44d33ad78b3e27475ff27>:0) (at <6da29fc85c44d33ad78b3e27475ff27>:0)MongoClient..ctor (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) Login_Screen.DoLogin() (at Assets/Script/Login_Screen.cs:61) UnityEngine.Events.InvokableCall.Invoke (at <6da29fc855c44d33ad78b3e27475ff27>:0);:0) UnityEngine.Events.UnityEvent.Invoke () (at <24599fe2776145d58ab771516c063d56>:0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/核心/Button.cs:68) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)(在 Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:110)UnityEngine.EventSystems.ExecuteEvents.Execute(UnityEngine.EventSystems.IPointerClickHandler 处理程序,UnityEngine.EventSystems.BaseEventData eventData)(位于 Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:50)UnityEngine.EventSystems.ExecuteEvents.Execute[T](UnityEngine.GameObject 目标,UnityEngine.EventSystems.BaseEventData eventData,UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] 函子)(在 Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:262) UnityEngine.EventSystems.EventSystem:Update()(位于 Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)

我真的浪费了 2 天的时间来寻找解决方案,请任何人帮助我.提前致谢

I really wasted 2 days of finding solution, Please help me anybody. Thanks in advance

推荐答案

我遇到了同样的问题.检查您的 db 和 db 用户的设置 - 确保它们可以从外部 IP 访问.

I had the same problem. Check the settings of your db, and of your db user - make sure they are reachable from external IPs.

我像这样修改了我的连接字符串:

I modified my connection string like this:

 private readonly Dictionary<string, string> _config = new Dictionary<string, string>()
        {
            {"dbUser", "xxxxxx"},
            {"dbName", "YourDbName"},
            {"password", "xxxxxxxxxxxxx"}
        };

        public IMongoDatabase CreateInstance()
        {
            var client = new MongoClient($"mongodb+srv://{_config["dbUser"]}:{HttpUtility.UrlEncode(_config["password"])}@pocdata.0u29g.mongodb.net/{HttpUtility.UrlEncode(_config["dbName"])}?retryWrites=true&w=majority");
            return client.GetDatabase(_config["dbName"]);
        }

我只是使用公共方法连接到数据库,但这不是强制性的 - 您可以直接替换值 - 只需对密码和数据库名称进行 url 编码

I just use the public method for connecting to db, but this is not mandatory - you can replace the values directly - just url encode the password and db name

这篇关于ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆