|
|
|
# coding:utf-8
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
sys.path.append(os.getcwd() + "/class/core")
|
|
|
|
import public
|
|
|
|
|
|
|
|
|
|
|
|
def getPluginName():
|
|
|
|
return 'mysql'
|
|
|
|
|
|
|
|
|
|
|
|
def getPluginDir():
|
|
|
|
return public.getPluginDir() + '/' + getPluginName()
|
|
|
|
|
|
|
|
|
|
|
|
def getServerDir():
|
|
|
|
return public.getServerDir() + '/' + getPluginName()
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def checkMyCnf():
|
|
|
|
# 处理MySQL配置文件
|
|
|
|
import os
|
|
|
|
confFile = '/etc/my.cnf'
|
|
|
|
if os.path.exists(confFile):
|
|
|
|
conf = readFile(confFile)
|
|
|
|
if len(conf) > 100:
|
|
|
|
return True
|
|
|
|
versionFile = '/www/server/mysql/version.pl'
|
|
|
|
if not os.path.exists(versionFile):
|
|
|
|
return False
|
|
|
|
|
|
|
|
versions = ['5.1', '5.5', '5.6', '5.7', 'AliSQL']
|
|
|
|
version = readFile(versionFile)
|
|
|
|
for key in versions:
|
|
|
|
if key in version:
|
|
|
|
version = key
|
|
|
|
break
|
|
|
|
|
|
|
|
shellStr = '''
|
|
|
|
#!/bin/bash
|
|
|
|
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
|
|
|
export PATH
|
|
|
|
|
|
|
|
CN='125.88.182.172'
|
|
|
|
HK='download.bt.cn'
|
|
|
|
HK2='103.224.251.67'
|
|
|
|
US='174.139.221.74'
|
|
|
|
sleep 0.5;
|
|
|
|
CN_PING=`ping -c 1 -w 1 $CN|grep time=|awk '{print $7}'|sed "s/time=//"`
|
|
|
|
HK_PING=`ping -c 1 -w 1 $HK|grep time=|awk '{print $7}'|sed "s/time=//"`
|
|
|
|
HK2_PING=`ping -c 1 -w 1 $HK2|grep time=|awk '{print $7}'|sed "s/time=//"`
|
|
|
|
US_PING=`ping -c 1 -w 1 $US|grep time=|awk '{print $7}'|sed "s/time=//"`
|
|
|
|
|
|
|
|
echo "$HK_PING $HK" > ping.pl
|
|
|
|
echo "$HK2_PING $HK2" >> ping.pl
|
|
|
|
echo "$US_PING $US" >> ping.pl
|
|
|
|
echo "$CN_PING $CN" >> ping.pl
|
|
|
|
nodeAddr=`sort -V ping.pl|sed -n '1p'|awk '{print $2}'`
|
|
|
|
if [ "$nodeAddr" == "" ];then
|
|
|
|
nodeAddr=$HK
|
|
|
|
fi
|
|
|
|
|
|
|
|
Download_Url=http://$nodeAddr:5880
|
|
|
|
|
|
|
|
|
|
|
|
MySQL_Opt()
|
|
|
|
{
|
|
|
|
MemTotal=`free -m | grep Mem | awk '{print $2}'`
|
|
|
|
if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
|
|
|
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
|
|
|
|
sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
|
|
|
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
|
|
|
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
|
|
|
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
|
|
|
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
|
|
|
|
sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
|
|
|
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf
|
|
|
|
elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
|
|
|
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
|
|
|
|
sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
|
|
|
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
|
|
|
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
|
|
|
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
|
|
|
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
|
|
|
|
sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
|
|
|
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
|
|
|
|
elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
|
|
|
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
|
|
|
|
sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
|
|
|
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
|
|
|
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
|
|
|
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
|
|
|
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
|
|
|
|
sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
|
|
|
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
|
|
|
|
elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then
|
|
|
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
|
|
|
|
sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
|
|
|
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
|
|
|
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
|
|
|
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
|
|
|
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
|
|
|
|
sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
|
|
|
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
|
|
|
|
elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then
|
|
|
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf
|
|
|
|
sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf
|
|
|
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf
|
|
|
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf
|
|
|
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf
|
|
|
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf
|
|
|
|
sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf
|
|
|
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf
|
|
|
|
elif [[ ${MemTotal} -ge 32768 ]]; then
|
|
|
|
sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf
|
|
|
|
sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf
|
|
|
|
sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf
|
|
|
|
sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf
|
|
|
|
sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf
|
|
|
|
sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf
|
|
|
|
sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf
|
|
|
|
sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf
|
|
|
|
sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
wget -O /etc/my.cnf $Download_Url/install/conf/mysql-%s.conf -T 5
|
|
|
|
MySQL_Opt
|
|
|
|
''' % (version,)
|
|
|
|
# 判断是否迁移目录
|
|
|
|
if os.path.exists('data/datadir.pl'):
|
|
|
|
newPath = public.readFile('data/datadir.pl')
|
|
|
|
mycnf = public.readFile('/etc/my.cnf')
|
|
|
|
mycnf = mycnf.replace('/www/server/data', newPath)
|
|
|
|
public.writeFile('/etc/my.cnf', mycnf)
|
|
|
|
|
|
|
|
os.system(shellStr)
|
|
|
|
WriteLog('TYPE_SOFE', 'MYSQL_CHECK_ERR')
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def status():
|
|
|
|
data = public.execShell(
|
|
|
|
"ps -ef|grep mysqld |grep -v grep | grep -v python | awk '{print $2}'")
|
|
|
|
if data[0] == '':
|
|
|
|
return 'stop'
|
|
|
|
return 'start'
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
initMysql = getServerDir() + '/scripts/mysql_install_db ' + '--basedir=' + \
|
|
|
|
getServerDir() + ' --datadir=' + getServerDir() + '/bin/mysql/data'
|
|
|
|
return initMysql
|
|
|
|
|
|
|
|
cmd = getServerDir() + '/bin/mysqld_safe ' + getServerDir() + \
|
|
|
|
'/conf/my.cnf ' + '--user=mysql&'
|
|
|
|
data = public.execShell(cmd)
|
|
|
|
if data[0] == '':
|
|
|
|
return 'stop'
|
|
|
|
return data[0]
|
|
|
|
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
data = public.execShell(
|
|
|
|
"ps -ef|grep mysql |grep -v grep |grep -v python |awk '{print $2}' | xargs kill -9")
|
|
|
|
if data[0] == '':
|
|
|
|
return 'ok'
|
|
|
|
return 'fail'
|
|
|
|
|
|
|
|
|
|
|
|
def restart():
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
|
|
|
|
def reload():
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
|
|
|
|
def getConf():
|
|
|
|
path = os.path.dirname(os.getcwd()) + "/redis/redis.conf"
|
|
|
|
return path
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
func = sys.argv[1]
|
|
|
|
if func == 'run_info':
|
|
|
|
print runInfo()
|
|
|
|
elif func == 'conf':
|
|
|
|
print getConf()
|
|
|
|
elif func == 'status':
|
|
|
|
print status()
|
|
|
|
elif func == 'start':
|
|
|
|
print start()
|
|
|
|
elif func == 'stop':
|
|
|
|
print stop()
|
|
|
|
elif func == 'restart':
|
|
|
|
print restart()
|
|
|
|
elif func == 'reload':
|
|
|
|
print reload()
|