MySQL多实例管理 for python脚本

#!/usr/bin/env python
#! _*_ coding:utf-8 _*_

#注:多实例DB数据,my.conf,sock文件目录要统一
#每个实例要建有shutdown权限mt_user用户

import os,sys
import socket

myd = '/usr/local/mysql/bin/mysqld'
myadmin = '/usr/local/mysql/bin/mysqladmin'
m_user = 'mt_user'
m_password = 'password'

def IsOpen(ip,port):
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        s.connect((ip,int(port)))
        s.shutdown(2)
        return True
    except:
        return False

result = IsOpen('127.0.0.1',sys.argv[2])

if sys.argv[1] == 'start':
    if result:
        print('Sorry that the port is occupied!')
    else:
        os.system(myd+ ' --defaults-file=/data/mysql/mysql'+sys.argv[2]+'/my.cnf&')
elif sys.argv[1] == 'restart':
    os.system('%s -u%s -p%s -S /tmp/mysql%s.sock shutdown' %(myadmin,m_user,m_password,sys.argv[2]))
    os.system('sleep 2')
    os.system(myd+ ' --defaults-file=/data/mysql/mysql'+sys.argv[2]+'/my.cnf&')
elif sys.argv[1] == 'stop':
    os.system('%s -u%s -p%s -S /tmp/mysql%s.sock shutdown' %(myadmin,m_user,m_password,sys.argv[2]))
else:
    print('Please enter ===python %s start+port or restart+port or stop+port===' %sys.argv[0])
 
 
#python mysql.py start 3306
#python mysql.py start 3307
 
#python mysql.py restart 3306
#python mysql.py restart 3307

#python mysql.py stop 3306
#python mysql.py stop 3307

0 个评论

要回复文章请先登录注册