简体   繁体   English

为什么我无法连接到 mongoDB 图集?

[英]Why can't I connect to mongoDB atlas?

I am new in MongoDB, all my life I used MySQL.我是 MongoDB 的新手,我一生都在使用 MySQL。

I have created an account in atlas, set the IP to my IP and created a user and saved the password.我在 atlas 中创建了一个帐户,将 IP 设置为我的 IP 并创建了一个用户并保存了密码。

here is my code, why doesn't it work?这是我的代码,为什么它不起作用?

app.js应用程序.js

 const express = require('express'); const bodyParser = require('body-parser'); const mongoPractice = require('./mongo'); const app = express(); app.use(bodyParser.json()); app.post('/products', mongoPractice.createProduct); app.get('/products'); app.listen(3000);

and the mongo.js:和 mongo.js:

 const MongoClient = require("mongodb").MongoClient; const url = "mongodb+srv://idan:<85IwoSzeQssHMzLN>@cluster0.tpejv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"; const createProduct = async (req, res, next) => { const newProduct = { name: req.body.name, price: req.body.price, }; const client = new MongoClient(url); try { await client.connect(); const db = client.db(); const result = db.collection("products").insertOne(newProduct); } catch (error) { return res.json(error); } client.close(); res.json(newProduct); }; const getProducts = async (req, res, next) => {}; exports.createProduct = createProduct; exports.getProducts = getProducts;

the POSTMAN output: POSTMAN output:

在此处输入图像描述

Your ip may have changed, (check if the current ip address has information "(includes your current IP address)" . For testing(.) you can add address 0.0.0.0/0 to the whitelist - it means every ip will be accepted - this solution is good for beginners Your ip may have changed, (check if the current ip address has information "(includes your current IP address)" . For testing(.) you can add address 0.0.0.0/0 to the whitelist - it means every ip will be accepted - 这个解决方案适合初学者

  1. Firstly check you connection link from mongodb connect首先检查您从 mongodb 连接的连接链接
  2. Check username, password again再次检查用户名、密码
  3. You can change password and try again您可以更改密码并重试

In mongo.js在 mongo.js

You need to remove "< >" around the password.您需要删除密码周围的“< >”。

const url = "mongodb+srv://idan:**85IwoSzeQssHMzLN**@cluster0.tpejv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";

I encountered the same error once and I might have solution.我曾经遇到过同样的错误,我可能有解决方案。

The most common one is that your IP address set to access the database might not match with your current IP address in which case you need to set it to your current IP or set to allow access from anywhere.最常见的是您设置为访问数据库的 IP 地址可能与您当前的 IP 地址不匹配,在这种情况下,您需要将其设置为您当前的 IP 或设置为allow access from anywhere.

The issue which I had : If you have recently started using an ethernet cable try going back to wireless to access the mongoDB database from your backend script.我遇到的问题:如果您最近开始使用ethernet cable ,请尝试back to wireless以从后端脚本访问 mongoDB 数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM