From fc0974dc911e5f7ac09721513e60334558f91550 Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Fri, 22 Dec 2023 00:34:23 +0800 Subject: [PATCH] =?UTF-8?q?mysql=E4=BF=AE=E5=A4=8D=E5=8A=9F=E8=83=BD-?= =?UTF-8?q?=E6=95=8F=E6=84=9F=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/mariadb/index.py | 62 +++++++++++++++++++++++++++++----------- plugins/mysql/index.py | 1 - 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/plugins/mariadb/index.py b/plugins/mariadb/index.py index dd5988e50..2e0c4aaed 100755 --- a/plugins/mariadb/index.py +++ b/plugins/mariadb/index.py @@ -302,21 +302,6 @@ def cleanBinLog(): return mw.returnJson(True, '清理BINLOG成功!') -def setSkipGrantTables(v): - ''' - 设置是否密码验证 - ''' - conf = getConf() - con = mw.readFile(conf) - if v: - if con.find('#skip-grant-tables') != -1: - con = con.replace('#skip-grant-tables', 'skip-grant-tables') - else: - con = con.replace('skip-grant-tables', '#skip-grant-tables') - mw.writeFile(conf, con) - return True - - def getErrorLog(): args = getArgs() path = getDataDir() @@ -1276,16 +1261,59 @@ def setDbAccess(): psdb.where('username=?', (name,)).save('accept,rw', (access, 'rw',)) return mw.returnJson(True, '设置成功!') +def openSkipGrantTables(): + mycnf = getConf() + content = mw.readFile(mycnf) + content = content.replace('#skip-grant-tables','skip-grant-tables') + mw.writeFile(mycnf, content) + return True + +def closeSkipGrantTables(): + mycnf = getConf() + content = mw.readFile(mycnf) + content = content.replace('skip-grant-tables','#skip-grant-tables') + mw.writeFile(mycnf, content) + return True + + +def resetDbRootPwd(version): + serverdir = getServerDir() + myconf = serverdir + "/etc/my.cnf" + pwd = mw.getRandomString(16) + + db_option = "-S " + serverdir + "/mysql.sock" + cmd_pass = serverdir + '/bin/mysql ' + db_option + ' -uroot -e' + cmd_pass = cmd_pass + \ + "\"flush privileges;use mysql;grant all privileges on *.* to 'root'@'localhost' identified by '" + pwd + "';" + cmd_pass = cmd_pass + "flush privileges;\"" + + data = mw.execShell(cmd_pass) + # if data[1].find("ERROR") != -1: + # print("init mariadb password fail:" + data[1]) + # exit(1) + return True def fixDbAccess(version): + + pdb = pMysqlDb() + mdb_ddir = getDataDir() + if not os.path.exists(mdb_ddir): + return mw.returnJson(False, '数据目录不存在,尝试重启重建!') + try: - pdb = pMysqlDb() psdb = pSqliteDb('databases') data = pdb.query('show databases') isError = isSqlError(data) if isError != None: + # 重置密码 appCMD(version, 'stop') - mw.execShell("rm -rf " + getServerDir() + "/data") + openSkipGrantTables() + appCMD(version, 'start') + + resetDbRootPwd(version) + + appCMD(version, 'stop') + closeSkipGrantTables() appCMD(version, 'start') return mw.returnJson(True, '修复成功!') return mw.returnJson(True, '正常无需修复!') diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index 71299d20e..43d72d897 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -1627,7 +1627,6 @@ def fixDbAccess(version): pdb = pMysqlDb() mdb_ddir = getDataDir() - if not os.path.exists(mdb_ddir): return mw.returnJson(False, '数据目录不存在,尝试重启重建!')