'Cannot connect to MongoDB Atlas despite allowing all IP address to access
I am unable to access MongoDB database for some reason. I have run through the usual suspects such as removing <> brackets around the password and also making my cluster accessible from any IP address. I am trying to connect to one of the sample databases provided by mongoDB 'sample_mflix', I am not sure if this is an issue. In the tutorial I was referring to https://www.youtube.com/watch?v=n1mdAPFq2Os&t=887s the creator leaves the database as 'test' and it works, but i tried this and failed also. Any help would be much appreciated.
const { ApolloServer } = require('apollo-server');
const gql = require('graphql-tag');
const mongoose = require ('mongoose');
const { MONGODB } = require('./config.js');
const typeDefs = gql`
type Query{
sayHi: String!
}
`;
//each query has resolver
const resolvers = {
Query: {
sayHi: () => 'Hello World!!!'
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
mongoose.connect('mongodb+srv://MYUSERNAME:[email protected]/**sSAMPLEDATABASEgivenbyMongoDB**?retryWrites=true&w=majority', {useNewUrlParser: true})
.then(() => {
console.log('MongoDB connected');
return server.listen({ port: 5000 });
})
.then(res => {
console.log(`Server running at ${res.url}`);
});
Solution 1:[1]
I am not sure what happened but I did nothing and suddenly it works
Solution 2:[2]
If your password contains non-alphanumeric character such as @, #, & you need to make sure you pass the password through encodeURIComponent otherwise mongodb could misinterpret the password the password and therefore reject it.
MYUSERNAME:encodeURIComponent(MYPASSWORD)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Bob Tin |
Solution 2 | yaxx |