diff --git a/plugins/mysql/index.html b/plugins/mysql/index.html index 861b3d959..381ae98cf 100755 --- a/plugins/mysql/index.html +++ b/plugins/mysql/index.html @@ -6,16 +6,18 @@

配置文件

端口

当前状态

+

性能优化

慢日志

+

管理列表

-
-
+
\ No newline at end of file diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index f8c72ac99..14fa7600a 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -282,6 +282,49 @@ def runInfo(): result['Position'] = 'OFF' return public.getJson(result) + +def myDbStatus(): + result = {} + db = pMysqlDb() + data = db.query('show variables') + gets = ['table_open_cache', 'thread_cache_size', 'query_cache_type', 'key_buffer_size', 'query_cache_size', 'tmp_table_size', 'max_heap_table_size', 'innodb_buffer_pool_size', + 'innodb_additional_mem_pool_size', 'innodb_log_buffer_size', 'max_connections', 'sort_buffer_size', 'read_buffer_size', 'read_rnd_buffer_size', 'join_buffer_size', 'thread_stack', 'binlog_cache_size'] + result['mem'] = {} + for d in data: + for g in gets: + if d[0] == g: + result['mem'][g] = d[1] + if result['mem']['query_cache_type'] != 'ON': + result[ + 'mem']['query_cache_size'] = '0' + return public.getJson(result) + + +def setDbStatus(): + gets = ['key_buffer_size', 'query_cache_size', 'tmp_table_size', 'max_heap_table_size', 'innodb_buffer_pool_size', 'innodb_log_buffer_size', 'max_connections', 'query_cache_type', + 'table_open_cache', 'thread_cache_size', 'sort_buffer_size', 'read_buffer_size', 'read_rnd_buffer_size', 'join_buffer_size', 'thread_stack', 'binlog_cache_size'] + emptys = ['max_connections', 'query_cache_type', + 'thread_cache_size', 'table_open_cache'] + args = getArgs() + conFile = getConf() + content = public.readFile(conFile) + n = 0 + for g in gets: + s = 'M' + if n > 5: + s = 'K' + if g in emptys: + s = '' + rep = '\s*' + g + '\s*=\s*\d+(M|K|k|m|G)?\n' + c = g + ' = ' + args[g] + s + '\n' + if content.find(g) != -1: + content = re.sub(rep, '\n' + c, content, 1) + else: + content = content.replace('[mysqld]\n', '[mysqld]\n' + c) + n += 1 + public.writeFile(conFile, content) + return public.returnJson(True, '设置成功!') + if __name__ == "__main__": func = sys.argv[1] if func == 'status': @@ -302,6 +345,10 @@ if __name__ == "__main__": print initdUinstall() elif func == 'run_info': print runInfo() + elif func == 'db_status': + print myDbStatus() + elif func == 'set_db_status': + print setDbStatus() elif func == 'conf': print getConf() elif func == 'show_log': diff --git a/plugins/mysql/js/mysql.js b/plugins/mysql/js/mysql.js index 102a2b5ad..62a73fbbb 100755 --- a/plugins/mysql/js/mysql.js +++ b/plugins/mysql/js/mysql.js @@ -37,7 +37,7 @@ function runInfo(){ // console.log(rdata); var cache_size = ((parseInt(rdata.Qcache_hits) / (parseInt(rdata.Qcache_hits) + parseInt(rdata.Qcache_inserts))) * 100).toFixed(2) + '%'; if (cache_size == 'NaN%') cache_size = 'OFF'; - var Con = '
\ + var Con = '
\ \ \ \ @@ -45,7 +45,7 @@ function runInfo(){ \ \
启动时间' + getLocalTime(rdata.Run) + '每秒查询' + parseInt(rdata.Questions / rdata.Uptime) + '
总连接次数' + rdata.Connections + '每秒事务' + parseInt((parseInt(rdata.Com_commit) + parseInt(rdata.Com_rollback)) / rdata.Uptime) + '
接收' + toSize(rdata.Bytes_received) + 'Position' + rdata.Position + '
\ - \ +
\ \ \ \ @@ -77,7 +77,6 @@ function myPort(){ $('#btn_change_port').click(function(){ var port = $("input[name='port']").val(); - // console.log(port); myPost('set_my_port','port='+port,function(data){ var rdata = $.parseJSON(data.data); if (rdata.status){ @@ -90,55 +89,39 @@ function myPort(){ }); } - -function selectChange() { - $("#SelectVersion,#selectVer").change(function() { - var info = $(this).val(); - var name = info.split(" ")[0]; - var version = info.split(" ")[1]; - max = 64 - msg = "64M" - if (name == 'mysql') { - memSize = getCookie('memSize'); - switch (version) { - case '5.1': - max = 256; - msg = '256M'; - break; - case '8.0': - max = 5200; - msg = '6GB'; - break; - case '5.7': - max = 1500; - msg = '2GB'; - break; - case '5.6': - max = 800; - msg = '1GB'; - break; - case 'AliSQL': - max = 800; - msg = '1GB'; - break; - case 'mariadb_10.0': - max = 800; - msg = '1GB'; - break; - case 'mariadb_10.1': - max = 1500; - msg = '2GB'; - break; - } - if (memSize < max) { - layer.msg(lan.bt.insatll_mem.replace('{1}', msg).replace('{2}', version), { icon: 5 }); - } - } - }); +function dbList(){ + var con = '
\ + \ + \ + \ + \ + \ + \ + \ +
\ +
\ +
活动/峰值连接数' + rdata.Threads_running + '/' + rdata.Max_used_connections + '若值过大,增加max_connections
\ + \ + \ + \ + \ + \ + \ + \ +
数据库名用户名密码备份备注操作
\ +
\ +
1共2条数据
\ +
\ + 同步选中\ + 同步所有\ + 从服务器获取\ +
\ + \ + '; + + $(".soft-man-con").html(con); } - - //设置二进制日志 function SetBinLog() { var loadT = layer.msg(lan.public.the, { icon: 16, time: 0, shade: 0.3 }); @@ -159,14 +142,18 @@ function closeMySqlLog() { }); } -//数据库端口 -function changeMySQLPort(act) { + + + + +//数据库存储信置 +function changeMySQLDataPath(act) { if (act != undefined) { - layer.confirm(lan.soft.mysql_port_title, { closeBtn: 2, icon: 3 }, function() { - var port = $("#dataport").val(); - var data = 'port=' + port; - var loadT = layer.msg(lan.public.the, { icon: 16, time: 0, shade: [0.3, '#000'] }); - $.post('/database?action=SetMySQLPort', data, function(rdata) { + layer.confirm(lan.soft.mysql_to_msg, { closeBtn: 2, icon: 3 }, function() { + var datadir = $("#datadir").val(); + var data = 'datadir=' + datadir; + var loadT = layer.msg(lan.soft.mysql_to_msg1, { icon: 16, time: 0, shade: [0.3, '#000'] }); + $.post('/database?action=SetDataDir', data, function(rdata) { layer.close(loadT) layer.msg(rdata.msg, { icon: rdata.status ? 1 : 5 }); }); @@ -176,50 +163,130 @@ function changeMySQLPort(act) { $.post('/database?action=GetMySQLInfo', '', function(rdata) { var LimitCon = '

\ - \ - \ + \ + \

'; - $(".soft-man-con").html(LimitCon); }); } -//软件切换版本 -function softChangeVer(name, version) { - if (name == "mysqld") name = "mysql"; - var veropt = version.split("|"); - var SelectVersion = ''; - for (var i = 0; i < veropt.length; i++) { - SelectVersion += ''; - } - var body = "
" + lan.soft.select_version + "
'; +//数据库日志 +function mysqlLog(act) { + //获取二进制日志相关信息 + $.post('/database?action=BinLog', "status=1", function(rdata) { + var limitCon = '

\ + ' + lan.soft.mysql_log_bin + ' ' + ToSize(rdata.msg) + '\ + \ +

' + lan.soft.mysql_log_err + '

\ + \ +

' - if (name == 'mysql') { - body += "" - } + $(".soft-man-con").html(limitCon); - $(".soft-man-con").html(body); - $(".btn-success").click(function() { - var ver = $("#selectVer").val(); - oneInstall(name, ver.split(" ")[1]); + //获取错误日志 + $.post('/database?action=GetErrorLog', "", function(error_body) { + if (error_body.status === false) { + layer.msg(error_body.msg, { icon: 5 }); + error_body = lan.soft.mysql_log_ps1; + } + if (error_body == "") error_body = lan.soft.mysql_log_ps1; + $("#error_log").text(error_body); + var ob = document.getElementById('error_log'); + ob.scrollTop = ob.scrollHeight; + }); }); - selectChange(); +} + + +//数据库配置状态 +function myPerfOpt() { + //获取MySQL配置 + myPost('db_status','',function(data){ + var rdata = $.parseJSON(data.data); + console.log(rdata); + + var key_buffer_size = toSizeM(rdata.mem.key_buffer_size); + var query_cache_size = toSizeM(rdata.mem.query_cache_size); + var tmp_table_size = toSizeM(rdata.mem.tmp_table_size); + var innodb_buffer_pool_size = toSizeM(rdata.mem.innodb_buffer_pool_size); + var innodb_additional_mem_pool_size = toSizeM(rdata.mem.innodb_additional_mem_pool_size); + var innodb_log_buffer_size = toSizeM(rdata.mem.innodb_log_buffer_size); + + var sort_buffer_size = toSizeM(rdata.mem.sort_buffer_size); + var read_buffer_size = toSizeM(rdata.mem.read_buffer_size); + var read_rnd_buffer_size = toSizeM(rdata.mem.read_rnd_buffer_size); + var join_buffer_size = toSizeM(rdata.mem.join_buffer_size); + var thread_stack = toSizeM(rdata.mem.thread_stack); + var binlog_cache_size = toSizeM(rdata.mem.binlog_cache_size); + + var a = key_buffer_size + query_cache_size + tmp_table_size + innodb_buffer_pool_size + innodb_additional_mem_pool_size + innodb_log_buffer_size; + var b = sort_buffer_size + read_buffer_size + read_rnd_buffer_size + join_buffer_size + thread_stack + binlog_cache_size; + var memSize = a + rdata.mem.max_connections * b; + + + var memCon = '
\ +
最大使用内存: \ + \ + ' + lan.soft.mysql_set_maxmem + ': MB\ +
\ +

key_buffer_sizeMB, ' + lan.soft.mysql_set_key_buffer_size + '

\ +

query_cache_sizeMB, ' + lan.soft.mysql_set_query_cache_size + '

\ +

tmp_table_sizeMB, ' + lan.soft.mysql_set_tmp_table_size + '

\ +

innodb_buffer_pool_sizeMB, ' + lan.soft.mysql_set_innodb_buffer_pool_size + '

\ +

innodb_log_buffer_sizeMB, ' + lan.soft.mysql_set_innodb_log_buffer_size + '

\ +

innodb_additional_mem_pool_sizeMB

\ +

sort_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_sort_buffer_size + '

\ +

read_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_read_buffer_size + '

\ +

read_rnd_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_read_rnd_buffer_size + '

\ +

join_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_join_buffer_size + '

\ +

thread_stackKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_thread_stack + '

\ +

binlog_cache_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_binlog_cache_size + '

\ +

thread_cache_size ' + lan.soft.mysql_set_thread_cache_size + '

\ +

table_open_cache ' + lan.soft.mysql_set_table_open_cache + '

\ +

max_connections ' + lan.soft.mysql_set_max_connections + '

\ +
\ +
' + + $(".soft-man-con").html(memCon); + + $(".conf_p input[name*='size'],.conf_p input[name='max_connections'],.conf_p input[name='thread_stack']").change(function() { + comMySqlMem(); + }); + + $(".conf_p select[name='mysql_set']").change(function() { + mySQLMemOpt($(this).val()); + comMySqlMem(); + }); + }); +} + +function reBootMySqld(){ + pluginOpService('mysql','restart',''); } //设置MySQL配置参数 -function SetMySQLConf() { - $.post('/system?action=GetMemInfo', '', function(memInfo) { - //var memSize = memInfo['memTotal']; - //var setSize = parseInt($("input[name='memSize']").val()); - //if(memSize < setSize){ - // var msg = lan.soft.mysql_set_err.replace('{1}',memSize).replace('{2}',setSize); - // layer.msg(msg,{icon:2,time:5000}); - // return; - //} +function setMySQLConf() { + $.post('/system/system_total', '', function(memInfo) { + var memSize = memInfo['memTotal']; + var setSize = parseInt($("input[name='memSize']").val()); + + if(memSize < setSize){ + var errMsg = "错误,内存分配过高!

物理内存: {1}MB
最大使用内存: {2}MB
可能造成的后果: 导致数据库不稳定,甚至无法启动MySQLd服务!"; + var msg = errMsg.replace('{1}',memSize).replace('{2}',setSize); + layer.msg(msg,{icon:2,time:5000}); + return; + } + var query_cache_size = parseInt($("input[name='query_cache_size']").val()); var query_cache_type = 0; if (query_cache_size > 0) { @@ -244,17 +311,35 @@ function SetMySQLConf() { max_connections: parseInt($("input[name='max_connections']").val()) }; - $.post('/database?action=SetDbConf', data, function(rdata) { - layer.msg(rdata.msg, { icon: rdata.status ? 1 : 2 }); + myPost('set_db_status', data, function(data){ + var rdata = $.parseJSON(data.data); + showMsg(rdata.msg,function(){ + reBootMySqld(); + },{ icon: rdata.status ? 1 : 2 }); }); - }) + },'json'); } //MySQL内存优化方案 -function MySQLMemOpt(opt) { +function mySQLMemOpt(opt) { var query_size = parseInt($("input[name='query_cache_size']").val()); switch (opt) { + case '0': + $("input[name='key_buffer_size']").val(8); + if (query_size) $("input[name='query_cache_size']").val(4); + $("input[name='tmp_table_size']").val(8); + $("input[name='innodb_buffer_pool_size']").val(16); + $("input[name='sort_buffer_size']").val(256); + $("input[name='read_buffer_size']").val(256); + $("input[name='read_rnd_buffer_size']").val(128); + $("input[name='join_buffer_size']").val(128); + $("input[name='thread_stack']").val(256); + $("input[name='binlog_cache_size']").val(32); + $("input[name='thread_cache_size']").val(4); + $("input[name='table_open_cache']").val(32); + $("input[name='max_connections']").val(500); + break; case '1': $("input[name='key_buffer_size']").val(128); if (query_size) $("input[name='query_cache_size']").val(64); @@ -333,18 +418,8 @@ function MySQLMemOpt(opt) { } } - -//重启MySQL -function ReBootMySqld() { - var loadT = layer.msg(lan.get('service_the', [lan.bt.restart, 'MySQLd']), { icon: 16, time: 0, shade: 0.3 }); - $.post('/system?action=ServiceAdmin', 'name=mysqld&type=restart', function(rdata) { - layer.close(loadT); - layer.msg(rdata.msg, { icon: rdata.status ? 1 : 2 }); - }); -} - //计算MySQL内存开销 -function ComMySqlMem() { +function comMySqlMem() { var key_buffer_size = parseInt($("input[name='key_buffer_size']").val()); var query_cache_size = parseInt($("input[name='query_cache_size']").val()); var tmp_table_size = parseInt($("input[name='tmp_table_size']").val()); @@ -364,125 +439,4 @@ function ComMySqlMem() { var b = sort_buffer_size + read_buffer_size + read_rnd_buffer_size + join_buffer_size + thread_stack + binlog_cache_size var memSize = a + max_connections * b $("input[name='memSize']").val(memSize.toFixed(2)); -} - -//数据库存储信置 -function changeMySQLDataPath(act) { - if (act != undefined) { - layer.confirm(lan.soft.mysql_to_msg, { closeBtn: 2, icon: 3 }, function() { - var datadir = $("#datadir").val(); - var data = 'datadir=' + datadir; - var loadT = layer.msg(lan.soft.mysql_to_msg1, { icon: 16, time: 0, shade: [0.3, '#000'] }); - $.post('/database?action=SetDataDir', data, function(rdata) { - layer.close(loadT) - layer.msg(rdata.msg, { icon: rdata.status ? 1 : 5 }); - }); - }); - return; - } - - $.post('/database?action=GetMySQLInfo', '', function(rdata) { - var LimitCon = '

\ - \ - \ -

'; - $(".soft-man-con").html(LimitCon); - }); -} - - - -//数据库日志 -function mysqlLog(act) { - //获取二进制日志相关信息 - $.post('/database?action=BinLog', "status=1", function(rdata) { - var limitCon = '

\ - ' + lan.soft.mysql_log_bin + ' ' + ToSize(rdata.msg) + '\ - \ -

' + lan.soft.mysql_log_err + '

\ - \ -

' - - $(".soft-man-con").html(limitCon); - - //获取错误日志 - $.post('/database?action=GetErrorLog', "", function(error_body) { - if (error_body.status === false) { - layer.msg(error_body.msg, { icon: 5 }); - error_body = lan.soft.mysql_log_ps1; - } - if (error_body == "") error_body = lan.soft.mysql_log_ps1; - $("#error_log").text(error_body); - var ob = document.getElementById('error_log'); - ob.scrollTop = ob.scrollHeight; - }); - }); -} - - -//数据库配置状态 -function mysqlStatus() { - //获取MySQL配置 - $.post('/database?action=GetDbStatus', "", function(rdata) { - var key_buffer_size = ToSizeM(rdata.mem.key_buffer_size) - var query_cache_size = ToSizeM(rdata.mem.query_cache_size) - var tmp_table_size = ToSizeM(rdata.mem.tmp_table_size) - var innodb_buffer_pool_size = ToSizeM(rdata.mem.innodb_buffer_pool_size) - var innodb_additional_mem_pool_size = ToSizeM(rdata.mem.innodb_additional_mem_pool_size) - var innodb_log_buffer_size = ToSizeM(rdata.mem.innodb_log_buffer_size) - - var sort_buffer_size = ToSizeM(rdata.mem.sort_buffer_size) - var read_buffer_size = ToSizeM(rdata.mem.read_buffer_size) - var read_rnd_buffer_size = ToSizeM(rdata.mem.read_rnd_buffer_size) - var join_buffer_size = ToSizeM(rdata.mem.join_buffer_size) - var thread_stack = ToSizeM(rdata.mem.thread_stack) - var binlog_cache_size = ToSizeM(rdata.mem.binlog_cache_size) - - var a = key_buffer_size + query_cache_size + tmp_table_size + innodb_buffer_pool_size + innodb_additional_mem_pool_size + innodb_log_buffer_size - var b = sort_buffer_size + read_buffer_size + read_rnd_buffer_size + join_buffer_size + thread_stack + binlog_cache_size - var memSize = a + rdata.mem.max_connections * b - - - var memCon = '
\ -
' + lan.soft.mysql_set_msg + '\ - \ - ' + lan.soft.mysql_set_maxmem + ': MB\ -
\ -

key_buffer_sizeMB, ' + lan.soft.mysql_set_key_buffer_size + '

\ -

query_cache_sizeMB, ' + lan.soft.mysql_set_query_cache_size + '

\ -

tmp_table_sizeMB, ' + lan.soft.mysql_set_tmp_table_size + '

\ -

innodb_buffer_pool_sizeMB, ' + lan.soft.mysql_set_innodb_buffer_pool_size + '

\ -

innodb_log_buffer_sizeMB, ' + lan.soft.mysql_set_innodb_log_buffer_size + '

\ -

innodb_additional_mem_pool_sizeMB

\ -

sort_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_sort_buffer_size + '

\ -

read_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_read_buffer_size + '

\ -

read_rnd_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_read_rnd_buffer_size + '

\ -

join_buffer_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_join_buffer_size + '

\ -

thread_stackKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_thread_stack + '

\ -

binlog_cache_sizeKB * ' + lan.soft.mysql_set_conn + ', ' + lan.soft.mysql_set_binlog_cache_size + '

\ -

thread_cache_size ' + lan.soft.mysql_set_thread_cache_size + '

\ -

table_open_cache ' + lan.soft.mysql_set_table_open_cache + '

\ -

max_connections ' + lan.soft.mysql_set_max_connections + '

\ -
\ -
' - - $(".soft-man-con").html(memCon); - - $(".conf_p input[name*='size'],.conf_p input[name='max_connections'],.conf_p input[name='thread_stack']").change(function() { - ComMySqlMem(); - }); - - $(".conf_p select[name='mysql_set']").change(function() { - MySQLMemOpt($(this).val()); - ComMySqlMem(); - }); - - }); } \ No newline at end of file