76 lines
3.0 KiB
Python
76 lines
3.0 KiB
Python
from Common import *
|
||
|
||
|
||
class InstallMariaDb:
|
||
|
||
def __init__(self, install_path):
|
||
self.service_name = 'ComacMariaDB'
|
||
self.root_path = install_path
|
||
self.mariadb_data_path = os.path.join(install_path, 'mariadb', 'data')
|
||
self.mariadb_port = mariadb_port
|
||
self.passowrd = mariadb_passowrd
|
||
self.init_schema = mariadb_init_schema
|
||
self.init_sql = os.path.join(install_path, 'mariadb', 'data', 'init.sql')
|
||
pass
|
||
|
||
def start_install_mariadb(self):
|
||
existStatus = check_service_exist(self.service_name)
|
||
if existStatus:
|
||
start_service_if_not_running(self.service_name)
|
||
return
|
||
logger.info("开始安装")
|
||
self.__register_service()
|
||
logger.info("启动数据库")
|
||
self.__start_mariadb()
|
||
logger.info("数据库初始化")
|
||
self.__init_db_user()
|
||
logger.info("安装完成,10秒自动退出")
|
||
time.sleep(10)
|
||
pass
|
||
|
||
def __start_mariadb(self):
|
||
args1 = 'net'
|
||
args2 = 'start'
|
||
res = subprocess.run([args1, args2, self.service_name], text=True, capture_output=True)
|
||
logger.info(res.stdout)
|
||
|
||
def __register_service(self):
|
||
args1 = self.root_path + '/mariadb/bin/mysql_install_db.exe'
|
||
args2 = '--datadir=' + self.mariadb_data_path
|
||
args3 = '--service=' + self.service_name
|
||
args4 = '--password=' + self.passowrd
|
||
args5 = '--port=' + str(self.mariadb_port)
|
||
res = subprocess.run([args1, args2, args3, args4, args5], text=True, capture_output=True)
|
||
logger.info(res.stdout)
|
||
return res
|
||
|
||
def __init_db_user(self):
|
||
command1 = fr'{self.root_path}\mariadb\bin\mysql.exe -u root -p{self.passowrd} -P {self.mariadb_port} -e "create database if not exists {self.init_schema}; use {self.init_schema};"'
|
||
command2 = fr'{self.root_path}\mariadb\bin\mysql.exe -u root -p****** -P {self.mariadb_port} -e "create database if not exists {self.init_schema}; use {self.init_schema};"'
|
||
logger.info(command2)
|
||
with os.popen(command1) as stream:
|
||
res = stream.read()
|
||
logger.info(res)
|
||
|
||
schema_dir = fr'{self.root_path}\mariadb\data\{self.init_schema}'
|
||
if not os.path.exists(schema_dir):
|
||
logger.error("数据库初始化失败")
|
||
raise MyCustomError("数据库安装失败")
|
||
|
||
|
||
if __name__ == '__main__':
|
||
current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(
|
||
os.path.abspath(__file__))
|
||
log_dir = os.path.join(current_dir, "logs")
|
||
ensure_dir(log_dir)
|
||
logger.add(
|
||
sink=os.path.join(log_dir, "InstallMariaDB_{time}.log"), # 文件路径模板
|
||
rotation="10 MB", # 文件大小达到10MB时轮转
|
||
retention="1 days", # 保留最近1天的日志
|
||
compression="zip", # 压缩旧日志节省空间
|
||
enqueue=True, # 线程安全写入
|
||
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}" # 自定义格式
|
||
)
|
||
InstallMariaDb(current_dir).start_install_mariadb()
|
||
pass
|