macOS启动MongoDB报Address already in use错误
故障现象
在macOS系统终端执行/usr/local/mongodb/bin/mongod命令启动MongoDB服务时启动失败,运行日志显示CONTROL模块抛出错误码为9001的SocketException异常,核心错误提示为「Address already in use(地址已被占用)」,监听器设置失败后进程按流程执行各模块关闭操作,最终以退出码48终止运行。
故障原因
报错本质是MongoDB默认监听的27017端口已被其他进程占用,导致网络监听器无法完成绑定,服务启动流程中断。日志中出现的TCP Fast Open参数警告为系统兼容性提示,不影响核心启动流程,无需额外处理。
修复方案
方案1:释放默认27017端口(推荐)
- 执行端口查询命令,定位占用27017端口的进程PID:
lsof -i :27017
返回结果中PID列即为占用端口的进程编号,绝大多数场景下是之前异常退出残留的mongod进程。
- 执行强制终止命令清理占用进程,将
<PID>替换为上一步查询到的实际进程编号:
kill -9 <PID>
- 重新执行启动命令即可正常拉起服务:
/usr/local/mongodb/bin/mongod
方案2:更换MongoDB监听端口
如果27017端口被其他不可中断的服务占用,启动时可通过--port参数指定其他未被占用的端口,示例为使用27018端口启动:
/usr/local/mongodb/bin/mongod --port 27018
注意后续通过客户端连接MongoDB时,需要对应指定修改后的端口参数。
完整运行日志
(base) siddhu@siddhus-MacBook-Air ~ % /usr/local/mongodb/bin/mongod {"t":{"$date":"2022-06-08T11:46:06.285+05:30"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"thread1","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"} {"t":{"$date":"2022-06-08T11:46:06.286+05:30"},"s":"I", "c":"NETWORK", "id":4915701, "ctx":"thread1","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}} {"t":{"$date":"2022-06-08T11:46:06.286+05:30"},"s":"W", "c":"ASIO", "id":22601, "ctx":"thread1","msg":"No TransportLayer configured during NetworkInterface startup"} {"t":{"$date":"2022-06-08T11:46:06.286+05:30"},"s":"I", "c":"NETWORK", "id":4648602, "ctx":"thread1","msg":"Implicit TCP FastOpen in use."} {"t":{"$date":"2022-06-08T11:46:06.287+05:30"},"s":"W", "c":"ASIO", "id":22601, "ctx":"thread1","msg":"No TransportLayer configured during NetworkInterface startup"} {"t":{"$date":"2022-06-08T11:46:06.287+05:30"},"s":"I", "c":"REPL", "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}} {"t":{"$date":"2022-06-08T11:46:06.287+05:30"},"s":"I", "c":"REPL", "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}} {"t":{"$date":"2022-06-08T11:46:06.287+05:30"},"s":"I", "c":"CONTROL", "id":5945603, "ctx":"thread1","msg":"Multi threading initialized"} {"t":{"$date":"2022-06-08T11:46:06.288+05:30"},"s":"I", "c":"CONTROL", "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":10144,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"siddhus-MacBook-Air.local"}} {"t":{"$date":"2022-06-08T11:46:06.288+05:30"},"s":"I", "c":"CONTROL", "id":23403, "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"5.0.9","gitVersion":"6f7dae919422dcd7f4892c10ff20cdc721ad00e6","modules":[],"allocator":"system","environment":{"distarch":"x86_64","target_arch":"x86_64"}}}} {"t":{"$date":"2022-06-08T11:46:06.288+05:30"},"s":"I", "c":"CONTROL", "id":51765, "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Mac OS X","version":"21.5.0"}}} {"t":{"$date":"2022-06-08T11:46:06.288+05:30"},"s":"I", "c":"CONTROL", "id":21951, "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{}}} {"t":{"$date":"2022-06-08T11:46:06.289+05:30"},"s":"I", "c":"NETWORK", "id":5693100, "ctx":"initandlisten","msg":"Asio socket.set_option failed with std::system_error","attr":{"note":"acceptor TCP fast open","option":{"level":6,"name":261,"data":"00 04 00 00"},"error":{"what":"set_option: Invalid argument","message":"Invalid argument","category":"asio.system","value":22}}} {"t":{"$date":"2022-06-08T11:46:06.289+05:30"},"s":"E", "c":"CONTROL", "id":20568, "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}} {"t":{"$date":"2022-06-08T11:46:06.289+05:30"},"s":"I", "c":"REPL", "id":4784900, "ctx":"initandlisten","msg":"Stepping down the ReplicationCoordinator for shutdown","attr":{"waitTimeMillis":15000}} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"COMMAND", "id":4784901, "ctx":"initandlisten","msg":"Shutting down the MirrorMaestro"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"SHARDING", "id":4784902, "ctx":"initandlisten","msg":"Shutting down the WaitForMajorityService"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"NETWORK", "id":4784905, "ctx":"initandlisten","msg":"Shutting down the global connection pool"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"NETWORK", "id":4784918, "ctx":"initandlisten","msg":"Shutting down the ReplicaSetMonitor"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"SHARDING", "id":4784921, "ctx":"initandlisten","msg":"Shutting down the MigrationUtilExecutor"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"ASIO", "id":22582, "ctx":"MigrationUtil-TaskExecutor","msg":"Killing all outstanding egress activity."} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"COMMAND", "id":4784923, "ctx":"initandlisten","msg":"Shutting down the ServiceEntryPoint"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"CONTROL", "id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"CONTROL", "id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"CONTROL", "id":4784928, "ctx":"initandlisten","msg":"Shutting down the TTL monitor"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"CONTROL", "id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"-", "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"FTDC", "id":4784926, "ctx":"initandlisten","msg":"Shutting down full-time data capture"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"CONTROL", "id":20565, "ctx":"initandlisten","msg":"Now exiting"} {"t":{"$date":"2022-06-08T11:46:06.290+05:30"},"s":"I", "c":"CONTROL", "id":23138, "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":48}}
内容的提问来源于stack exchange,提问作者THE ULTIMATE GAMER
相关产品推荐
相关产品推荐

