首页 > 解决方案 > 更改了 MongoDB 端口,但仍尝试使用默认端口

问题描述

我更改了 /etc/mongod.conf 文件中的端口,即

net:
  port: 12345
  bindIp: 127.0.0.1,123.123.23.255 #localhost and server pub ip

然后我重新启动了mongo

sudo systemctl restart mongod.service

并检查一切正常:

sudo systemctl status mongod

mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-07-10 13:35:14 UTC; 16s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 1927 (mongod)
    Tasks: 23
   Memory: 136.3M
      CPU: 1.240s
   CGroup: /system.slice/mongod.service
           └─1927 /usr/bin/mongod --config /etc/mongod.conf

然后我尝试登录mongo:

mongo --username me --password my_password --authenticationDatabase authdb

并得到以下错误:

MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
2018-07-10T13:29:14.716+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-07-10T13:29:14.717+0000 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :

然后我在 mongod.conf 中将端口更改回 27017 并且可以登录到数据库 OK。

知道为什么 mongo shell 会尝试 27017,即使 mongo 端口已更改?

标签: databasemongodb

解决方案


您在 mongo 服务器(更改 .conf 文件时配置的服务器)和用于连接 mongo 服务器的 mongo 客户端之间感到困惑。如果您将端口更改为 12345,那么您必须在尝试连接时对其进行精确化,并在连接命令中提供它:

mongo --username me --password my_password --authenticationDatabase authdb --port 12345

或者

mongo --username me --password my_password --authenticationDatabase authdb --host 127.0.0.1:12345

编辑:在 mongo 命令中没有任何端口规范,将使用默认端口,所以 27017。


推荐阅读