首页 > 解决方案 > 每当我启动本地服务器时,WSL2 ubuntu Mongod 都会不断更改端口

问题描述

我按照这个链接在 ubuntu wsl 上设置了 mongodb,它有点工作 https://github.com/michaeltreat/Windows-Subsystem-For-Linux-Setup-Guide/blob/master/readmes/installs/MongoDB.md

但是,每当我使用命令sudo mongod --dbpath ~/data/db

它将在本地打开一个 Mongod 服务器并显示这个


    2020-05-07T14:50:56.880-0700 I CONTROL  [initandlisten] allocator: tcmalloc
    2020-05-07T14:50:56.880-0700 I CONTROL  [initandlisten] modules: none
    2020-05-07T14:50:56.880-0700 I CONTROL  [initandlisten] build environment:
    2020-05-07T14:50:56.880-0700 I CONTROL  [initandlisten]     distmod: ubuntu1604
    2020-05-07T14:50:56.880-0700 I CONTROL  [initandlisten]     distarch: x86_64
    2020-05-07T14:50:56.880-0700 I CONTROL  [initandlisten]     target_arch: x86_64
    2020-05-07T14:50:56.880-0700 I CONTROL  [initandlisten] options: { storage: { dbPath: "/home/misterjoe/data/db" } }
    2020-05-07T14:50:56.880-0700 I -        [initandlisten] Detected data files in /home/misterjoe/data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
    2020-05-07T14:50:56.880-0700 I STORAGE  [initandlisten]
    2020-05-07T14:50:56.880-0700 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2020-05-07T14:50:56.880-0700 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2020-05-07T14:50:56.880-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=5840M,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),
    2020-05-07T14:50:57.360-0700 I STORAGE  [initandlisten] WiredTiger message [1588888257:360834][18606:0x7f3480c394c0], txn-recover: Main recovery loop: starting at 6/13440
    2020-05-07T14:50:57.409-0700 I STORAGE  [initandlisten] WiredTiger message [1588888257:409482][18606:0x7f3480c394c0], txn-recover: Recovering log 6 through 7
    2020-05-07T14:50:57.441-0700 I STORAGE  [initandlisten] WiredTiger message [1588888257:441849][18606:0x7f3480c394c0], txn-recover: Recovering log 7 through 7
    2020-05-07T14:50:57.467-0700 I STORAGE  [initandlisten] WiredTiger message [1588888257:467231][18606:0x7f3480c394c0], txn-recover: Set global recovery timestamp: 0
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten]
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten]
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten]
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten]
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2020-05-07T14:50:57.486-0700 I CONTROL  [initandlisten]
    2020-05-07T14:50:57.488-0700 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/home/misterjoe/data/db/diagnostic.data'
    2020-05-07T14:50:57.489-0700 I NETWORK  [initandlisten] listening via socket bound to 127.0.0.1
    2020-05-07T14:50:57.489-0700 I NETWORK  [initandlisten] listening via socket bound to /tmp/mongodb-27017.sock
    2020-05-07T14:50:57.489-0700 I NETWORK  [initandlisten] waiting for connections on port 27017

然后,如果我使用 mongo 数据库启动应用程序,我会得到这个

2020-05-07T14:53:19.392-0700 I NETWORK  [listener] connection accepted from 127.0.0.1:60332 #1 (1 connection now open)
2020-05-07T14:53:19.395-0700 I NETWORK  [conn1] received client metadata from 127.0.0.1:60332 conn1: { driver: { name: "nodejs", version: "3.5.5" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.19.84-microsoft-standard" }, platform: "'Node.js v13.10.1, LE (legacy)" }

我试图将它保持在同一个端口上,这样我就可以使用 mongodb compass 并将它转到我选择的某个数据库。像 RedditClone 或 todoApp 数据库。但是,如果我在该 IP 中键入它不存在,并且我可以从该数据库中看到任何内容的唯一方法是在 javascript 中使用 .find({}) 。请帮忙 :(

标签: mongodbubuntuwindows-subsystem-for-linuxwsl-2

解决方案


你的实际问题是什么?日志中的所有内容都表明您已设置服务器并且能够成功连接到它(从您的本地计算机)。Compass 似乎没有提供设置默认数据库的方法,例如 mongo shell 命令行。如果您可以浏览可用的数据库或在 Compass 中创建新数据库,则您已连接到服务器。

2020-05-07T14:50:57.489-0700 I NETWORK  [initandlisten] waiting for connections on port 27017

此行表示您启动了服务器,它正在侦听 localhost:27017

2020-05-07T14:53:19.392-0700 I NETWORK  [listener] connection accepted from 127.0.0.1:60332 #1 (1 connection now open)

此行表示新客户端成功地从端口 60332 打开了与此服务器的连接(在您的情况下托管在端口 27017 上)。

如果您运行 mongo shell,您将在日志中看到另一个连接到服务器的打开。每个客户端(在您的情况下听起来像 Compass)都将在动态端口上运行,因此您可以同时运行多个客户端。您不连接到客户端端口,只连接到服务器。客户端端口告诉服务器将结果返回到哪个端口。

URL 中指定的数据库是身份验证数据库,由于您没有启用身份验证,因此将其忽略。引用文档

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

...

默认身份验证数据库

可选的。如果连接字符串包含 username:password@ 身份验证凭据但未指定 authSource 选项,则要使用的身份验证数据库。

如果 authSource 和 defaultauthdb 都未指定,客户端将尝试向 admin 数据库验证指定用户。


推荐阅读