Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "Server is unavailable" with Node >= 17 #77

Open
jer-sen opened this issue Jun 8, 2023 · 1 comment
Open

Error "Server is unavailable" with Node >= 17 #77

jer-sen opened this issue Jun 8, 2023 · 1 comment

Comments

@jer-sen
Copy link

jer-sen commented Jun 8, 2023

Node >= 17 use IPv6 by default, not compatible with MongoDB by default.
This patch can be used to use IPv4:

diff --git a/node_modules/mongodb-topology-manager/lib/utils.js b/node_modules/mongodb-topology-manager/lib/utils.js
index c16f717..1acbbfd 100644
--- a/node_modules/mongodb-topology-manager/lib/utils.js
+++ b/node_modules/mongodb-topology-manager/lib/utils.js
@@ -27,6 +27,10 @@ function cleanupSocket(socket) {
  * @param {function} callback
  */
 function checkAvailable(host, port, callback) {
+  // Use IPv4 since MongoDB does not currently support automatically IPv6
+  require('node:dns').lookup(host, { family: 4 }, (err, ipv4) => {
+    if (err) console.error(err);
+    //console.log(`IPv4: ${ipv4}`);
   const socket = new net.Socket();
 
   socket.on('connect', () => {
@@ -43,7 +47,8 @@ function checkAvailable(host, port, callback) {
     callback(null, false);
   });
 
-  socket.connect({ port: port, host: host });
+  socket.connect({ port: port, host: ipv4 });
+  });
 }
 
 /**

@jer-sen
Copy link
Author

jer-sen commented Jun 12, 2023

Or just add a command line option to bind servers to IPv6 addresses in index.js:

const options = {
         port: port,
         dbpath: isWin ? `${dbPath}\\${port}` : `${dbPath}/${port}`,
-        bind_ip: hostname
+        bind_ip: hostname,
+        ipv6: null,
       };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant