|
|
|
@ -8,6 +8,10 @@ import time |
|
|
|
|
sys.path.append(os.getcwd() + "/class/core") |
|
|
|
|
import public |
|
|
|
|
|
|
|
|
|
app_debug = False |
|
|
|
|
if public.isAppleSystem(): |
|
|
|
|
app_debug = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getPluginName(): |
|
|
|
|
return 'mysql' |
|
|
|
@ -44,137 +48,49 @@ def getArgs(): |
|
|
|
|
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) |
|
|
|
|
public.writeLog('TYPE_SOFE', 'MYSQL_CHECK_ERR') |
|
|
|
|
return True |
|
|
|
|
def getConf(): |
|
|
|
|
path = getServerDir() + '/conf/my.cnf' |
|
|
|
|
return path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getInitdTpl(): |
|
|
|
|
path = getPluginDir() + '/init.d/mysql.tpl' |
|
|
|
|
return path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def contentReplace(content): |
|
|
|
|
service_path = public.getServerDir() |
|
|
|
|
content = content.replace('{$ROOT_PATH}', public.getRootDir()) |
|
|
|
|
content = content.replace('{$SERVER_PATH}', service_path) |
|
|
|
|
return content |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = public.readFile(initd_tpl) |
|
|
|
|
content = contentReplace(content) |
|
|
|
|
public.writeFile(file_bin, content) |
|
|
|
|
public.execShell('chmod +x ' + file_bin) |
|
|
|
|
|
|
|
|
|
mysql_conf_dir = getServerDir() + '/conf' |
|
|
|
|
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 = public.readFile(mysql_conf_tpl) |
|
|
|
|
content = contentReplace(content) |
|
|
|
|
public.writeFile(mysql_conf, content) |
|
|
|
|
|
|
|
|
|
return file_bin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def status(): |
|
|
|
@ -186,6 +102,8 @@ def status(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start(): |
|
|
|
|
|
|
|
|
|
init_file = initDreplace() |
|
|
|
|
initMysql = getServerDir() + '/scripts/mysql_install_db ' + '--basedir=' + \ |
|
|
|
|
getServerDir() + ' --datadir=' + getServerDir() + '/bin/mysql/data' |
|
|
|
|
return initMysql |
|
|
|
@ -215,17 +133,42 @@ def reload(): |
|
|
|
|
return 'ok' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getConf(): |
|
|
|
|
path = os.path.dirname(os.getcwd()) + "/redis/redis.conf" |
|
|
|
|
return path |
|
|
|
|
def initdStatus(): |
|
|
|
|
if not app_debug: |
|
|
|
|
if public.isAppleSystem(): |
|
|
|
|
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 public.isAppleSystem(): |
|
|
|
|
return "Apple Computer does not support" |
|
|
|
|
|
|
|
|
|
mysql_bin = initDreplace() |
|
|
|
|
initd_bin = getInitDFile() |
|
|
|
|
shutil.copyfile(mysql_bin, initd_bin) |
|
|
|
|
public.execShell('chmod +x ' + initd_bin) |
|
|
|
|
return 'ok' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initdUinstall(): |
|
|
|
|
if not app_debug: |
|
|
|
|
if public.isAppleSystem(): |
|
|
|
|
return "Apple Computer does not support" |
|
|
|
|
initd_bin = getInitDFile() |
|
|
|
|
os.remove(initd_bin) |
|
|
|
|
return 'ok' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
func = sys.argv[1] |
|
|
|
|
if func == 'run_info': |
|
|
|
|
print runInfo() |
|
|
|
|
elif func == 'conf': |
|
|
|
|
print getConf() |
|
|
|
|
elif func == 'status': |
|
|
|
|
if func == 'status': |
|
|
|
|
print status() |
|
|
|
|
elif func == 'start': |
|
|
|
|
print start() |
|
|
|
@ -235,3 +178,15 @@ if __name__ == "__main__": |
|
|
|
|
print restart() |
|
|
|
|
elif func == 'reload': |
|
|
|
|
print reload() |
|
|
|
|
elif func == 'initd_status': |
|
|
|
|
print initdStatus() |
|
|
|
|
elif func == 'initd_install': |
|
|
|
|
print initdInstall() |
|
|
|
|
elif func == 'initd_uninstall': |
|
|
|
|
print initdUinstall() |
|
|
|
|
elif func == 'run_info': |
|
|
|
|
print runInfo() |
|
|
|
|
elif func == 'conf': |
|
|
|
|
print getConf() |
|
|
|
|
else: |
|
|
|
|
print 'error' |
|
|
|
|