From e173c2e51a1cce1d1d8594b83a8dfb0a740c3503 Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Fri, 22 Dec 2023 00:18:41 +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/mysql/index.py | 88 ++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index 975b0c061..0bbc2ff91 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -405,22 +405,6 @@ def cleanBinLog(): db.execute("PURGE MASTER LOGS BEFORE '" + cleanTime + "';") 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() filename = getErrorLogsFile() @@ -536,7 +520,6 @@ def initMysqlPwd(): pSqliteDb('config').where('id=?', (1,)).save('mysql_root', (pwd,)) return True - def initMysql8Pwd(): time.sleep(8) @@ -1274,7 +1257,7 @@ def syncGetDatabases(): host = user["Host"] break - ps = mw.getMsg('INPUT_PS') + ps = vdb_name if vdb_name == 'test': ps = mw.getMsg('DATABASE_TEST') addTime = time.strftime('%Y-%m-%d %X', time.localtime()) @@ -1589,15 +1572,80 @@ def setDbAccess(): 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) + + pSqliteDb('config').where('id=?', (1,)).save('mysql_root', (pwd,)) + + if float(version) < 5.7: + cmd_pass = serverdir + '/bin/mysql --defaults-file=' + myconf + ' -uroot -e' + cmd_pass = cmd_pass + '"UPDATE mysql.user SET password=PASSWORD(\'' + pwd + "') WHERE user='root';" + cmd_pass = cmd_pass + 'flush privileges;"' + data = mw.execShell(cmd_pass) + # print(data) + else: + auth_policy = getAuthPolicy() + + reset_pwd = 'flush privileges;' + reset_pwd = reset_pwd + \ + "UPDATE mysql.user SET authentication_string='' WHERE user='root';" + reset_pwd = reset_pwd + "flush privileges;" + reset_pwd = reset_pwd + \ + "alter user 'root'@'localhost' IDENTIFIED by '" + pwd + "';" + reset_pwd = reset_pwd + \ + "alter user 'root'@'localhost' IDENTIFIED WITH "+auth_policy+" by '" + pwd + "';" + reset_pwd = reset_pwd + "flush privileges;" + + tmp_file = "/tmp/mysql_init_tmp.log" + mw.writeFile(tmp_file, reset_pwd) + cmd_pass = serverdir + '/bin/mysql --defaults-file=' + myconf + ' -uroot -proot < ' + tmp_file + + data = mw.execShell(cmd_pass) + # print(data) + os.remove(tmp_file) + return True + def fixDbAccess(version): + + pdb = pMysqlDb() + mdb_ddir = getDataDir() + + if not os.path.exists(mdb_ddir): + return mw.returnJson(False, '数据目录不存在,尝试重启重建!') + try: - pdb = pMysqlDb() 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, '正常无需修复!') except Exception as e: