I have encountered an error while installing the MongoDB Plugin on aaPanel and have completed the fix; it may help a few people in the future.
REQUEST_DATE: 2025-08-01 21:27:57
PAN_VERSION: 7.0.22
OS_VERSION: Ubuntu 22.04.5 LTS x86_64(Py3.12.3)
REMOTE_ADDR: 1.52.0.51
REQUEST_URI: POST /v2/plugin%3Faction%3Da%26amp%3Bname%3Dmongodb%26amp%3Bs%3Dget_databases
REQUEST_FORM: {}
USER_AGENT: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
THE SIMPLEST WAY IS TO ACCESS THE FILE:
mongodb_main.py at
/www/server/panel/plugin/mongodb/mongodb_main.py
And replace it with the following content..
`#coding: utf-8
+-------------------------------------------------------------------
| aaPanel
+-------------------------------------------------------------------
| Copyright (c) 2015-2099 aaPanel(www.aapanel.com) All rights reserved.
+-------------------------------------------------------------------
+-------------------------------------------------------------------
import json
import os
import re
import public
from databaseModelV2.mongodbModel import panelMongoDB as mgdb
public.sys_path_append('/www/server/panel/class')
public.sys_path_append('/www/server/panel/class_v2')
class mongodb_main:
__mongodb_path = '/www/server/mongodb'
#获取数据库列表
def get_databases(self, get):
try:
db_client = mgdb().get_db_obj('admin')
if not isinstance(db_client, str):
return db_client.command('listDatabases')
else:
raise Exception(db_client)
except Exception as e:
raise Exception(public.lang("MongoDB connection fail, Please check the configuration information! error: %s" % str(e)))
#获取服务状态
def get_service_status(self, get):
pidFile = self.__mongodb_path + '/log/configsvr.pid'
pid = int(public.readFile(pidFile))
import psutil
if pid in psutil.pids():
return True
return False
#取负载状态
def get_status(self, get):
tmp = public.ExecShell('mongo admin --eval "db.serverStatus()"|grep -v MongoDB|grep -v connecting')[0]
tmp = tmp.strip().replace("NumberLong(","").replace(")}","}").replace("),",",").replace("ISODate(","").replace("\n","").replace("\t","").replace(")","")
return json.loads(tmp)
#创建数据库
def create_database(self, get):
public.ExecShell('mongo '+get.dbname+' --eval \'db.'+get.dbname+'.insert({"name":"'+get.dbname+'"})\'')
public.ExecShell('mongo '+get.dbname+' --eval \'db.'+get.dbname+'.remove({"name":"'+get.dbname+'"})\'')
return public.returnMsg(True,'CREATE_SUCCESS')
#删除数据库
def remove_database(self, get):
sysdb = ['admin','config','local']
if get.dbname in sysdb: return public.returnMsg(False,'SYS_DB_CANT_DEL');
public.ExecShell('mongo '+get.dbname+' --eval \'db.dropDatabase()\'')
return public.returnMsg(True,'DEL_SUCCESS')
#获取配置文件
def get_config(self, get):
filename = self.__mongodb_path + '/config.conf'
if os.path.exists(filename):
return public.readFile(filename)
return ""
#保存配置文件
def save_config(self, get):
public.writeFile(self.__mongodb_path + '/config.conf',get.config_body)
get.status = '2'
self.service_admin(get)
return public.returnMsg(True,'SAVE_SUCCESS')
#获取日志文件
def get_logs(self, get):
filename = self.__mongodb_path + '/log/config.log'
if os.path.exists(filename):
return public.GetNumLines(self.__mongodb_path + '/log/config.log',1000)
return public.GetMsg("LOG_EMPTY")
# 获取配置项
def get_options(self, get):
options = ['port', 'bindIp', 'path', 'dbPath', 'pidFilePath', 'authorization']
data = {}
conf = self.get_config(None)
for opt in options:
tmp = re.findall(opt + ":\s+(.+)", conf)
if not tmp: continue;
data[opt] = tmp[0]
if not 'authorization' in data:
data['authorization'] = "disabled"
return data
#保存配置项
def save_options(self, get):
options = ['port','bindIp','path','pidFilePath']
conf = self.get_config(None)
for opt in options:
conf = re.sub(opt + ":\s+(.+)",opt + ": " + get[opt],conf)
if opt == 'dbPath':
if get[opt][-1] == "/":
get[opt] = get[opt][:-1]
# if not "bt_mongodb" in get[opt] and get[opt] != "/www/server/mongodb/data":
# conf = re.sub(opt + ":\s+(.+)", opt + ": " + get[opt]+"/bt_mongodb/", conf)
# os.system("mkdir -p {}".format(get[opt]+"/bt_mongodb/"))
# os.system("chown mongo.mongo {}".format(get[opt]+"/bt_mongodb/"))
filename = self.__mongodb_path + '/config.conf'
public.writeFile(filename,conf)
get.status = '2'
self.service_admin(get)
return public.returnMsg(True,'SAVE_CONF')
#服务管理
def service_admin(self, get):
statusOption = {"0": "stop", "1": "start", "2": "restart"}
statusString = statusOption[get.status]
public.ExecShell('/etc/init.d/mongodb ' + statusString)
return public.returnMsg(True,'OPERATE_SUCCESS')
`