Error: MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27020

244 Views Asked by At

I'm trying to use MongoDB Client-Side Field Level Encryption, using their code sample, but keep getting this error:

MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27020

This is the code I'm using:

const clientEncryption = new ClientEncryption(unencryptedClient, {
      kmsProviders,
      keyVaultNamespace,
    });

// genrate encyption key
const dataKeyId = await clientEncryption.createDataKey("local").then(() =\> {
  console.log("Encyption key genrated");
});

const schemaMap = {
  \[\`${dbName}.${collName}\`\]: {
    properties: {
      sensitiveField: {
        encrypt: {
          keyId: \[dataKeyId\],
          bsonType: "string",
          algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
        },
      },
    },
  },
};

const encryptedClient = new MongoClient(URL, {
  useUnifiedTopology: true,
  autoEncryption: {
    keyVaultNamespace,
    kmsProviders,
    schemaMap,
  },
});

try {
  await encryptedClient.connect();
  const db = encryptedClient.db(dbName);
  const collection = db.collection(collName);

  `const` doc = {
    medicalRecords: "my-sensitive-data",
    sensitiveField: "hsbdb-jd256b",
    bloodType: "O+",
    insurance: "hgvdw35w6-7643",
  };


  const result = await collection.insertOne(doc);
  // const result = await collection.findOne({ bloodType: "O+" });
  // console.log("result: ", result);
  if (result) console.log("data entered successfully");
} catch (error) {
  console.log("Error:", error);
}
1

There are 1 best solutions below

0
On

27020 is a default port for mongocryptd binary. See description in #2 here. However, I would recommend using shared library instead mongocryptd. See my answer here in particular part about cryptSharedLibPath