Unable to connect to atlas mongo db from strapi - node.js

I am using strapi and Mongo DB.
Am able to connect to mlab Mongo DB, but not to atlas clustered Mongo DB.
I am using following configuration in strapi config :
{
"defaultConnection": "default",
{
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"uri": "mongodb://<username>:<pwd>#mydb-shard-00-00-abcde.mongodb.net:27017,my-db-shard-00-01-abcde.mongodb.net:27017,mydb-shard-00-02-abcde.mongodb.net:27017/mydb?ssl=true&replicaSet=mydb-shard-0&authSource=admin",
},
"options": {"ssl":true}
}
}
}
Updated
It is working as expected there was mismatch in the spelling of clusters. For one of the clusters there is capital letter.

If you just start with new project you just need to do run
yarn create strapi-app my-strapi
then Choose your installation type
Custom (manual settings)Choose your installation type Custom (manual
settings)
Choose your main database:
SQLite
❯ Mongo
MySQL
Postgres
Database name: (my-strapi) (shows project name by default; hit enter)
Host: myproject-fcvyt.mongodb.net
+srv connection: true
Port (It will be ignored if you enable +srv): 27017 (do nothing, simply hit enter)
Username:<mongodb atlas cluster username>
Password:<mongodb atlas cluster password>
Authentication database (Maybe "admin" or blank): (leave blank hit enter)
Enable SSL connection: true

MongoDB atlas uri should include +srv. E.g., "mongodb+srv://<username>:<pwd>..."
*this is assuming you are using the node.js driver and MongoDB 3.6. Full doc here.

for reference these are the options that worked for me
Choose your default database client mongo
? Database name: server
? Host: cluster0.qcyvc.mongodb.net
? +srv connection: true
? Port (It will be ignored if you enable +srv): 27017
? Username: petros
? Password: ********
? Authentication database (Maybe "admin" or blank):
? Enable SSL connection: Yes

Related

Cannot connect to mongodb with strapi

PS C:\Users\mihir\Desktop\node leraning\strapi_practice> npx create-strapi-app first
npx: installed 92 in 92.729s
Creating a new Strapi application at C:\Users\mihir\Desktop\node leraning\strapi_practice\first.
? Choose your installation type
? Choose your installation type Custom (manual settings)
? Choose your default database client
? Choose your default database client mongo
? Database name: learn
? Database name: learn
? Host: cluster_link {useNewUrlParser: true, useUnifiedTopology: true}
? Host: cluster_link {useNewUrlParser: true, useUnifiedTopology: true}
? +srv connection: (false) true
? +srv connection: true
? Port (It will be ignored if you enable +srv): (27017)
? Port (It will be ignored if you enable +srv): 27017
? Username: mihir
? Username: mihir
? Password: ****************
? Password: ****************
? Authentication database (Maybe "admin" or blank):
? Authentication database (Maybe "admin" or blank):
? Enable SSL connection: (y/N) y
? Enable SSL connection: Yes
⛔️ Connection test failed: EPERM: operation not permitted, lstat 'C:\Users\mihir\AppData\Local\Temp\strapiff50605eed47\node_modules\strapi-connector-mongoose\node_modules\.bin'
I wrote a command to create a new strapi app with custom settings.I answered the custom setting questions in above manner.When i choose database as mongo I also passed {useNewUrlParser: true, useUnifiedTopology: true}.But at last it is showing connection test failed.So i am unable to connect with my mongo.Please suggest some solution.
I think that You should start mongodb first then start with installation

Connect to SQL Server via Hamachi VPN - SSMS works, Node doesn't

I can connect fine with SQL Server Management Studio, but not with Node.
I am using the mssql npm package.
Here's the node connection.
db: {
server: "123.456.789.255\\WEBSQL",
database: "db_name",
user: "user",
password: "pass"
}
I am getting a ETIMEOUT error.
ConnectionError: Failed to connect to 123.456.789.255\\WEBSQL in 15000ms
Can anyone point me to the right direction?
Adding this fixed the problem.
options: {
encrypt: false,
},

Aurora Postgres Connection Timeout Nodejs

This was working the last time I looked at it. The code remains unchanged.
I have an SSH tunnel through an ec2 instance to an Aurora Postgres DB. I can connect to it through psql. I can also connect to it through Datagrip. I'm able to run queries.
But using the exact same connection settings through nodejs I get a connection timeout. The connection settings are exactly the same as the ones I'm using in psql and Datagrip.
pool = new Pool({
user: "myuser",
host: "localhost",
database: 'somedb',
password: "password",
port: 1234
});
pool.query('SELECT column1, column2 FROM test ORDER BY id ASC', (error, results) => {
if (error) {
response.status(400).send(`Error: ${error.message}`);
return;
}
response.status(200).json(results.rows);
})
The same code works on the server directly. It just no longer works when trying to connect via ssh tunnel. (although it used to work)
The problem was the version of the pg npm package. I updated to a newer package and the connection started working again.

Error connecting to the Mongo database. bad auth Authentication failed heroku + atlas mongodb + strapi

I might not have enough code here to provide.
I am currently using strapi and running my app on heroku + atlas mongodb. Everything was working fine when I was running the app with postgresql by heroku but I am migrating everything to atlas mongodb instead.
in strapi I have my config as
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-hook-mongoose",
"settings": {
"client": "mongo",
"host": "${process.env.DATABASE_HOST}",
"port": "${process.env.DATABASE_PORT}",
"database": "${process.env.DATABASE_NAME}",
"username": "${process.env.DATABASE_USERNAME}",
"password": "${process.env.DATABASE_PASSWORD}"
},
"options": {
"useNullAsDefault": true,
"ssl": "${process.env.DATABASE_SSL}"
}
}
}
}
these configs are in .env which if running locally the file would look like
DATABASE_HOST=xxxxxxx-shard-00-00-xxxxxx.mongodb.net:25758,xxxxxxx-shard-00-01-xxxxxx.mongodb.net:25758,xxxxxxx-shard-00-02-xxxxxx.mongodb.net:25758/test?ssl=true&replicaSet=xxxxxxx-shard-0&authSource=admin&retryWrites=true
DATABASE_PORT=25758
DATABASE_USERNAME=xxxxx
DATABASE_PASSWORD=xxxxx
DATABASE_NAME=xxxxx
DATABASE_SSL=true
if I run it locally, it works perfectly and able to connect to DB.
so I deployed to heroku and went to heroku's setting to config vars. I configured the same vars as what I have in the .env file
I also checked atlas mongodb's ip whitelist, I whitelist to all traffics (any ip) so this shouldn't be the problem.
But as mentioned, I used the same .env locally and config vars on heroku....but on heroku I got this error message
error Error connecting to the Mongo database. bad auth Authentication failed.
Anyone has suggestions on the reason authentication would fail?
Thanks in advance for any suggestions and help.
DATABASE_SSL=true Environment Variables don't support boolean values, meaning it's passing that as a string. For now you will need to manually set that value in your config and not pass it over env in heroku.

getting ReplicaSetNoPrimary and MongoServerSelectionError error while connecting MongoDB with nodejs

I am trying to connect to mongodb but getting below error could you please help
var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://usernamexyz:passwordxyz#hostmxy-mw-e6-u1238.nam.nsroot.net:47017/sampleDB?replicaSet=NAME_2436&readPreference=primary&authSource=admin&w=1', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Mongodb connected"))
.catch(err => console.log(err));
And the error i am getting as below
MongoServerSelectionError: connection <monitor> to 155.30.360.129:37017 closed
at Timeout._onTimeout (C:\Fintech\NodeFirstApp\node_modules\mongodb\lib\core\sdam\topology.js:448:30)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
name: 'MongoServerSelectionError',
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map {
'hostmxy-mw-e6-u1238.nam.nsroot.net:47017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
},
[Symbol(mongoErrorContextSymbol)]: {}
}
Go to Mongodb where you created a cluster and under "ipwhitelist" click on the edit IP address button and then choose "Add current IP address"
There was a firewall issue for me, and here are the steps i followed:
Search for Windows Firewall with Advanced Security
Select Outbound Rules
Select New Rule
Select Port in Rule Type, Click next
Select TCP and Specific remote ports and write in there 27015-27017
Save the rule
This worked for me :)
I setup replica set on mongod server in order to have oplog facility and use it to sync database programmatically with golang client.
It was working fine with local windows server.
mongod.exe --dbpath $dpath --bind_ip 0.0.0.0 --port $port --replSet rs0
mongo.exe' -port $port --eval 'rs.initiate()'
When I used docker it showed me that ReplicaSetNoPrimary Error.
docker run --name mongo21 -d -p 27021:27017 mongo --bind_ip_all --replSet rs0
docker container exec mongo21 mongosh --eval 'rs.initiate()' 127.0.0.1
I noticed that perl client as well as Robo3 T, mongosh connected to the server localhost:27021 without problem.
mongosh shows which mongo uri it uses:
mongodb://127.0.0.1:27017/test?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.1.9
I found I can specify client option before connect:
clientOpts := options.Client().ApplyURI(uri).SetDirect(true)
client, err := mongo.Connect(ctx, clientOpts)
That option fixed golang client connection issue.
Some MongoDB implementations as a service such as IBM Cloud, GCP or AWS might need a certificate to be able to connect. That was my case. The problem was resolved when I added a certificate to the docker file and then passed the parameters as options
let options = {
tls: true,
tlsCAFile: `/path/to/cert`,
useUnifiedTopology: true
};
// connects to a MongoDB database
MongoClient.connect(connectionString, options)
Reference https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-mongodb-external-app
I was also getting the same error while connecting to MongoDB atlas.
You have to do following things.
1. allow outbound port 27015-27017 in firewall in your system.
2. update the latest MongoDB and NodeJs.
3. whitelist your system IP in MongoDB atlas cluster under network access.
It worked for me. I hope it works for you also.
Updating "Network Access" did not work for me. What worked for me was to update "Database Access" by "Autogenerate Secure Password" and make sure to click "Update User" to save new password!
MongoDB Atlas > Database Access > complete 3 steps in image
I had the same issue with this. It was fixed when I added the current IP address to IP whitelist inside of MongoDB.
I was using Atlas Mongo Cluster and using 'mongomirror' I am trying to push the data from one replicaset cluster to another. In the process, I was getting this error..
After ot of research, I understood that, the name of cluster visible in UI should not be used.. instead you need to run the below command
rs.status()
This will give a JSON output which has the RS name and respective primary and secondary node endpoints you need to use.
In my case, for abc-temp-shard-cluster, the cluster name I got by running the above command was something like below
atlas-12avca-shard-0/atlas-12avca-shard-00-00.1aqer5.mongodb.net:27017,atlas-12avca-shard-00-01.1aqer5.mongodb.net:27017,atlas-12avca-shard-00-02.1aqer5.mongodb.net:27017
try to enable ipv6 support at mongo server using --ipv6 flag
mongod --dbpath="D:/data_path" -replSet rs0 --bind_ip 0.0.0.0,127.0.0.1,localhost,::1 --ipv6

Resources