【js学习笔记】mongoose连接mongodb数据库失败问题解决
mongoose连接mongodb数据库失败问题解决
·
MongooseError: Operation
XXXbuffering timed out after 10000ms
输出错误信息,发现数据库连接不上
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/ele-admin')
.then(() => console.log('数据库连接成功'))
.catch(err => console.log('数据库连接失败', err));
数据库连接失败
TypeError [ERR_INVALID_URL]: Inva
at onParseError (internal/url.js:279:9)
at new URL (internal/url.js:355:5)
at isAtlas (C:\Users\86176\ele-admin\node_mo\topology\isAtlas.js:17:17)
at MongooseServerSelectionError.assimilateErmin\node_modules\mongoose\lib\error\serverSelect
at C:\Users\86176\ele-admin\node_modules\mon3:36 {
input: '127.0.0.1:27017',
code: 'ERR_INVALID_URL'
}
将127.0.0.1改成localhost,还是报错
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/ele-admin')
.then(() => console.log('数据库连接成功'))
.catch(err => console.log('数据库连接失败', err));
MongooseServerSelectionError: Serorts maximum wire version 5, but this version ofes at least 6 (MongoDB 3.6)
数据库连接失败 MongooseServerSelectionError: Serorts maximum wire version 5, but this version ofes at least 6 (MongoDB 3.6)
at NativeConnection.Connection.openUri (C:\Umodules\mongoose\lib\connection.js:807:32)
at C:\Users\86176\ele-admin\node_modules\mon
at C:\Users\86176\ele-admin\node_modules\monrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\86176\ele-admib\helpers\promiseOrCallback.js:31:10)
n\node_modules\mongoose\lib\index.js:342:20)
at Object.<anonymous> (C:\Users\86176\ele-admin\server\index.js:9:10)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
应该是mongoose和mongodb版本对不上,可以去这个链接查看对应的版本。
[对应版本查看](https://mongoosejs.com/docs/compatibility.html#mongodb-server-version-compatibility)
cmd查看mongodb版本:
mongo --version
package.json查看mongoose版本
"dependencies": {
"axios": "^0.27.2",
"core-js": "^3.8.3",
"cors": "^2.8.5",
"element-ui": "^2.4.5",
"express": "^5.0.0-beta.1",
"mongoose": "^5.2.1",
"vue": "^2.6.14",
"vue-router": "^3.5.1"
},
- 重新安装mongodb:
网上已经有很多教程了,这里只说一个重新安mongodb会出现的问题,原本的mongodb没能正常停止,如何关闭服务
//客户端登录服务,注意,这里通过localhost登录,如果需要远程登录,必须先登录认证才行。
mongo --port 27017
//#切换到admin库
use admin
//关闭服务
db.shutdownServer()
- 重新安装mongoose:
npm i mongoose@5.2.1 //@指定版本号
mongodb和mongoose版本对应上后就可以正常使用了,具体的使用mongoose对mongodb进行增删改查等操作放在下一篇笔记了。
更多推荐
已为社区贡献1条内容
所有评论(0)