在配置文件mongodb中添加ip

时间:2018-01-24 14:38:52

标签: mongodb configuration

我在Windows Server 2012上安装了mongodb 3.6并修改了配置文件以添加新的IP,

systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db
security:
    authorization: enabled
net:
    port: 27017 
    bindIp: 127.0.0.1,192.168.1.11

当我尝试启动该服务时,这会向我发送下一条消息


The MongoDB service is starting.
The MongoDB service could not be started.

Service specific error: 48.

You can get more help with the NET HELPMSG 3547 command.

我检查日志文件并发现此消息

2018-01-24T09:18:23.511-0500 I CONTROL  [initandlisten] options: { config: "C:\data\mongod.cfg", net: { bindIp: "127.0.0.1,192.168.1.11", port: 27017 }, security: { authorization: "enabled" }, service: true, storage: { dbPath: "c:\data\db" }, systemLog: { destination: "file", path: "c:\data\log\mongod.log" } }
2018-01-24T09:18:23.512-0500 E STORAGE  [initandlisten] Failed to set up listener: SocketException: The requested address is not valid in this context.
2018-01-24T09:18:23.512-0500 I CONTROL  [serviceStopWorker] now exiting

现在我修改配置文件以在ips之间添加空格

systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db
security:
    authorization: enabled
net:
    port: 27017 
    bindIp: 127.0.0.1, 192.168.1.11

我开始服务


The MongoDB service is starting ..
The MongoDB service started successfully.

我再次检查日志文件并找到以下内容

2018-01-24T09:24:53.807-0500 I CONTROL  [initandlisten] options: { config: "C:\data\mongod.cfg", net: { bindIp: "127.0.0.1, 192.168.1.11", port: 27017 }, security: { authorization: "enabled" }, service: true, storage: { dbPath: "c:\data\db" }, systemLog: { destination: "file", path: "c:\data\log\mongod.log" } }
2018-01-24T09:24:53.808-0500 I NETWORK  [initandlisten] getaddrinfo(" 192.168.1.11") failed: Unknown host.
2018-01-24T09:24:53.808-0500 W NETWORK  [initandlisten] Found no addresses for  192.168.1.11
2018-01-24T09:24:53.808-0500 I -        [initandlisten] Detected data files in c:\data\db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-01-24T09:24:53.808-0500 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7679M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-01-24T09:24:54.029-0500 I STORAGE  [initandlisten] WiredTiger message [1516803894:28660][640:140720456536192], txn-recover: Main recovery loop: starting at 20/11264
2018-01-24T09:24:54.197-0500 I STORAGE  [initandlisten] WiredTiger message [1516803894:196669][640:140720456536192], txn-recover: Recovering log 20 through 21
2018-01-24T09:24:54.297-0500 I STORAGE  [initandlisten] WiredTiger message [1516803894:296682][640:140720456536192], txn-recover: Recovering log 21 through 21
2018-01-24T09:24:55.031-0500 I CONTROL  [initandlisten] 
2018-01-24T09:24:55.031-0500 I CONTROL  [initandlisten] ** WARNING: The file system cache of this machine is configured to be greater than 40% of the total memory. This can lead to increased memory pressure and poor performance.
2018-01-24T09:24:55.031-0500 I CONTROL  [initandlisten] See http://dochub.mongodb.org/core/wt-windows-system-file-cache
2018-01-24T09:24:55.031-0500 I CONTROL  [initandlisten] 
2018-01-24T09:24:55.231-0500 W FTDC     [initandlisten] Failed to initialize Performance Counters for FTDC: WindowsPdhError: PdhExpandCounterPathW failed with 'The specified object was not found on the computer.' for counter '\Memory\Available Bytes'
2018-01-24T09:24:55.231-0500 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'c:/data/db/diagnostic.data'
2018-01-24T09:24:55.232-0500 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-01-24T09:24:55.232-0500 I STORAGE  [initandlisten] Service running

我在很多方面尝试过,[] {} :;并且总是抛出错误,但是当我将bindIp保留为0.0.0.0时,它允许我从其他ips连接。请帮忙。谢谢。

1 个答案:

答案 0 :(得分:3)

bindIp不是客户端IP,而是表示mongodb正在侦听的接口的服务器IP。 0.0.0.0是一个特殊情况,告诉mongodb监听所有可用的接口。

如果您想限制从特定客户端 IP地址访问mongodb,您可以在user or role级别进行访问。在这种情况下,服务器仍将侦听所有允许的接口,但不允许来自未知IP的用户登录。

启动mongod或config file时,--auth命令行选项会启用此功能。

请阅读 step-by-step guide如何正确启用身份验证。

createUser命令的一个示例,它允许testUser仅从192.168.1.11 IP连接:

db.createUser({ user: "testUser",
                pwd: "testPassword",
                roles: [ { role: "readWriteAnyDatabase", db: "admin" } ],
                authenticationRestrictions: [ { clientSource: ["192.168.1.11"] } ]
              });

clientSource数组可以具有CIDR表示法中的确切IP或/和网络掩码的列表,例如, 192.168.1.0/24允许用户从192.168.1.0192.168.1.255的任何IP进行连接。