74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
import shutil
|
||
import time
|
||
|
||
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("数据库初始化")
|
||
self.__init_db()
|
||
logger.info("安装完成,10秒自动退出")
|
||
time.sleep(10)
|
||
pass
|
||
|
||
def __start_mariadb(self):
|
||
command = 'net start ' + self.service_name
|
||
with os.popen(command) as stream:
|
||
res = stream.read()
|
||
logger.info(res)
|
||
|
||
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 -u root -p{self.passowrd} -P {self.mariadb_port} -e "create database if not exists {self.init_schema}; use {self.init_schema};"'
|
||
with os.popen(command1) as stream:
|
||
res = stream.read()
|
||
logger.info(res)
|
||
|
||
def __init_db(self):
|
||
new_path = get_resource_path(os.path.join('datas', 'init.sql'))
|
||
shutil.copy(str(new_path), self.mariadb_data_path)
|
||
command2 = fr'{self.root_path}\mariadb\bin\mysql --no-defaults -u root -p{self.passowrd} -P {self.mariadb_port} {self.init_schema} < {self.init_sql}'
|
||
with os.popen(command2) as stream:
|
||
res2 = stream.read()
|
||
logger.info(res2)
|
||
os.remove(self.init_sql)
|
||
pass
|
||
|
||
|
||
if __name__ == '__main__':
|
||
current_dir = os.getcwd()
|
||
InstallMariaDb(current_dir).start_install_mariadb()
|
||
pass
|