Atlas mongoDB cloud service with dart package mongo_dart cause Connection Exception - mongodb

I'm trying to write into Atlas mongoDB cloud service with dart package mongo_dart.
This is how to connect to:
static const String uri =
"mongodb+srv://User:Password#Project.e0nro.mongodb.net/DbName?retryWrites=true&w=majority&tls=true";
Future<Db> connectToDatabase() async {
var db = await Db.create(uri);
await db.open(secure: true);
print('Connected to database');
return db;
}
And this is how to update my data:
await coll.update(
{
"symbol": symbol,
},
{
r"$addToSet": {"pens": pen.toJson()}
},
);
I've got this error rapidly:
Unhandled exception:
MongoDB ConnectionException: connection closed: The socket connection has been reset by peer.
Possible causes:
- Trying to connect to an ssl/tls encrypted database without specifiyng
either the query parm tls=true or the secure=true parameter in db.open()
- Others
And sometimes this one:
Unhandled exception:
MongoDB ConnectionException: connection closed: SocketException: Read failed (OS Error: Connection reset by peer, errno = 104), address = ????.e0nro.mongodb.net, port = 52508
In Atlas panel I added the IP address to access the database globally
0.0.0.0/0 (includes your current IP address) access Active
How can I solve these errors?

Related

Error: Introspection error Error querying the database: Server selection timeout: No available servers. Topology

Error: Introspection error
Error querying the database: Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: <host>:27017, Type: Unknown, Error: failed to lookup address information: nodename nor servname provided, or not known }, ] }
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
DATABASE_URL="mongodb://<username>:<password>#<host>:27017/<database_name>"
npx prisma db pull

Problem connecting to Mongodb Atlas - Error: "querySrv ECONNREFUSED"

I have created a free cluster for mongodb atlas. Under that cluster I have created a database and a collection to test things out. As I was using node js driver 4.0 or above, this is the connection string I got:
mongodb+srv://myName:myPass#cluster0.2sjva.mongodb.net/myDb?retryWrites=true&w=majority
This is my backend code:
const { MongoClient } = require("mongodb");
// Connection URI
const uri =
"mongodb+srv://myName:myPass#cluster0.2sjva.mongodb.net/myDb?retryWrites=true&w=majority";
// Create a new MongoClient
const client = new MongoClient(uri);
async function run() {
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db("myDb").command({ ping: 1 });
console.log("Connected successfully to server");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
But when I run this code, I get this following error:
Error: querySrv ECONNREFUSED _mongodb._tcp.cluster0.2sjva.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:210:19) {
errno: undefined,
code: 'ECONNREFUSED',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.2sjva.mongodb.net'
}
Here is some information I can provide:
I have whitelisted my current Ip address, After this error I used 0.0.0.0. But the problem persists.
Os: Arch linux
I have also tried to use this connection string for older driver version: mongodb://... and that worked.
My question: why connection string for node js driver version > 4.0 doesn't work in my case?
Thank you for reading!

Unknown auth message code 1397113172 when connect to Heroku postgres

Thanks reading my issue.
Currently, I am using postgres (hobby-dev) on Heroku and facing this issue every time that I connect to the database.
error: Uncaught (in promise) Error: Unknown auth message code 1397113172
throw new Error(`Unknown auth message code ${code}`);
^
at Connection.handleAuth (connection.ts:197:15)
at Connection.startup (connection.ts:155:16)
at async Pool._createConnection (pool.ts:32:5)
at async pool.ts:61:7
at async Promise.all (index 0)
at async Pool._startup (pool.ts:63:25)
My application using Deno now
import { Pool } from "https://deno.land/x/postgres/mod.ts";
import { config } from "./config.ts";
const port = config.DB_PORT ? parseInt(config.DB_PORT || "") : undefined;
const POOL_CONNECTIONS = 20;
const dbPool = new Pool({
port,
hostname: config.DB_HOST,
user: config.DB_USER,
database: config.DB_NAME,
password: config.DB_PASS
}, POOL_CONNECTIONS);
export { dbPool };
Here is debug screen.
I have found this issue post and it mentioned about lacking ssl. Not sure how to do it on heroku.
I have tried some solutions, even change lib to pg and it still not work. I am very appreciated if any clue or help to fix this issue.
Note:
I read a document on heroku about "Heroku Postgres Connection Pooling is not available for Hobby-tier databases.". Then I switched to use Client with syntax similar like this to connect to Heroku postgres this:
import { Client } from "https://deno.land/x/postgres/mod.ts";
let config;
config = {
hostname: "localhost",
port: 5432,
user: "user",
database: "test",
applicationName: "my_custom_app"
};
// alternatively
config = "postgres://user#localhost:5432/test?application_name=my_custom_app";
const client = new Client(config);
await client.connect();
await client.end();
ref: https://deno-postgres.com/#/

Unable to connect to postgres using deno.js

Unable to connect to postgres in deno.js.
Here is the configuration:
const dbCreds = {
applicationName: "appname",
user: "user_sfhjwre",
database: "d9iu8mve7nen",
password: "68790f31eelkhlashdlkagsvADSDa52f9d8faed894c037ef6f9c9f09885603",
hostname: "ec2-345-34-97-212.eu-east-1.xx.amazonaws.com",
port: 5432,
};
export { dbCreds };
Usage:
import { Client } from "https://deno.land/x/postgres/mod.ts";
import { dbCreds } from "../config.ts";
const client = new Client(dbCreds);
await client.connect();
Also tried:
config = "postgres://user#localhost:5432/test?application_name=my_custom_app";
const client = new Client(config);
await client.connect();
Same result:
Uncaught Error: Unknown auth message code 1397113172
Is there anything wrong with the syntax, I can connect to the same db using prisma.
I have the PostgreSQL server in a remote server and, each time my public IP changes, I need to change the pg_hba.conf in order to set my new public IP as authorized for remote access.
Hope this helps.
Best regards.

issues connecting to postgres using pg client

Trying to connect to postgres using the pg client (following these instructions).
Here is my connection string var connectionString = "postgres://postgres:pass#localhost/ip:5432/chat";
Here's the error I'm getting trying to connect:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): error: database "ip:5432/twitchchat" does not exist
However, when I run pg_isready I get the response /tmp:5432 - accepting connections Which I read as telling me postgres is running on port 5432.
The database is very definitely existing.
Here's the simple connection code:
var pg = require('pg');
var connectionString =
"postgres://postgres:pass#localhost/ip:5432/chat";
var connection = new pg.Client(connectionString);
connection.connect();
What's happening here? How do I fix this?
Your connection string is malformed:
Change it to:
`"postgres://postgres:pass#localhost:5432/chat"`
The correct format is:
postgresql://[user]:[password]#[address]:[port]/[dbname]
Ok I fixed it with the following:
const { Client } = require('pg');
const connection = new Client({
user: 'postgres',
host: 'localhost',
database: 'chat',
password: 'pass',
port: 5432,
});
connection.connect();