【发布时间】:2019-12-24 18:12:01
【问题描述】:
我是这里的初学者。
我目前正在设置我的 goorm IDE 并尝试连接 MongoDB Atlas。
但是,我无法将我的 MongoDB Atlas 集群连接到我的 goorm IDE,它显示以下消息:
第一次连接时出现错误,无法连接到服务器 [cluster0-shard-00-00-1kwgi.mongodb.net:27017] [MongoError:错误的身份验证失败。]
按照 Ian Schoonover 的教程,我尝试将 0.0.0.0/0 的 IP 列入白名单。但是,我仍然无法连接我的 MongoDB Atlas。
下面是我在 IDE 中的代码
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
mongoose.connect('mongodb+srv://dylanOh:123456@cluster0-1kwgi.mongodb.net/test?retryWrites=true&w=majority',{
useNewUrlParser : true,
useCreateIndex : true
}).then(()=>{
console.log('Connected to DB!');
}).catch(err=>{
console.log('ERROR',err.message);
});
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'ejs');
//Below is my testing info before setting up the database
const campgrounds =[
{name: 'Shenandoah', image:'https://www.nps.gov/shen/planyourvisit/images/20170712_A7A9022_nl_Campsites_BMCG_960.jpg?maxwidth=1200&maxheight=1200&autorotate=false'},
{name: 'Mount Rainier', image:'https://www.nps.gov/mora/planyourvisit/images/OhanaCampground2016_CMeleedy_01_web.jpeg?maxwidth=1200&maxheight=1200&autorotate=false'},
{name: 'Florida', image:'https://www.visitflorida.com/content/visitflorida/en-us/places-to-stay/campgrounds-florida/_jcr_content/full_width/vf_image.img.1280.500.jpg'}]
app.get('/',(req, res)=>{
res.render('landing');
});
app.get('/campgrounds', (req,res)=>{
res.render('campgrounds', {campgrounds:campgrounds});
});
app.post('/campgrounds', (req,res)=>{
const name=req.body.name ;
const image=req.body.image;
const newCampground = {name:name, image:image}
campgrounds.push(newCampground);
res.redirect('/campgrounds');
});
app.get('/campgrounds/new', (req,res)=>{
res.render('new');
});
app.listen('3000', ()=>{
console.log('YelpCamp has started!');
});
作为预期结果,它应该显示“已连接到数据库!”在我的终端。
但是,在第一次连接时显示“错误无法连接到服务器 [cluster0-shard-00-00-1kwgi.mongodb.net:27017] [MongoError: bad auth Authentication failed.]”。
【问题讨论】:
-
dylanOh 是您的 db 用户,而 123456 是您的 db 用户的密码,而不是您的 Mongo Atlas 帐户的密码吗?
-
哦,是的,他们不是,我的 Mongo Atlas 登录帐户不同,而我只是随机输入的 123456。
-
如果您使用数据库凭据,那么您的数据库用户名或密码错误,您可以再次检查您的数据库凭据或创建一个新的数据库用户
-
嗨@onuriltan,我创建了第二个用户并且数据库成功连接!谢谢你的建议,我认为我的第一个用户没有工作,因为我没有更新我的 mongoDB Atlas 上的密码。
标签: node.js mongoose connection