Mongodb。 Docker。 SocketException:连接到 127.0.0.1:27017 时出错

Mongodb. Docker. SocketException: Error connecting to 127.0.0.1:27017

我正在使用 Docker Desktop 并尝试启动 2 个容器:mongodb 和 configuration/export for mongo db。 我可以成功启动 mongodb 容器。 来自命令行:

 TCP    127.0.0.1:27017        0.0.0.0:0              LISTENING

但是使用“configure mongo”容器有以下问题:

异常:连接失败

MongoDB shell version v4.0.5

connecting to: mongodb://127.0.0.1:27017/testDb?gssapiServiceName=mongodb

2022-04-17T10:16:12.300+0000 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

connect@src/mongo/shell/mongo.js:328:13

Docker 日志:

docker-compose logs  -f -t
mongodb            | 2022-04-17T10:05:30.226492200Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=4edb2cf1520c
mongodb            | 2022-04-17T10:05:30.226528800Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] db version v3.6.23
mongodb            | 2022-04-17T10:05:30.226533600Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] git version: d352e6a4764659e0d0350ce77279de3c1f243e5c
mongodb            | 2022-04-17T10:05:30.226536000Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
mongodb            | 2022-04-17T10:05:30.226538400Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongodb            | 2022-04-17T10:05:30.226540600Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] modules: none
mongodb            | 2022-04-17T10:05:30.226542700Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] build environment:
mongodb            | 2022-04-17T10:05:30.226544800Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
mongodb            | 2022-04-17T10:05:30.226546900Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongodb            | 2022-04-17T10:05:30.226549000Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongodb            | 2022-04-17T10:05:30.226551300Z 2022-04-17T10:05:30.226+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true } }
mongodb            | 2022-04-17T10:05:30.226835700Z 2022-04-17T10:05:30.226+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongodb            | 2022-04-17T10:05:30.226845600Z 2022-04-17T10:05:30.226+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3398M,cache_overflow=(file_max=0M),session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),compatibility=(release="3.0",require_max="3.0"),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
mongodb            | 2022-04-17T10:05:30.890237100Z 2022-04-17T10:05:30.889+0000 I STORAGE  [initandlisten] WiredTiger message [1650189930:889935][1:0x7fcfbc1eea40], txn-recover: Main recovery loop: starting at 10/5760
mongodb            | 2022-04-17T10:05:30.944878200Z 2022-04-17T10:05:30.944+0000 I STORAGE  [initandlisten] WiredTiger message [1650189930:944601][1:0x7fcfbc1eea40], txn-recover: Recovering log 10 through 11
mongodb            | 2022-04-17T10:05:30.984505600Z 2022-04-17T10:05:30.984+0000 I STORAGE  [initandlisten] WiredTiger message [1650189930:984131][1:0x7fcfbc1eea40], txn-recover: Recovering log 11 through 11
mongodb            | 2022-04-17T10:05:31.016351600Z 2022-04-17T10:05:31.016+0000 I STORAGE  [initandlisten] WiredTiger message [1650189931:16109][1:0x7fcfbc1eea40], txn-recover: Set global recovery timestamp: 0
mongodb            | 2022-04-17T10:05:31.020908600Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten]
mongodb            | 2022-04-17T10:05:31.020935800Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongodb            | 2022-04-17T10:05:31.020940700Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongodb            | 2022-04-17T10:05:31.020944900Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten]
mongodb            | 2022-04-17T10:05:31.020948500Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten]
mongodb            | 2022-04-17T10:05:31.020963500Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
mongodb            | 2022-04-17T10:05:31.020967200Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
mongodb            | 2022-04-17T10:05:31.021026300Z 2022-04-17T10:05:31.020+0000 I CONTROL  [initandlisten]
mongodb            | 2022-04-17T10:05:31.026044000Z 2022-04-17T10:05:31.025+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'        
mongodb            | 2022-04-17T10:05:31.026721000Z 2022-04-17T10:05:31.026+0000 I NETWORK  [initandlisten] listening via socket bound to 0.0.0.0
mongodb            | 2022-04-17T10:05:31.026742300Z 2022-04-17T10:05:31.026+0000 I NETWORK  [initandlisten] listening via socket bound to /tmp/mongodb-27017.sock
mongodb            | 2022-04-17T10:05:31.026748400Z 2022-04-17T10:05:31.026+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

configure-mongodb  | 2022-04-17T10:05:42.001049300Z MongoDB shell version v4.0.5
configure-mongodb  | 2022-04-17T10:05:42.041142600Z connecting to: mongodb://127.0.0.1:27017/testDb?gssapiServiceName=mongodb
configure-mongodb  | 2022-04-17T10:05:42.062474900Z 2022-04-17T10:05:42.062+0000 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
configure-mongodb  | 2022-04-17T10:05:42.062521800Z connect@src/mongo/shell/mongo.js:328:13
configure-mongodb  | 2022-04-17T10:05:42.062530800Z @(connect):1:6
configure-mongodb  | 2022-04-17T10:05:42.064886200Z exception: connect failed

您需要使用服务名称(或容器name/id)作为主机名。错误说:

Error: couldn't connect to server 127.0.0.1:27017

所以您正在尝试连接到 configure-mongodb 容器内的实例 运行,而您实际上想连接到 mongodb 容器。

改为使用以下内容:

mongodb://mongodb:27017/testDb?gssapiServiceName=mongodb