Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/mysql/index.py

1105 lines
32 KiB

7 years ago
# coding:utf-8
import sys
import io
import os
import time
6 years ago
import subprocess
6 years ago
import re
6 years ago
import json
7 years ago
6 years ago
reload(sys)
sys.setdefaultencoding('utf-8')
7 years ago
sys.path.append(os.getcwd() + "/class/core")
import mw
7 years ago
6 years ago
6 years ago
app_debug = False
if mw.isAppleSystem():
6 years ago
app_debug = True
7 years ago
6 years ago
def getPluginName():
return 'mysql'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
6 years ago
6 years ago
sys.path.append(getPluginDir() + "/class")
import mysql
6 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
6 years ago
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
6 years ago
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
6 years ago
6 years ago
def getConf():
6 years ago
path = getServerDir() + '/etc/my.cnf'
6 years ago
return path
def getInitdTpl():
path = getPluginDir() + '/init.d/mysql.tpl'
return path
def contentReplace(content):
service_path = mw.getServerDir()
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
6 years ago
content = content.replace('{$SERVER_PATH}', service_path)
6 years ago
content = content.replace('{$SERVER_APP_PATH}', service_path + '/mysql')
6 years ago
return content
6 years ago
def pSqliteDb(dbname='databases'):
6 years ago
file = getServerDir() + '/mysql.db'
name = 'mysql'
if not os.path.exists(file):
conn = mw.M(dbname).dbPos(getServerDir(), name)
csql = mw.readFile(getPluginDir() + '/conf/mysql.sql')
6 years ago
csql_list = csql.split(';')
for index in range(len(csql_list)):
conn.execute(csql_list[index], ())
else:
conn = mw.M(dbname).dbPos(getServerDir(), name)
6 years ago
return conn
def pMysqlDb():
6 years ago
db = mysql.mysql()
db.__DB_CNF = getConf()
db.setPwd(pSqliteDb('config').where(
'id=?', (1,)).getField('mysql_root'))
return db
6 years ago
6 years ago
def initDreplace():
initd_tpl = getInitdTpl()
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
if not os.path.exists(file_bin):
content = mw.readFile(initd_tpl)
6 years ago
content = contentReplace(content)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
6 years ago
6 years ago
mysql_conf_dir = getServerDir() + '/etc'
6 years ago
if not os.path.exists(mysql_conf_dir):
os.mkdir(mysql_conf_dir)
mysql_conf = mysql_conf_dir + '/my.cnf'
if not os.path.exists(mysql_conf):
mysql_conf_tpl = getPluginDir() + '/conf/my.cnf'
content = mw.readFile(mysql_conf_tpl)
6 years ago
content = contentReplace(content)
mw.writeFile(mysql_conf, content)
6 years ago
return file_bin
7 years ago
def status():
data = mw.execShell(
6 years ago
"ps -ef|grep mysqld |grep -v grep | grep -v python | awk '{print $2}'")
7 years ago
if data[0] == '':
return 'stop'
return 'start'
6 years ago
def getDataDir():
file = getConf()
content = mw.readFile(file)
6 years ago
rep = 'datadir\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
6 years ago
def binLog():
args = getArgs()
conf = getConf()
con = mw.readFile(conf)
6 years ago
if con.find('#log-bin=mysql-bin') != -1:
if args.has_key('status'):
return mw.returnJson(False, '0')
6 years ago
con = con.replace('#log-bin=mysql-bin', 'log-bin=mysql-bin')
con = con.replace('#binlog_format=mixed', 'binlog_format=mixed')
mw.execShell('sync')
6 years ago
restart()
else:
path = getDataDir()
if args.has_key('status'):
dsize = 0
for n in os.listdir(path):
if len(n) < 9:
continue
if n[0:9] == 'mysql-bin':
dsize += os.path.getsize(path + '/' + n)
return mw.returnJson(True, dsize)
6 years ago
con = con.replace('log-bin=mysql-bin', '#log-bin=mysql-bin')
con = con.replace('binlog_format=mixed', '#binlog_format=mixed')
mw.execShell('sync')
6 years ago
restart()
mw.execShell('rm -f ' + path + '/mysql-bin.*')
6 years ago
mw.writeFile(conf, con)
return mw.returnJson(True, '设置成功!')
6 years ago
6 years ago
def getErrorLog():
args = getArgs()
path = getDataDir()
filename = ''
for n in os.listdir(path):
if len(n) < 5:
continue
if n == 'error.log':
filename = path + '/' + n
break
print filename
6 years ago
if not os.path.exists(filename):
return mw.returnJson(False, '指定文件不存在!')
6 years ago
if args.has_key('close'):
mw.writeFile(filename, '')
return mw.returnJson(False, '日志已清空')
info = mw.getNumLines(filename, 1000)
return mw.returnJson(True, 'OK', info)
6 years ago
6 years ago
def getShowLogFile():
file = getConf()
content = mw.readFile(file)
6 years ago
rep = 'slow-query-log-file\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
6 years ago
def pGetDbUser():
if mw.isAppleSystem():
user = mw.execShell(
6 years ago
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
return user
return 'mysql'
6 years ago
def initMysqlData():
datadir = getDataDir()
if not os.path.exists(datadir + '/mysql'):
6 years ago
serverdir = getServerDir()
6 years ago
user = pGetDbUser()
6 years ago
cmd = 'cd ' + serverdir + ' && ./scripts/mysql_install_db --user=' + \
user + ' --basedir=' + serverdir + ' --ldata=' + datadir
mw.execShell(cmd)
6 years ago
return 0
return 1
def initMysqlPwd():
6 years ago
time.sleep(5)
6 years ago
6 years ago
serverdir = getServerDir()
pwd = mw.getRandomString(16)
6 years ago
cmd_pass = serverdir + '/bin/mysqladmin -uroot password ' + pwd
pSqliteDb('config').where('id=?', (1,)).save('mysql_root', (pwd,))
mw.execShell(cmd_pass)
6 years ago
return True
6 years ago
def myOp(method):
6 years ago
import commands
6 years ago
init_file = initDreplace()
6 years ago
cmd = init_file + ' ' + method
6 years ago
try:
initData = initMysqlData()
6 years ago
subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True,
bufsize=4096, stderr=subprocess.PIPE)
6 years ago
if (initData == 0):
initMysqlPwd()
6 years ago
return 'ok'
6 years ago
except Exception as e:
return str(e)
6 years ago
7 years ago
def start():
6 years ago
return myOp('start')
6 years ago
7 years ago
def stop():
6 years ago
return myOp('stop')
7 years ago
def restart():
6 years ago
return myOp('restart')
7 years ago
def reload():
6 years ago
return myOp('reload')
7 years ago
6 years ago
def initdStatus():
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
return 'ok'
return 'fail'
def initdInstall():
import shutil
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
mysql_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(mysql_bin, initd_bin)
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
6 years ago
return 'ok'
def initdUinstall():
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
mw.execShell('chkconfig --del ' + getPluginName())
6 years ago
initd_bin = getInitDFile()
os.remove(initd_bin)
return 'ok'
7 years ago
def getMyDbPos():
file = getConf()
content = mw.readFile(file)
rep = 'datadir\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
def setMyDbPos():
args = getArgs()
data = checkArgs(args, ['datadir'])
if not data[0]:
return data[1]
s_datadir = getMyDbPos()
t_datadir = args['datadir']
if t_datadir == s_datadir:
return mw.returnJson(False, '与当前存储目录相同,无法迁移文件!')
if not os.path.exists(t_datadir):
mw.execShell('mkdir -p ' + t_datadir)
# mw.execShell('/etc/init.d/mysqld stop')
stop()
mw.execShell('cp -rf ' + s_datadir + '/* ' + t_datadir + '/')
mw.execShell('chown -R mysql.mysql ' + t_datadir)
mw.execShell('chmod -R 755 ' + t_datadir)
mw.execShell('rm -f ' + t_datadir + '/*.pid')
mw.execShell('rm -f ' + t_datadir + '/*.err')
path = getServerDir()
myfile = path + '/etc/my.cnf'
mycnf = mw.readFile(myfile)
mw.writeFile(path + '/etc/my_backup.cnf', mycnf)
mycnf = mycnf.replace(s_datadir, t_datadir)
mw.writeFile(myfile, mycnf)
start()
result = mw.execShell(
'ps aux|grep mysqld| grep -v grep|grep -v python')
if len(result[0]) > 10:
mw.writeFile('data/datadir.pl', t_datadir)
return mw.returnJson(True, '存储目录迁移成功!')
else:
mw.execShell('pkill -9 mysqld')
mw.writeFile(myfile, mw.readFile(path + '/etc/my_backup.cnf'))
start()
return mw.returnJson(False, '文件迁移失败!')
6 years ago
def getMyPort():
file = getConf()
content = mw.readFile(file)
6 years ago
rep = 'port\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
6 years ago
6 years ago
def setMyPort():
args = getArgs()
data = checkArgs(args, ['port'])
if not data[0]:
return data[1]
6 years ago
port = args['port']
file = getConf()
content = mw.readFile(file)
6 years ago
rep = "port\s*=\s*([0-9]+)\s*\n"
content = re.sub(rep, 'port = ' + port + '\n', content)
mw.writeFile(file, content)
6 years ago
restart()
return mw.returnJson(True, '编辑成功!')
6 years ago
def runInfo():
db = pMysqlDb()
data = db.query('show global status')
6 years ago
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']
6 years ago
try:
6 years ago
# print data
if data[0] == 1045 or data[0] == 2003:
pwd = db.getPwd()
return mw.returnJson(False, 'mysql password error:' + pwd + '!')
6 years ago
except Exception as e:
6 years ago
pass
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 mw.getJson(result)
6 years ago
6 years ago
def myDbStatus():
result = {}
db = pMysqlDb()
data = db.query('show variables')
6 years ago
isError = isSqlError(data)
if isError != None:
return isError
6 years ago
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 mw.getJson(result)
6 years ago
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 = mw.readFile(conFile)
6 years ago
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
mw.writeFile(conFile, content)
return mw.returnJson(True, '设置成功!')
6 years ago
6 years ago
def isSqlError(mysqlMsg):
6 years ago
# 检测数据库执行错误
6 years ago
mysqlMsg = str(mysqlMsg)
if "MySQLdb" in mysqlMsg:
return mw.returnJson(False, 'MySQLdb组件缺失! <br>进入SSH命令行输入: pip install mysql-python')
6 years ago
if "2002," in mysqlMsg:
return mw.returnJson(False, '数据库连接失败,请检查数据库服务是否启动!')
6 years ago
if "using password:" in mysqlMsg:
return mw.returnJson(False, '数据库管理密码错误!')
6 years ago
if "Connection refused" in mysqlMsg:
return mw.returnJson(False, '数据库连接失败,请检查数据库服务是否启动!')
6 years ago
if "1133" in mysqlMsg:
return mw.returnJson(False, '数据库用户不存在!')
6 years ago
if "1007" in mysqlMsg:
return mw.returnJson(False, '数据库已经存在!')
6 years ago
return None
6 years ago
def mapToList(map_obj):
# map to list
try:
if type(map_obj) != list and type(map_obj) != str:
map_obj = list(map_obj)
return map_obj
except:
return []
6 years ago
def __createUser(dbname, username, password, address):
pdb = pMysqlDb()
pdb.execute(
"CREATE USER `%s`@`localhost` IDENTIFIED BY '%s'" % (username, password))
pdb.execute(
"grant all privileges on %s.* to `%s`@`localhost`" % (dbname, username))
for a in address.split(','):
pdb.execute(
"CREATE USER `%s`@`%s` IDENTIFIED BY '%s'" % (username, a, password))
pdb.execute(
"grant all privileges on %s.* to `%s`@`%s`" % (dbname, username, a))
pdb.execute("flush privileges")
6 years ago
def getDbList():
args = getArgs()
page = 1
page_size = 10
search = ''
data = {}
if 'page' in args:
page = int(args['page'])
if 'page_size' in args:
page_size = int(args['page_size'])
if 'search' in args:
search = args['search']
conn = pSqliteDb('databases')
limit = str((page - 1) * page_size) + ',' + str(page_size)
condition = ''
if not search == '':
condition = "name like '%" + search + "%'"
field = 'id,pid,name,username,password,accept,ps,addtime'
clist = conn.where(condition, ()).field(
field).limit(limit).order('id desc').select()
count = conn.where(condition, ()).count()
_page = {}
_page['count'] = count
_page['p'] = page
_page['row'] = page_size
_page['tojs'] = 'dbList'
data['page'] = mw.getPage(_page)
6 years ago
data['data'] = clist
info = {}
info['root_pwd'] = pSqliteDb('config').where(
'id=?', (1,)).getField('mysql_root')
data['info'] = info
return mw.getJson(data)
6 years ago
6 years ago
def syncGetDatabases():
pdb = pMysqlDb()
6 years ago
psdb = pSqliteDb('databases')
data = pdb.query('show databases')
isError = isSqlError(data)
if isError != None:
return isError
users = pdb.query(
"select User,Host from mysql.user where User!='root' AND Host!='localhost' AND Host!=''")
nameArr = ['information_schema', 'performance_schema', 'mysql', 'sys']
n = 0
for value in data:
b = False
for key in nameArr:
if value[0] == key:
b = True
break
if b:
continue
if psdb.where("name=?", (value[0],)).count():
continue
host = '127.0.0.1'
for user in users:
if value[0] == user[0]:
host = user[1]
break
ps = mw.getMsg('INPUT_PS')
6 years ago
if value[0] == 'test':
ps = mw.getMsg('DATABASE_TEST')
6 years ago
addTime = time.strftime('%Y-%m-%d %X', time.localtime())
6 years ago
if psdb.add('name,username,password,accept,ps,addtime', (value[0], value[0], '', host, ps, addTime)):
6 years ago
n += 1
msg = mw.getInfo('本次共从服务器获取了{1}个数据库!', (str(n),))
return mw.returnJson(True, msg)
6 years ago
6 years ago
6 years ago
def toDbBase(find):
pdb = pMysqlDb()
psdb = pSqliteDb('databases')
if len(find['password']) < 3:
find['username'] = find['name']
find['password'] = mw.md5(str(time.time()) + find['name'])[0:10]
6 years ago
psdb.where("id=?", (find['id'],)).save(
'password,username', (find['password'], find['username']))
result = pdb.execute("create database `" + find['name'] + "`")
if "using password:" in str(result):
return -1
if "Connection refused" in str(result):
return -1
password = find['password']
__createUser(find['name'], find['username'], password, find['accept'])
return 1
6 years ago
def syncToDatabases():
6 years ago
args = getArgs()
6 years ago
data = checkArgs(args, ['type', 'ids'])
6 years ago
if not data[0]:
return data[1]
pdb = pMysqlDb()
result = pdb.execute("show databases")
isError = isSqlError(result)
if isError:
return isError
stype = int(args['type'])
psdb = pSqliteDb('databases')
n = 0
6 years ago
6 years ago
if stype == 0:
6 years ago
data = psdb.field('id,name,username,password,accept').select()
6 years ago
for value in data:
result = toDbBase(value)
if result == 1:
n += 1
else:
6 years ago
data = json.loads(args['ids'])
for value in data:
find = psdb.where("id=?", (value,)).field(
'id,name,username,password,accept').find()
# print find
result = toDbBase(find)
if result == 1:
n += 1
msg = mw.getInfo('本次共同步了{1}个数据库!', (str(n),))
return mw.returnJson(True, msg)
6 years ago
6 years ago
def setRootPwd():
args = getArgs()
data = checkArgs(args, ['password'])
if not data[0]:
return data[1]
6 years ago
password = args['password']
try:
pdb = pMysqlDb()
result = pdb.query("show databases")
isError = isSqlError(result)
if isError != None:
return isError
m_version = mw.readFile(getServerDir() + '/version.pl')
6 years ago
if m_version.find('5.7') == 0 or m_version.find('8.0') == 0:
pdb.execute(
"UPDATE mysql.user SET authentication_string='' WHERE user='root'")
pdb.execute(
"ALTER USER 'root'@'localhost' IDENTIFIED BY '%s'" % password)
pdb.execute(
"ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY '%s'" % password)
else:
result = pdb.execute(
"update mysql.user set Password=password('" + password + "') where User='root'")
pdb.execute("flush privileges")
pSqliteDb('config').where('id=?', (1,)).save('mysql_root', (password,))
return mw.returnJson(True, '数据库root密码修改成功!')
6 years ago
except Exception as ex:
return mw.returnJson(False, '修改错误:' + str(ex))
6 years ago
6 years ago
def setUserPwd():
args = getArgs()
data = checkArgs(args, ['password', 'name'])
if not data[0]:
return data[1]
newpassword = args['password']
username = args['name']
id = args['id']
try:
pdb = pMysqlDb()
psdb = pSqliteDb('databases')
name = psdb.where('id=?', (id,)).getField('name')
m_version = mw.readFile(getServerDir() + '/version.pl')
6 years ago
if m_version.find('5.7') == 0 or m_version.find('8.0') == 0:
tmp = pdb.query(
"select Host from mysql.user where User='" + name + "' AND Host!='localhost'")
accept = mapToList(tmp)
pdb.execute(
"update mysql.user set authentication_string='' where User='" + username + "'")
result = pdb.execute(
"ALTER USER `%s`@`localhost` IDENTIFIED BY '%s'" % (username, newpassword))
for my_host in accept:
pdb.execute("ALTER USER `%s`@`%s` IDENTIFIED BY '%s'" % (
username, my_host[0], newpassword))
else:
result = pdb.execute("update mysql.user set Password=password('" +
newpassword + "') where User='" + username + "'")
isError = isSqlError(result)
if isError != None:
return isError
pdb.execute("flush privileges")
psdb.where("id=?", (id,)).setField('password', newpassword)
return mw.returnJson(True, mw.getInfo('修改数据库[{1}]密码成功!', (name,)))
6 years ago
except Exception as ex:
# print str(ex)
return mw.returnJson(False, mw.getInfo('修改数据库[{1}]密码失败!', (name,)))
6 years ago
def setDbPs():
6 years ago
args = getArgs()
data = checkArgs(args, ['id', 'name', 'ps'])
if not data[0]:
return data[1]
6 years ago
ps = args['ps']
sid = args['id']
name = args['name']
try:
psdb = pSqliteDb('databases')
psdb.where("id=?", (sid,)).setField('ps', ps)
return mw.returnJson(True, mw.getInfo('修改数据库[{1}]备注成功!', (name,)))
except Exception as e:
return mw.returnJson(True, mw.getInfo('修改数据库[{1}]备注失败!', (name,)))
def addDb():
args = getArgs()
6 years ago
data = checkArgs(args,
['password', 'name', 'codeing', 'db_user', 'dataAccess', 'ps'])
if not data[0]:
return data[1]
6 years ago
if not 'address' in args:
address = ''
else:
address = args['address'].strip()
dbname = args['name'].strip()
dbuser = args['db_user'].strip()
codeing = args['codeing'].strip()
password = args['password'].strip()
dataAccess = args['dataAccess'].strip()
6 years ago
ps = args['ps'].strip()
6 years ago
reg = "^[\w\.-]+$"
if not re.match(reg, args['name']):
return mw.returnJson(False, '数据库名称不能带有特殊符号!')
6 years ago
checks = ['root', 'mysql', 'test', 'sys', 'panel_logs']
if dbuser in checks or len(dbuser) < 1:
return mw.returnJson(False, '数据库用户名不合法!')
6 years ago
if dbname in checks or len(dbname) < 1:
return mw.returnJson(False, '数据库名称不合法!')
6 years ago
if len(password) < 1:
password = mw.md5(time.time())[0:8]
6 years ago
wheres = {
'utf8': 'utf8_general_ci',
'utf8mb4': 'utf8mb4_general_ci',
'gbk': 'gbk_chinese_ci',
'big5': 'big5_chinese_ci'
}
codeStr = wheres[codeing]
pdb = pMysqlDb()
psdb = pSqliteDb('databases')
if psdb.where("name=? or username=?", (dbname, dbuser)).count():
return mw.returnJson(False, '数据库已存在!')
6 years ago
result = pdb.execute("create database `" + dbname +
"` DEFAULT CHARACTER SET " + codeing + " COLLATE " + codeStr)
# print result
isError = isSqlError(result)
if isError != None:
return isError
pdb.execute("drop user '" + dbuser + "'@'localhost'")
for a in address.split(','):
pdb.execute("drop user '" + dbuser + "'@'" + a + "'")
__createUser(dbname, dbuser, password, address)
addTime = time.strftime('%Y-%m-%d %X', time.localtime())
psdb.add('pid,name,username,password,accept,ps,addtime',
6 years ago
(0, dbname, dbuser, password, address, ps, addTime))
return mw.returnJson(True, '添加成功!')
6 years ago
def delDb():
6 years ago
args = getArgs()
data = checkArgs(args, ['id', 'name'])
if not data[0]:
return data[1]
6 years ago
try:
6 years ago
id = args['id']
name = args['name']
psdb = pSqliteDb('databases')
pdb = pMysqlDb()
find = psdb.where("id=?", (id,)).field(
'id,pid,name,username,password,accept,ps,addtime').find()
accept = find['accept']
username = find['username']
# 删除MYSQL
result = pdb.execute("drop database `" + name + "`")
isError = isSqlError(result)
if isError != None:
return isError
users = pdb.query(
"select Host from mysql.user where User='" + username + "' AND Host!='localhost'")
pdb.execute("drop user '" + username + "'@'localhost'")
for us in users:
pdb.execute("drop user '" + username + "'@'" + us[0] + "'")
pdb.execute("flush privileges")
# 删除SQLITE
psdb.where("id=?", (id,)).delete()
return mw.returnJson(True, '删除成功!')
6 years ago
except Exception as ex:
return mw.returnJson(False, '删除失败!' + str(ex))
6 years ago
6 years ago
def getDbAccess():
args = getArgs()
data = checkArgs(args, ['username'])
if not data[0]:
return data[1]
username = args['username']
pdb = pMysqlDb()
users = pdb.query("select Host from mysql.user where User='" +
username + "' AND Host!='localhost'")
isError = isSqlError(users)
if isError != None:
return isError
users = mapToList(users)
if len(users) < 1:
return mw.returnJson(True, "127.0.0.1")
6 years ago
accs = []
for c in users:
accs.append(c[0])
userStr = ','.join(accs)
return mw.returnJson(True, userStr)
6 years ago
6 years ago
6 years ago
def toSize(size):
d = ('b', 'KB', 'MB', 'GB', 'TB')
s = d[0]
for b in d:
if size < 1024:
return str(size) + ' ' + b
size = size / 1024
s = b
6 years ago
_size = round(size, 2)
6 years ago
print size, _size
6 years ago
return str(size) + ' ' + b
6 years ago
6 years ago
6 years ago
def setDbAccess():
args = getArgs()
data = checkArgs(args, ['username', 'access'])
if not data[0]:
return data[1]
name = args['username']
access = args['access']
pdb = pMysqlDb()
psdb = pSqliteDb('databases')
dbname = psdb.where('username=?', (name,)).getField('name')
password = psdb.where("username=?", (name,)).getField('password')
users = pdb.query("select Host from mysql.user where User='" +
name + "' AND Host!='localhost'")
for us in users:
pdb.execute("drop user '" + name + "'@'" + us[0] + "'")
__createUser(dbname, name, password, access)
6 years ago
psdb.where('username=?', (name,)).save('accept', (access,))
return mw.returnJson(True, '设置成功!')
6 years ago
6 years ago
def getDbInfo():
args = getArgs()
data = checkArgs(args, ['name'])
if not data[0]:
return data[1]
db_name = args['name']
pdb = pMysqlDb()
# print 'show tables from `%s`' % db_name
table_res = pdb.query('show tables from `%s`' % db_name)
isError = isSqlError(table_res)
if isError != None:
return isError
tables = mapToList(table_res)
ret = {}
if type(tables) == list:
try:
data = mapToList(pdb.query(
"select sum(DATA_LENGTH)+sum(INDEX_LENGTH) from information_schema.tables where table_schema='%s'" % db_name))[0][0]
except:
data = 0
if not data:
data = 0
ret['data_size'] = mw.toSize(data)
6 years ago
# print ret
6 years ago
ret['database'] = db_name
ret3 = []
for i in tables:
if i == 1049:
return mw.returnJson(False, '指定数据库不存在!')
6 years ago
table = mapToList(
pdb.query("show table status from `%s` where name = '%s'" % (db_name, i[0])))
if not table:
continue
try:
ret2 = {}
ret2['type'] = table[0][1]
ret2['rows_count'] = table[0][4]
ret2['collation'] = table[0][14]
6 years ago
data_size = table[0][6] + table[0][8]
6 years ago
ret2['data_byte'] = data_size
ret2['data_size'] = mw.toSize(data_size)
6 years ago
ret2['table_name'] = i[0]
ret3.append(ret2)
except:
continue
ret['tables'] = (ret3)
return mw.getJson(ret)
6 years ago
def repairTable():
args = getArgs()
data = checkArgs(args, ['db_name', 'tables'])
if not data[0]:
return data[1]
db_name = args['db_name']
tables = json.loads(args['tables'])
pdb = pMysqlDb()
mysql_table = mapToList(pdb.query('show tables from `%s`' % db_name))
ret = []
if type(mysql_table) == list:
if len(mysql_table) > 0:
for i in mysql_table:
for i2 in tables:
if i2 == i[0]:
ret.append(i2)
if len(ret) > 0:
for i in ret:
pdb.execute('REPAIR TABLE `%s`.`%s`' % (db_name, i))
return mw.returnJson(True, "修复完成!")
return mw.returnJson(False, "修复失败!")
def optTable():
args = getArgs()
data = checkArgs(args, ['db_name', 'tables'])
if not data[0]:
return data[1]
db_name = args['db_name']
tables = json.loads(args['tables'])
pdb = pMysqlDb()
mysql_table = mapToList(pdb.query('show tables from `%s`' % db_name))
ret = []
if type(mysql_table) == list:
if len(mysql_table) > 0:
for i in mysql_table:
for i2 in tables:
if i2 == i[0]:
ret.append(i2)
if len(ret) > 0:
for i in ret:
pdb.execute('OPTIMIZE TABLE `%s`.`%s`' % (db_name, i))
return mw.returnJson(True, "优化成功!")
return mw.returnJson(False, "优化失败或者已经优化过了!")
def alterTable():
args = getArgs()
data = checkArgs(args, ['db_name', 'tables'])
if not data[0]:
return data[1]
db_name = args['db_name']
tables = json.loads(args['tables'])
table_type = args['table_type']
pdb = pMysqlDb()
mysql_table = mapToList(pdb.query('show tables from `%s`' % db_name))
ret = []
if type(mysql_table) == list:
if len(mysql_table) > 0:
for i in mysql_table:
for i2 in tables:
if i2 == i[0]:
ret.append(i2)
if len(ret) > 0:
for i in ret:
pdb.execute('alter table `%s`.`%s` ENGINE=`%s`' %
(db_name, i, table_type))
return mw.returnJson(True, "更改成功!")
return mw.returnJson(False, "更改失败!")
6 years ago
def getTotalStatistics():
st = status()
data = {}
if st == 'start':
data['status'] = True
data['count'] = pSqliteDb('databases').count()
data['ver'] = mw.readFile(getServerDir() + '/version.pl').strip()
return mw.returnJson(True, 'ok', data)
6 years ago
else:
data['status'] = False
data['count'] = 0
return mw.returnJson(False, 'fail', data)
6 years ago
7 years ago
if __name__ == "__main__":
func = sys.argv[1]
6 years ago
if func == 'status':
7 years ago
print status()
elif func == 'start':
print start()
elif func == 'stop':
print stop()
elif func == 'restart':
print restart()
elif func == 'reload':
print reload()
6 years ago
elif func == 'initd_status':
print initdStatus()
elif func == 'initd_install':
print initdInstall()
elif func == 'initd_uninstall':
print initdUinstall()
elif func == 'run_info':
print runInfo()
6 years ago
elif func == 'db_status':
print myDbStatus()
elif func == 'set_db_status':
print setDbStatus()
6 years ago
elif func == 'conf':
print getConf()
6 years ago
elif func == 'bin_log':
print binLog()
6 years ago
elif func == 'error_log':
print getErrorLog()
6 years ago
elif func == 'show_log':
6 years ago
print getShowLogFile()
elif func == 'my_db_pos':
print getMyDbPos()
elif func == 'set_db_pos':
print setMyDbPos()
6 years ago
elif func == 'my_port':
print getMyPort()
elif func == 'set_my_port':
print setMyPort()
6 years ago
elif func == 'init_pwd':
print initMysqlPwd()
6 years ago
elif func == 'get_db_list':
print getDbList()
6 years ago
elif func == 'add_db':
print addDb()
elif func == 'del_db':
print delDb()
6 years ago
elif func == 'sync_get_databases':
print syncGetDatabases()
6 years ago
elif func == 'sync_to_databases':
print syncToDatabases()
6 years ago
elif func == 'set_root_pwd':
print setRootPwd()
6 years ago
elif func == 'set_user_pwd':
print setUserPwd()
elif func == 'get_db_access':
print getDbAccess()
6 years ago
elif func == 'set_db_access':
print setDbAccess()
elif func == 'set_db_ps':
print setDbPs()
6 years ago
elif func == 'get_db_info':
print getDbInfo()
elif func == 'repair_table':
print repairTable()
elif func == 'opt_table':
print optTable()
elif func == 'alter_table':
print alterTable()
6 years ago
elif func == 'get_total_statistics':
print getTotalStatistics()
6 years ago
else:
print 'error'