Issues connecting to cluster to start database server - node.js

I'm follow this guide to setting up an app with a data base
(https://www.mongodb.com/languages/mern-stack-tutorial).
I'm at the step where I have everything installed and an Atlas account set up. I'm trying to connect to my database from VS Code but every time I try it it gives me this
code: 8000,
codeName: 'AtlasError',
connectionGeneration: 0,
[Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
All the links that I have looked for related issues on AtlasError or error code 8000s would tell me to not include the <> that is put in with the and in the Atlas URI
mongodb+srv://<username>:<password>#sandbox.jadwj.mongodb.net/employees?retryWrites=true&w=majority. Other suggestions have been to use the username and password that has access to the database rather than the account which have been made certain of.
I tried looking up error labels in the documentation but I don't see anything.
This is the whole output.
Server is running on port: 5000
MongoServerError: bad auth : Authentication failed.
at Connection.onMessage (C:\Users\Alexander Chea\Desktop\Starz\mern\server\node_modules\mongodb\lib\cmap\connection.js:227:30)
at MessageStream.<anonymous> (C:\Users\Alexander Chea\Desktop\Starz\mern\server\node_modules\mongodb\lib\cmap\connection.js:60:60)
at MessageStream.emit (node:events:513:28)
at processIncomingData (C:\Users\Alexander Chea\Desktop\Starz\mern\server\node_modules\mongodb\lib\cmap\message_stream.js:125:16)
at MessageStream._write (C:\Users\Alexander Chea\Desktop\Starz\mern\server\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Writable.write (node:internal/streams/writable:337:10)
at TLSSocket.ondata (node:internal/streams/readable:766:22)
at TLSSocket.emit (node:events:513:28) {
ok: 0,
code: 8000,
codeName: 'AtlasError',
connectionGeneration: 0,
[Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
Any help would be appreciated, thanks.

Try to remove employees from the URL path because Atlas already knows which database are you using

Related

Problems implementing reconnection strategy for mongodb using mongoose on a replica set

I have an experimental app that uses a NodeJS backend which connects to a mongodb instance using mongoose. Mongodb is configured to run as single-node replica set to give me access to change streams which I use for real-time updates over a websocket connection.
For several reasons I'd like to be able to start the backend without the DB service running and have it connect as soon as it becomes available (primary reason: Docker startup). Also, I would like it to reconnect if the db server becomes unavailable for some reason.
A promising fix for reconnecting to mongodb seems to be https://stackoverflow.com/a/41859984/8447743. However, it does not seem to work in my case. It does work for delayed connection (mongod service starting after app), but gives an error on reconnection:
C:\Users\<Path to app>\node_modules\mongoose\node_modules\mongodb\src\cmap\connection.ts:740
callback(new MongoServerError(document));
^
MongoServerError: Cannot run getMore on cursor 8780071429437342673, which was created in session 993f2cbd-db02-4565-af56-f0c9a88ba012 - 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=, without an lsid
at MessageStream.messageHandler (C:\Users\<Path to app>\node_modules\mongoose\node_modules\mongodb\src\cmap\connection.ts:740:20)
at MessageStream.emit (node:events:526:28)
at MessageStream.emit (node:domain:475:12)
at processIncomingData (C:\Users\<Path to app>\node_modules\mongoose\node_modules\mongodb\src\cmap\message_stream.ts:167:12)
at MessageStream._write (C:\Users\<Path to app>\node_modules\mongoose\node_modules\mongodb\src\cmap\message_stream.ts:64:5)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at Socket.ondata (node:internal/streams/readable:754:22)
at Socket.emit (node:events:526:28) {
ok: 0,
code: 50737,
codeName: 'Location50737',
'$clusterTime': {
clusterTime: Timestamp { low: 2, high: 1650366308, unsigned: true },
signature: { hash: [Binary], keyId: 0 }
},
operationTime: Timestamp { low: 2, high: 1650366308, unsigned: true }
}
[nodemon] app crashed - waiting for file changes before starting...
According to https://www.mongodb.com/docs/manual/reference/command/lockInfo/ I might able to get this lsid that seems to be missing, but I haven't found a way to make that known to mongoose for reconnecting. Is that possible somehow? Is there another way to avoid the error?

Truffle migrate --network bsc error: header not found

When trying to run truffle migrate --network bsc, truffle usually (not always) manages to deploy the migrations contract, then fails with an error: header not found.
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({ code: -32000, message: 'header not found' })
at new NodeError (node:internal/errors:363:5)
at Web3ProviderEngine.emit (node:events:354:17)
at D:\Contracts\novaria\node_modules\web3-provider-engine\index.js:54:14
at afterRequest (D:\Contracts\novaria\node_modules\web3-provider-engine\index.js:148:21)
at D:\Contracts\novaria\node_modules\web3-provider-engine\index.js:174:21
at D:\Contracts\novaria\node_modules\web3-provider-engine\index.js:232:9
at D:\Contracts\novaria\node_modules\async\internal\once.js:12:16
at replenish (D:\Contracts\novaria\node_modules\async\internal\eachOfLimit.js:61:25)
at D:\Contracts\novaria\node_modules\async\internal\eachOfLimit.js:71:9
at eachLimit (D:\Contracts\novaria\node_modules\async\eachLimit.js:43:36)
at D:\Contracts\novaria\node_modules\async\internal\doLimit.js:9:16
at end (D:\Contracts\novaria\node_modules\web3-provider-engine\index.js:211:5)
at Request._callback (D:\Contracts\novaria\node_modules\web3-provider-engine\subproviders\rpc.js:70:28)
at Request.self.callback (D:\Contracts\novaria\node_modules\request\request.js:185:22)
at Request.emit (node:events:365:28)
at Request.<anonymous> (D:\Contracts\novaria\node_modules\request\request.js:1154:10)
at Request.emit (node:events:365:28)
at IncomingMessage.<anonymous> (D:\Contracts\novaria\node_modules\request\request.js:1076:12)
at Object.onceWrapper (node:events:471:28)
at IncomingMessage.emit (node:events:377:35)
at endReadableNT (node:internal/streams/readable:1312:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Here's the config for bsc network:
bsc: {
provider: () => { return new HDWalletProvider(mnemonic, `https://bsc-dataseed2.binance.org/`)},
network_id: 56,
confirmations: 10,
timeoutBlocks: 200,
skipDryRun: true,
},
compilers: {
solc: {
version: "0.8.7", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {
enabled: true,
runs: 200
},
Deploying to testnet and development works without issue. I have in the past deployed to bsc with truffle (been a while though). I've tried changing RPC urls, and messed around with timeout and confirmations (pretty sure that doesn't make a difference for this error). After searching the internet for solutions, the only answer that seems to have worked for people is to change the RPC, but I haven't had any luck with that. Does anyone have any suggestions?
I had the same problem today. Fixed it by using the Websocket endpoint wss://bsc-ws-node.nariox.org:443 from the smart chain docs https://docs.binance.org/smart-chain/developer/rpc.html

CentOS + Node.js (v8.15.0) + Sequelize (v4.38.0) to Azure SQL Intermittent Connection Issues

On our production Azure Hosted CentOS API server we are having intermittent Sequelize connection issues to our Azure SQL database.
Our connection settings are as follows:
const sequelize = new Sequelize(dbDatabase, dbUser, dbPassword, {
host: dbHost,
dialect: 'mssql',
operatorsAliases: false,
pool: {
max: 5092,
min: 0,
acquire: 10000,
idle: 10000,
evict: 500,
},
dialectOptions: {
encrypt: false, // Use this if you're on Windows Azure
requestTimeout: 60000 * 2,
},
}
We have also set the ulimit for open files by the process to the system max.
We are using PM2 to run the server.
The two errors that sometimes appear in our logs roughly every 10-20 minutes are:
SequelizeConnectionError
at Connection.connection.on (/home/AZ-admin/XXX/node_modules/sequelize/lib/dialects/mssql/connection-manager.js:75:16)
at emitNone (events.js:106:13)
at Connection.emit (events.js:208:7)
at Connection.cleanupConnection (/home/AZ-admin/XXX/node_modules/tedious/lib/connection.js:568:16)
at Connection.enter (/home/AZ-admin/XXX/node_modules/tedious/lib/connection.js:1961:12)
at Connection.transitionTo (/home/AZ-admin/XXX/node_modules/tedious/lib/connection.js:993:26)
at Connection.socketEnd (/home/AZ-admin/XXX/node_modules/tedious/lib/connection.js:1036:12)
at Socket.<anonymous> (/home/AZ-admin/XXX/node_modules/tedious/lib/connection.js:877:18)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
or
SequelizeHostNotFoundError: Failed to connect to xxx-prod.database.windows.net:1433 - getaddrinfo ENOTFOUND xxx-prod.database.windows.net
at Connection.connection.on.err (/home/XXX/node_modules/sequelize/lib/dialects/mssql/connection-manager.js:98:22)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.socketError (/home/XXX/node_modules/tedious/lib/connection.js:1016:14)
at /home/XXX/node_modules/tedious/lib/connection.js:861:25
at GetAddrInfoReqWrap.callback (/home/XXX/node_modules/tedious/lib/connector.js:69:18)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:79:17)
We think this could anything from an issue with connections/files being opened and not closed and reaching a limit to a problem with our DNS looking up the domain name.
It does appear to be somewhat tied to an increase in traffic, but the correlation is not 100% clear.
I saw your sample code missing options in dialectOptions.
It should be like below.
"dialectOptions": {
options: {
encrypt: true,
}
}
Before I have create a sample project to connect mssql and mysql. Hope it can help you. For more details, please check my answer in below post.
Azure Database for MySQL - webapp nodejs

Firebase Auth testing with jest and emulator calling the login throws Error: Headers X-Client-Version forbidden

I try to login a dummy user with the firebase js sdk localy. I have the default firebase emulator running. After calling the function i get following exception:
Error: Headers X-Client-Version forbidden
at dispatchError (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:62:19)
at validCORSPreflightHeaders (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:99:5)
at Request.<anonymous> (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:367:12)
at Request.emit (events.js:315:20)
at Request.onRequestResponse (C:\Users\user\Documents\Projekte\Backend\functions\node_modules\request\request.js:1059:10)
at ClientRequest.emit (events.js:315:20)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:641:27)
at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
at Socket.socketOnData (_http_client.js:509:22)
at Socket.emit (events.js:315:20) undefined
at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:65:53)
at validCORSPreflightHeaders (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:99:5)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:367:12)
at Request.onRequestResponse (node_modules/request/request.js:1059:10)
console.log
t {
code: 'auth/network-request-failed',
message: 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.',
a: null
}
at Object.<anonymous> (test/test.ts:275:12)
If i try to connect to my online project it works fine, but i want to perform my testing localy with the emulator.
Example Code:
const app = firebase.initializeApp(firebaseConfig);
app.auth().useEmulator("http://localhost:9099");
app.firestore().settings({
host: "localhost:8080",
ssl: false,
});
test('Example test case', async () => {
try {
const cred: UserCredential = await app.auth().signInWithEmailAndPassword("foo#bar.de", "bla2377");
expect(cred).toBeTruthy();
expect(cred.user).toBeTruthy();
} catch (e) {
console.log(e);
expect(true).toBeFalsy();
}
});
Enviorment Informations:
Operating System version: Windows 10 Home 10.0.18363 Build 18363
Firebase SDK version: 8.2.3
Jest Version: 26.6.3
Node Version: 14
jsdom doesn't support wildcard access-control-allow-headers, which are used by firebase. This answer fixes the exception.

LoopbackJS version 3.x `Connection fails: MongoError: Authentication failed.`

I am trying to follow up the tutorial, I created a new app with lb, added CoffeeShop mode, then added a datasource, mongodb.
My MongoDB instance is on my local mac, and authorization is turned off.
I just run it with mongod command, no extra params, and there are no addition configs.
this is my datasources.json
{
"corp1": {
"host": "127.0.0.1",
"port": 27017,
"url": "",
"database": "devdb",
"password": "devpassword",
"name": "corp1",
"user": "devuser",
"connector": "mongodb"
}
}
I even created devdb database, and given devdb user to all databases as admin.
And still getting this error.
Connection fails: MongoError: Authentication failed.
It will be retried for the next request.
/Users/hazimdikenli/learn/loopback-getting-started/node_modules/mongodb/lib/mongo_client.js:462
throw err
^
MongoError: Authentication failed.
at Function.MongoError.create (/Users/hazimdikenli/learn/loopback-getting-started/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/hazimdikenli/learn/loopback-getting-started/node_modules/mongodb-core/lib/connection/pool.js:497:72
at authenticateStragglers (/Users/hazimdikenli/learn/loopback-getting-started/node_modules/mongodb-core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/Users/hazimdikenli/learn/loopback-getting-started/node_modules/mongodb-core/lib/connection/pool.js:477:5)
at Socket.<anonymous> (/Users/hazimdikenli/learn/loopback-getting-started/node_modules/mongodb-core/lib/connection/connection.js:333:22)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:597:20)
It am thinking that this is a newbie issue but I cannot find the problem. So please help me resolve this issue.
Although I am not running mongod with --auth option, it still turned out to be an authentication issue, it worked after I executed following.
db.createUser({
user : "devuser",
pwd : "devpassword",
roles: [ {role: "dbOwner", db: "devdb"},
{role: "readWrite", db: "devdb"}]
})
before this, I granted roles on "admin" with this script, but looks like it was not enough.
role: "userAdminAnyDatabase", db: "admin"

Resources