'MongoDB running on WSL2 can not be connected by Mongo Compass on Windows

I deployed a MongoDB database in WSL2. It's running well:

❯ sudo mongod --dbpath ~/data/db --logpath ~/log/db --fork
about to fork child process, waiting until server is ready for connections.
forked process: 195
child process started successfully, parent exiting
❯ sudo service postgresql restart
 * Restarting PostgreSQL 10 database server                                                                      [ OK ]
❯ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings:
2022-01-24T19:23:51.372+0800 I STORAGE  [initandlisten]
2022-01-24T19:23:51.372+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2022-01-24T19:23:51.372+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten]
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten]
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten]
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten]
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2022-01-24T19:23:52.043+0800 I CONTROL  [initandlisten]

In the past, I could directly use Mongo compass on windows to connect to the Mongo database on WSL(the last day I use it was anout 5 days ago), but now I don't know why I can't do so now: connect ECONNREFUSED 127.0.0.1:27017



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source