database - 更改了 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 端口已更改?
解决方案
您在 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。
推荐阅读
- javascript - 如何添加 HTML 附加 javascript...“引号”问题
- apache-spark - Spark EMR 作业失败:原因:org.apache.spark.memory.SparkOutOfMemoryError: Unable to acquire 16384 bytes of memory, got 0
- c# - 带有包含空格和“/”的字符串的 Http 请求
- react-native - 如何在 react-native 中使用加速度计传感器找到方向?
- r - R:使用 id 和记录频率创建新的数据框
- active-directory - 如何修复具有静态 IP 地址的域网络上的“未识别网络”?
- r - 使用参考列总结数据框
- java - JFLEX 是否有替代 \b 和/或负前瞻的方法?
- python - 使用 Discord.py 存储每个服务器的设置
- webpack - 更改 WebPack Chunk | 的 chunk entry-module\deferred-modules Angular Builders WebPack 插件