diff --git a/plugins/mysql/class/mysql.py b/plugins/mysql/class/mysql.py
index 6f1b058ab..6862b2a4c 100755
--- a/plugins/mysql/class/mysql.py
+++ b/plugins/mysql/class/mysql.py
@@ -33,6 +33,7 @@ class mysql:
self.__DB_PORT = int(re.search(rep, myconf).groups()[0])
except:
self.__DB_PORT = 3306
+ # print self.__DB_PASS
#self.__DB_PASS = public.M('config').where('id=?', (1,)).getField('mysql_root')
try:
self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS,
@@ -47,6 +48,9 @@ class mysql:
self.__DB_ERR = e
return False
+ def setPwd(self, pwd):
+ self.__DB_PASS = pwd
+
def execute(self, sql):
# 执行SQL语句返回受影响行
if not self.__Conn():
diff --git a/plugins/mysql/index.html b/plugins/mysql/index.html
index 7da5b38b2..861b3d959 100755
--- a/plugins/mysql/index.html
+++ b/plugins/mysql/index.html
@@ -4,6 +4,9 @@
diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py
index 1095e908c..f8c72ac99 100755
--- a/plugins/mysql/index.py
+++ b/plugins/mysql/index.py
@@ -72,22 +72,26 @@ def contentReplace(content):
return content
-def pSqliteDb():
+def pSqliteDb(dbname='databases'):
file = getServerDir() + '/mysql.db'
name = 'mysql'
if not os.path.exists(file):
- conn = public.M(name).dbPos(getServerDir(), name)
+ conn = public.M(dbname).dbPos(getServerDir(), name)
csql = public.readFile(getPluginDir() + '/conf/mysql.sql')
csql_list = csql.split(';')
for index in range(len(csql_list)):
conn.execute(csql_list[index], ())
else:
- conn = public.M(name).dbPos(getServerDir(), name)
+ conn = public.M(dbname).dbPos(getServerDir(), name)
return conn
def pMysqlDb():
- return ''
+ db = mysql.mysql()
+ db.__DB_CNF = getConf()
+ db.setPwd(pSqliteDb('config').where(
+ 'id=?', (1,)).getField('mysql_root'))
+ return db
def initDreplace():
@@ -127,7 +131,6 @@ def status():
def getDataDir():
-
file = getConf()
content = public.readFile(file)
rep = 'datadir\s*=\s*(.*)'
@@ -135,33 +138,50 @@ def getDataDir():
return tmp.groups()[0].strip()
+def getShowLogFile():
+ file = getConf()
+ content = public.readFile(file)
+ rep = 'slow-query-log-file\s*=\s*(.*)'
+ tmp = re.search(rep, content)
+ return tmp.groups()[0].strip()
+
+
def initMysqlData():
datadir = getDataDir()
- serverdir = getServerDir()
if not os.path.exists(datadir + '/mysql'):
+ serverdir = getServerDir()
cmd = 'cd ' + serverdir + ' && ./scripts/mysql_install_db --user=midoks --basedir=' + \
serverdir + ' --ldata=' + datadir
public.execShell(cmd)
+ return 0
+ return 1
- pwd = public.getRandomString(16)
- cmd_pass = serverdir + '/bin/mysqladmin -uroot -p12345'
- print cmd_pass
+
+def initMysqlPwd():
+ time.sleep(3)
+
+ serverdir = getServerDir()
+
+ pwd = public.getRandomString(16)
+ cmd_pass = serverdir + '/bin/mysqladmin -uroot password ' + pwd
+ pSqliteDb('config').where('id=?', (1,)).save('mysql_root', (pwd,))
+ public.execShell(cmd_pass)
return True
def myOp(method):
+ import commands
init_file = initDreplace()
cmd = init_file + ' ' + method
- if method == 'start':
- initMysqlData()
+ try:
+ initData = initMysqlData()
subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True,
bufsize=4096, stderr=subprocess.PIPE)
+ if (initData == 0):
+ initMysqlPwd()
return 'ok'
- else:
- data = public.execShell(cmd)
- if data[1] == '':
- return 'ok'
- return data[1]
+ except Exception as e:
+ return str(e)
def start():
@@ -213,16 +233,54 @@ def initdUinstall():
return 'ok'
+def getMyPort():
+ file = getConf()
+ content = public.readFile(file)
+ rep = 'port\s*=\s*(.*)'
+ tmp = re.search(rep, content)
+ return tmp.groups()[0].strip()
+
+
+def setMyPort():
+ args = getArgs()
+ if not 'port' in args:
+ return 'port missing'
+
+ port = args['port']
+ file = getConf()
+ content = public.readFile(file)
+ rep = "port\s*=\s*([0-9]+)\s*\n"
+ content = re.sub(rep, 'port = ' + port + '\n', content)
+ public.writeFile(file, content)
+ restart()
+ return public.returnJson(True, '编辑成功!')
+
+
def runInfo():
- db = mysql.mysql()
- db.__DB_CNF = getConf()
+ db = pMysqlDb()
data = db.query('show global status')
- print data
- return 'ok'
+ gets = ['Max_used_connections', 'Com_commit', 'Com_rollback', 'Questions', 'Innodb_buffer_pool_reads', 'Innodb_buffer_pool_read_requests', 'Key_reads', 'Key_read_requests', 'Key_writes', 'Key_write_requests', 'Qcache_hits', 'Qcache_inserts', 'Bytes_received', 'Bytes_sent', 'Aborted_clients', 'Aborted_connects',
+ 'Created_tmp_disk_tables', 'Created_tmp_tables', 'Innodb_buffer_pool_pages_dirty', 'Opened_files', 'Open_tables', 'Opened_tables', 'Select_full_join', 'Select_range_check', 'Sort_merge_passes', 'Table_locks_waited', 'Threads_cached', 'Threads_connected', 'Threads_created', 'Threads_running', 'Connections', 'Uptime']
+ try:
+ if data[0] == 1045:
+ return public.returnJson(False, 'MySQL密码错误!')
+ except:
+ pass
-
-def getShowLog():
- return 'ok'
+ result = {}
+ for d in data:
+ for g in gets:
+ if d[0] == g:
+ result[g] = d[1]
+ result['Run'] = int(time.time()) - int(result['Uptime'])
+ tmp = db.query('show master status')
+ try:
+ result['File'] = tmp[0][0]
+ result['Position'] = tmp[0][1]
+ except:
+ result['File'] = 'OFF'
+ result['Position'] = 'OFF'
+ return public.getJson(result)
if __name__ == "__main__":
func = sys.argv[1]
@@ -247,6 +305,10 @@ if __name__ == "__main__":
elif func == 'conf':
print getConf()
elif func == 'show_log':
- print getShowLog()
+ print getShowLogFile()
+ elif func == 'my_port':
+ print getMyPort()
+ elif func == 'set_my_port':
+ print setMyPort()
else:
print 'error'
diff --git a/plugins/mysql/js/database.html b/plugins/mysql/js/database.html
deleted file mode 100755
index 1340cf5c5..000000000
--- a/plugins/mysql/js/database.html
+++ /dev/null
@@ -1,80 +0,0 @@
-{% extends "default/layout.html" %}
-
-{% block content %}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-{% endblock %}
diff --git a/plugins/mysql/js/mysql.js b/plugins/mysql/js/mysql.js
index 1a30978f0..102a2b5ad 100755
--- a/plugins/mysql/js/mysql.js
+++ b/plugins/mysql/js/mysql.js
@@ -1,3 +1,96 @@
+function str2Obj(str){
+ var data = {};
+ kv = str.split('&');
+ for(i in kv){
+ v = kv[i].split('=');
+ data[v[0]] = v[1];
+ }
+ return data;
+}
+
+function myPost(method,args,callback){
+
+ var _args = null;
+ if (typeof(args) == 'string'){
+ _args = JSON.stringify(str2Obj(args));
+ } else {
+ _args = JSON.stringify(args);
+ }
+
+ var loadT = layer.msg('正在获取...', { icon: 16, time: 0, shade: 0.3 });
+ $.post('/plugins/run', {name:'mysql', func:method, args:_args}, function(data) {
+ layer.close(loadT);
+ if (!data.status){
+ layer.msg(data.msg,{icon:0,time:2000,shade: [0.3, '#000']});
+ return;
+ }
+
+ if(typeof(callback) == 'function'){
+ callback(data);
+ }
+ },'json');
+}
+
+function runInfo(){
+ myPost('run_info','',function(data){
+ var rdata = $.parseJSON(data.data);
+ // 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 = '
\
+ \
+ | 启动时间 | ' + getLocalTime(rdata.Run) + ' | 每秒查询 | ' + parseInt(rdata.Questions / rdata.Uptime) + ' |
\
+ | 总连接次数 | ' + rdata.Connections + ' | 每秒事务 | ' + parseInt((parseInt(rdata.Com_commit) + parseInt(rdata.Com_rollback)) / rdata.Uptime) + ' |
\
+ | 发送 | ' + toSize(rdata.Bytes_sent) + ' | File | ' + rdata.File + ' |
\
+ | 接收 | ' + toSize(rdata.Bytes_received) + ' | Position | ' + rdata.Position + ' |
\
+ \
+
\
+
\
+ | | | | \
+ \
+ | 活动/峰值连接数 | ' + rdata.Threads_running + '/' + rdata.Max_used_connections + ' | 若值过大,增加max_connections |
\
+ | 线程缓存命中率 | ' + ((1 - rdata.Threads_created / rdata.Connections) * 100).toFixed(2) + '% | 若过低,增加thread_cache_size |
\
+ | 索引命中率 | ' + ((1 - rdata.Key_reads / rdata.Key_read_requests) * 100).toFixed(2) + '% | 若过低,增加key_buffer_size |
\
+ | Innodb索引命中率 | ' + ((1 - rdata.Innodb_buffer_pool_reads / rdata.Innodb_buffer_pool_read_requests) * 100).toFixed(2) + '% | 若过低,增加innodb_buffer_pool_size |
\
+ | 查询缓存命中率 | ' + cache_size + ' | ' + lan.soft.mysql_status_ps5 + ' |
\
+ | 创建临时表到磁盘 | ' + ((rdata.Created_tmp_disk_tables / rdata.Created_tmp_tables) * 100).toFixed(2) + '% | 若过大,尝试增加tmp_table_size |
\
+ | 已打开的表 | ' + rdata.Open_tables + ' | 若过大,增加table_cache_size |
\
+ | 没有使用索引的量 | ' + rdata.Select_full_join + ' | 若不为0,请检查数据表的索引是否合理 |
\
+ | 没有索引的JOIN量 | ' + rdata.Select_range_check + ' | 若不为0,请检查数据表的索引是否合理 |
\
+ | 排序后的合并次数 | ' + rdata.Sort_merge_passes + ' | 若值过大,增加sort_buffer_size |
\
+ | 锁表次数 | ' + rdata.Table_locks_waited + ' | 若值过大,请考虑增加您的数据库性能 |
\
+ \
+
'
+ $(".soft-man-con").html(Con);
+ });
+}
+
+
+function myPort(){
+ myPost('my_port','',function(data){
+ var con = '
';
+ $(".soft-man-con").html(con);
+
+ $('#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){
+ layer.msg('修改成功!',{icon:1,time:2000,shade: [0.3, '#000']});
+ } else {
+ layer.msg(rdata.msg,{icon:1,time:2000,shade: [0.3, '#000']});
+ }
+ });
+ });
+ });
+}
+
+
function selectChange() {
$("#SelectVersion,#selectVer").change(function() {
var info = $(this).val();
@@ -297,21 +390,7 @@ function changeMySQLDataPath(act) {
});
}
-//MySQL-Slow日志
-function mysqlSlowLog() {
- var loadT = layer.msg(lan.public.the, { icon: 16, time: 0, shade: [0.3, '#000'] });
- $.post('/database?action=GetSlowLogs', {}, function(logs) {
- layer.close(loadT);
- if (logs.status !== true) {
- logs.msg = '';
- }
- if (logs.msg == '') logs.msg = '当前没有慢日志.';
- var phpCon = '
';
- $(".soft-man-con").html(phpCon);
- var ob = document.getElementById('error_log');
- ob.scrollTop = ob.scrollHeight;
- });
-}
+
//数据库日志
function mysqlLog(act) {
@@ -340,38 +419,6 @@ function mysqlLog(act) {
});
}
-//取数据库运行状态
-function mysqlRunStatus() {
- $.post('/database?action=GetRunStatus', "", function(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 = '
\
- \
- | ' + lan.soft.mysql_status_title1 + ' | ' + getLocalTime(rdata.Run) + ' | ' + lan.soft.mysql_status_title5 + ' | ' + parseInt(rdata.Questions / rdata.Uptime) + ' |
\
- | ' + lan.soft.mysql_status_title2 + ' | ' + rdata.Connections + ' | ' + lan.soft.mysql_status_title6 + ' | ' + parseInt((parseInt(rdata.Com_commit) + parseInt(rdata.Com_rollback)) / rdata.Uptime) + ' |
\
- | ' + lan.soft.mysql_status_title3 + ' | ' + ToSize(rdata.Bytes_sent) + ' | ' + lan.soft.mysql_status_title7 + ' | ' + rdata.File + ' |
\
- | ' + lan.soft.mysql_status_title4 + ' | ' + ToSize(rdata.Bytes_received) + ' | ' + lan.soft.mysql_status_title8 + ' | ' + rdata.Position + ' |
\
- \
-
\
-
\
- | | | | \
- \
- | ' + lan.soft.mysql_status_title9 + ' | ' + rdata.Threads_running + '/' + rdata.Max_used_connections + ' | ' + lan.soft.mysql_status_ps1 + ' |
\
- | ' + lan.soft.mysql_status_title10 + ' | ' + ((1 - rdata.Threads_created / rdata.Connections) * 100).toFixed(2) + '% | ' + lan.soft.mysql_status_ps2 + ' |
\
- | ' + lan.soft.mysql_status_title11 + ' | ' + ((1 - rdata.Key_reads / rdata.Key_read_requests) * 100).toFixed(2) + '% | ' + lan.soft.mysql_status_ps3 + ' |
\
- | ' + lan.soft.mysql_status_title12 + ' | ' + ((1 - rdata.Innodb_buffer_pool_reads / rdata.Innodb_buffer_pool_read_requests) * 100).toFixed(2) + '% | ' + lan.soft.mysql_status_ps4 + ' |
\
- | ' + lan.soft.mysql_status_title13 + ' | ' + cache_size + ' | ' + lan.soft.mysql_status_ps5 + ' |
\
- | ' + lan.soft.mysql_status_title14 + ' | ' + ((rdata.Created_tmp_disk_tables / rdata.Created_tmp_tables) * 100).toFixed(2) + '% | ' + lan.soft.mysql_status_ps6 + ' |
\
- | ' + lan.soft.mysql_status_title15 + ' | ' + rdata.Open_tables + ' | ' + lan.soft.mysql_status_ps7 + ' |
\
- | ' + lan.soft.mysql_status_title16 + ' | ' + rdata.Select_full_join + ' | ' + lan.soft.mysql_status_ps8 + ' |
\
- | ' + lan.soft.mysql_status_title17 + ' | ' + rdata.Select_range_check + ' | ' + lan.soft.mysql_status_ps9 + ' |
\
- | ' + lan.soft.mysql_status_title18 + ' | ' + rdata.Sort_merge_passes + ' | ' + lan.soft.mysql_status_ps10 + ' |
\
- | ' + lan.soft.mysql_status_title19 + ' | ' + rdata.Table_locks_waited + ' | ' + lan.soft.mysql_status_ps11 + ' |
\
- \
-
'
- $(".soft-man-con").html(Con);
- });
-}
//数据库配置状态
function mysqlStatus() {