From 083e8413c8626aa572bff5cd9e3682870c33e89e Mon Sep 17 00:00:00 2001 From: superlollipop <1769128867@qq.com> Date: Fri, 30 Aug 2024 22:26:43 +0800 Subject: [PATCH] fixed mongo connect error (#2760) Co-authored-by: quanbisen --- sql/engines/mongo.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/sql/engines/mongo.py b/sql/engines/mongo.py index b1ededea1b..752cf346ab 100644 --- a/sql/engines/mongo.py +++ b/sql/engines/mongo.py @@ -795,15 +795,25 @@ def execute_check(self, db_name=None, sql=""): def get_connection(self, db_name=None): self.db_name = db_name or self.instance.db_name or "admin" auth_db = self.instance.db_name or "admin" - self.conn = pymongo.MongoClient( - self.host, - self.port, - authSource=auth_db, - connect=True, - connectTimeoutMS=10000, - ) + if self.user and self.password: - self.conn[self.db_name].authenticate(self.user, self.password, auth_db) + self.conn = pymongo.MongoClient( + self.host, + self.port, + username=self.user, + password=self.password, + authSource=auth_db, + connect=True, + connectTimeoutMS=10000, + ) + else: + self.conn = pymongo.MongoClient( + self.host, + self.port, + authSource=auth_db, + connect=True, + connectTimeoutMS=10000, + ) return self.conn def close(self):