简体   繁体   English

无法使用pymongo连接到mongdb docker内部

[英]can't connect inside a mongdb docker with pymongo

so i've made a docker container with mongodb on a distant computer 192.168.0.17 i run the container with this command : 所以我在远程计算机192.168.0.17上用mongodb制作了一个docker容器,我使用以下命令运行该容器:

docker run -t -i -p 27019:27019 mongodb /bin/bash

inside the container i run it as a user wok with theses option : 在容器内,我以用户炒锅的方式使用以下选项运行它:

mongod --bind_ip 0.0.0.0 --noauth

here is the output (i've created and given the right to wok to /data/db/) : 这是输出(我已经创建并授予/ data / db /的权限):

wok@91cf429cfc28:~$ mongod --bind_ip 0.0.0.0 --noauth
warning: bind_ip of 0.0.0.0 is unnecessary; listens on all ips by default
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] MongoDB starting : pid=41 port=27017 dbpath=/data/db 64-bit host=91cf429cfc28
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] db version v3.2.4
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] git version: e2ee9ffcf9f5a94fad76802e28cc978718bb7a30
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] modules: none
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] build environment:
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten]     distmod: ubuntu1404
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten]     distarch: x86_64
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2016-04-08T15:40:08.661+0000 I CONTROL  [initandlisten] options: { net: { bindIp: "0.0.0.0" }, security: { authorization: "disabled" } }
2016-04-08T15:40:08.672+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2016-04-08T15:40:08.672+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=2G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-04-08T15:40:09.498+0000 I CONTROL  [initandlisten] 
2016-04-08T15:40:09.498+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-04-08T15:40:09.498+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-04-08T15:40:09.498+0000 I CONTROL  [initandlisten] 
2016-04-08T15:40:09.498+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-04-08T15:40:09.498+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-04-08T15:40:09.498+0000 I CONTROL  [initandlisten] 
2016-04-08T15:40:09.536+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2016-04-08T15:40:09.536+0000 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-04-08T15:40:09.537+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

so here is my little script with pymongo on the 192.168.0.16 computer : 所以这是我在192.168.0.16计算机上使用pymongo的小脚本:

from pymongo import MongoClient
from datetime import datetime
client = MongoClient("mongodb://192.168.0.17:27019")
db = client.test
result = db.restaurants.insert_one(
    {
        "address": {
            "street": "2 Avenue",
            "zipcode": "10075",
            "building": "1480",
            "coord": [-73.9557413, 40.7720266]
        },
        "borough": "Manhattan",
        "cuisine": "Italian",
        "grades": [
            {
                "date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
                "grade": "A",
                "score": 11
            },
            {
                "date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
                "grade": "B",
                "score": 17
            }
        ],
        "name": "Vella",
        "restaurant_id": "41704620"
    }
)

print(result)

so i have a connection refused error : 所以我有一个连接被拒绝的错误:

Traceback (most recent call last):
  File "/home/bussiere/WorkspaceSafe/testmongo.py", line 28, in <module>
    "restaurant_id": "41704620"
  File "/usr/local/lib/python3.4/dist-packages/pymongo/collection.py", line 622, in insert_one
    with self._socket_for_writes() as sock_info:
  File "/usr/lib/python3.4/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.4/dist-packages/pymongo/mongo_client.py", line 716, in _get_socket
    server = self._get_topology().select_server(selector)
  File "/usr/local/lib/python3.4/dist-packages/pymongo/topology.py", line 142, in select_server
    address))
  File "/usr/local/lib/python3.4/dist-packages/pymongo/topology.py", line 118, in select_servers
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeout

So if anyone have any ideas ... regards 所以,如果有人有任何想法...问候

I think your mongo daemon is listening on port 27017, and you're publishing 27019 from the container. 我认为您的mongo守护程序正在侦听端口27017,并且您正在从容器中发布27019。

Try: docker run -t -i -p 27019:27017 mongodb /bin/bash 尝试: docker run -t -i -p 27019:27017 mongodb /bin/bash

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

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