【问题标题】:error connecting to host: could not connect to server: server selection error: server selection timeout current topology: Type: Single Servers连接到主机时出错:无法连接到服务器:服务器选择错误:服务器选择超时当前拓扑:类型:单服务器
【发布时间】:2021-05-16 15:08:02
【问题描述】:

我需要将 CSV 文件导入 mongoDB,但我遇到以下错误:

    error connecting to host: could not connect to server: server selection error: server selection timeout
    current topology: Type: Single
    Servers:
    Addr: eaderline:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : dial tcp: lookup eaderline: no such host

我的导入字符串如下所示:

mongoimport --host Test-shard-0/test-shard-00-00-gceee.mongodb.net:27017,test-shard-00-01-gceee.mongodb.net:27017,test-shard-00-02-gceee.mongodb.net:27017 --ssl --username <username> --password <password> --authenticationDatabase admin --db EngineeringProject --collection FirstName --type csv -file "C:\Users\Ja\Desktop\INFA\ProjektInzynierski\DaneDBzMC\imiona.csv" -headerline

关于这个错误的有趣之处在于,我能够在 2 周前通过类似的字符串将数据导入 mongodb,如下所示,而今天这是不可能的,为什么?

mongoimport --host Test-shard-0/test-shard-00-00-gceee.mongodb.net:27017,test-shard-00-01-gceee.mongodb.net:27017,test-shard-00-02-gceee.mongodb.net:27017 --ssl --username <username> --password <password> --authenticationDatabase admin --db Test2 --collection COL_First --type csv -file "C:\Users\Ja\Desktop\Przykladowy\Mapy\liczba.csv" -headerline

有人看出有什么错误吗?

我检查了我机器上的 27017 端口是空闲的,也许集群端发生了什么事? 有没有办法启动一个空导入,只是为了检查连接?

【问题讨论】:

    标签: mongodb mongoimport


    【解决方案1】:

    使用 URI 命令它在 windows 命令行中对我有用

    mongoimport --uri "mongodb://**user:passwd**@***host:port***/database?ssl=true&replicaSet=**replicasetneme**"
    -c **collection** --file **csvFile** --fields **fields with comma separated**
    

    【讨论】:

      【解决方案2】:

      最后我能够通过 mongodb GUI 客户端 Compass 导入我的 CSV 文件。

      与此同时,我遇到了另一个问题。 Compass connect 集群显示了数据库,但没有显示集合。事实证明,我的用户的角色很糟糕。我删除了用户并再次添加它,它起作用了,我也可以看到集合。

      之后我就可以导入我的 CSV 文件了。

      【讨论】:

        【解决方案3】:

        1.首先确保db端口27017没有被其他本地服务器实例使用

        2.如果使用云,然后登录到控制台并点击Cluster Name然后点击Command Line Tools复制mongo导入命令,看起来像

        mongoimport --host DemoCluster-shard-0/democluster-shard-00-00-5zjhn.mongodb.net:27017,democluster-shard-00-01-5zjhn.mongodb.net:27017,democluster-shard-00-02-5zjhn.mongodb.net:27017 --ssl --username <USERNAME>--password <PASSWORD> --authenticationDatabase admin --db <DATABASE> --collection <COLLECTION> --type <FILETYPE> --file <FILENAME>
        

        注意:如果文件包含 json 数组,则将 TYPE 传递为 --jsonArray

        【讨论】:

          【解决方案4】:

          每次我遇到这种类型的错误:

          error connecting to host: could not connect to server...
          

          这是因为 MongoDB 没有启动。 尝试手动启动它。 作为 Windows 用户:

          【讨论】:

            猜你喜欢
            • 2019-10-08
            • 2014-10-29
            • 1970-01-01
            • 1970-01-01
            • 2020-12-14
            • 2014-03-25
            • 2012-07-09
            • 1970-01-01
            • 2013-11-08
            相关资源
            最近更新 更多