🥪 安装MongoDB后可正常连接到数据库,过段时间再重连MongoDB数据库时,显示由于目标计算机积极拒绝,无法连接。
1 问题描述
问题描述
Win10系统在本地安装了MongoDB数据库,安装后可以正常连接到数据库中。
过段时间,再重新使用DataGrip进行连接时,Test Connection
会报错显示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 DBMS: Mongo DBCase sensitivity: plain=mixed, delimited=mixed Driver: MongoDB JDBC Driver (ver. 1.18 , JDBC4.2 ) Effective version : UNKNOWN (ver. 0.0 ) Ping: 10 sec, 349 ms (keep-alive query results in error) Timed out after 10000 ms while waiting to connect . Client view of cluster state is { type =UNKNOWN , servers=[ { address=127.0 .0 .1 :27017 , type =UNKNOWN , state=CONNECTING, exception ={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: no further information} } ] }.
2 原因分析
原因分析
以管理员身份打开CMD命令行窗口,使用命令mongo
启动MongoDB数据库。
查看是否可以正常启动,结果显示“由于目标计算机积极拒绝,无法连接”。
猜测可能是由于服务未正常开启的原因,命令查询MongoDB当前的服务状态。
1 2 3 4 5 6 7 8 9 > mongo MongoDB shell version v5.0 .18 connecting to: mongodb://127 .0 .0 .1 :27017 /?compressors=disabled&gssapiServiceName=mongodbError: couldn 't connect to server 127.0.0.1:27017, connection attempt failed : SocketException : Error connecting to 127.0.0.1:27017 :: caused by :: 由于目标计算机积极拒绝,无法连接。 : connect @src /mongo /shell /mongo.js :372:17 @(connect ):2:6 exception : connect failed exiting with code 1
3 解决方法
解决方法
STATE : 4 RUNNING
:MongoDB服务正在运行。
STATE : 1 STOPPED
:MongoDB服务没有启动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 > sc query MongoDBSERVICE_NAME: MongoDB TYPE : 10 WIN32_OWN_PROCESS STATE : 1 STOPPED WIN32_EXIT_CODE : 1077 (0x435 ) SERVICE_EXIT_CODE : 0 (0x0 ) CHECKPOINT : 0x0 WAIT_HINT : 0x0 <!-- 启动失败,可能是权限不够 --> > net start MongoDB System error 5 has occurred .Access is denied .<!-- 以管理员权限启动MongoDB 服务 --> > net start MongoDB The MongoDB service is starting ..The MongoDB service was started successfully .
4 其他操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <!-- 停止MongoDB服务 --> > net stop MongoDB The MongoDB service is stopping. The MongoDB service was stopped successfully. <!-- 卸载MongoDB服务 --> > sc delete MongoDB [SC] DeleteService SUCCESS <!-- 安装MongoDB服务 --> <!-- MongoDB安装路径下创建data目录和log目录,log目录中创建MongoDB.log文件 --> <!-- dbpath指定data目录存放数据库数据,logpath指定MongoDB.log存放日志信息 --> > mongod -dbpath=...\MongoDB\data --logpath=...\MongoDB\log\MongoDB.log --install --serviceName "MongoDB" { "t":{"$date ":"2024 -05 -15 T14:51 :34 .899 Z"}, "s":"I", "c":"CONTROL", "id":20697 , "ctx":"-", "msg":"Renamed existing log file", "attr": { "oldLogPath":"...\\MongoDB\\log\\MongoDB.log", "newLogPath":"...\\MongoDB\\log\\MongoDB.log.2024 -05 -15 T14-51 -34 " } } <!-- 启动MongoDB服务 --> > net start MongoDB The MongoDB service is starting.. The MongoDB service was started successfully.