mongo icon indicating copy to clipboard operation
mongo copied to clipboard

Mongo Error on Deno runtime

Open NaderBhrr opened this issue 3 years ago • 9 comments

Hello I have tried to run a project on deno, but I receive the following error when trying to run it using "deno"

error: Uncaught Error: MongoError: "Connection failed: Connection refused (os error 111)"
        throw new MongoError(`Connection failed: ${e.message || e}`);
              ^
    at MongoClient.connect (client.ts:93:15)
    at async db.ts:5:12

Note:

I have tried it several times but the problem still persists.

Thanks in advance

NaderBhrr avatar Jan 30 '21 07:01 NaderBhrr

May I know how do you use it?

hackers267 avatar Jan 30 '21 08:01 hackers267

Expanding upon @hackers267, show your code. It's possible that the hostname/port is incorrect

ebebbington avatar Jan 31 '21 19:01 ebebbington

Thanks for the reply

I have allocated a separate module for database namely db.ts


import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";

const client = new MongoClient();
// await client.connect("mongodb://localhost:27017");
const db = await client.connect("mongodb://localhost:27017/kayran");

export * from "https://deno.land/x/[email protected]/mod.ts";

export default db;

Please note that, I did successfully launched my project but after a few re-run deno stopped creating the server and currently it is showing the above error

Thank you

NaderBhrr avatar Feb 01 '21 06:02 NaderBhrr

If you used os is window. Try replace localhost with 127.0.0.1

hackers267 avatar Feb 02 '21 00:02 hackers267

My OS is "Linux Manjaro" and I can run the project on the "Ubuntu" without any problem

NaderBhrr avatar Feb 02 '21 05:02 NaderBhrr

Make sure mongo is running by using sudo service mongodb start command

borsemayur2 avatar Apr 27 '21 02:04 borsemayur2

Faced the same situation on windows 10 and can confirm that changing localhost to 127.0.0.1 solved the issue @NaderBhrr

jellyninjadev avatar May 10 '21 14:05 jellyninjadev

If you used os is window. Try replace localhost with 127.0.0.1

This trick solved my problem on Ubuntu as well

jacoborus avatar Jun 05 '21 16:06 jacoborus

I have the same problem on ubuntu, but I don't think it's an issue with this driver. When I run Deno.connect({hostname: "<domain name of our database", port: 27017}) I'm getting the same os error 111. Connecting to the ip directly also fixes it for me, and connecting from my local machine to the domain name also works just fine. It turns out this was caused by ipv6 somehow. When I created a digitalocean droplet without ipv6 support it was able to connect to the server again.

edit: Seems like enabling ipv6 in the mongodb configuration file fixed it.

jespertheend avatar Sep 08 '22 12:09 jespertheend