时间:2021-07-01 10:21:17 帮助过:5人阅读
sys.exit()
# 判断mysql 是否 正在运行 , 强制关闭mysql 卸载mysql
def mysqlserver():
cmd="ps aux | grep -i mysql | grep -v ‘grep‘ | awk ‘{print $2}‘ "
thread=subprocess.Popen(cmd,shell=True,stderr=open(‘/dev/null‘,‘w‘),stdout=subprocess.PIPE).stdout.read()
if thread <> "" :
list_thread=thread.split(‘\n‘)
for i in range(len(list_thread)-1):
cmd="kill -9 %s"%(list_thread[i])
print ‘%s:%s‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),cmd)
subprocess.call(cmd,shell=True)
if os.path.exists("/etc/init.d/mysql"):
cmd="rm -rf /etc/init.d/mysql"
print ‘%s:%s‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),cmd)
subprocess.call(cmd,shell=True)
cmd="chkconfig --list | grep -w mysql | wc -l"
num=subprocess.Popen(cmd,shell=True,stderr=open(‘/dev/null‘,‘w‘),stdout=subprocess.PIPE).stdout.read().replace(‘\n‘,‘‘)
if int(num) > 1:
cmd="chkconfig --del mysql"
print ‘%s:%s‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),cmd)
subprocess.call(cmd,shell=True)
# 判断数据目录是否存在,以及目录所有者
def dir_data():
if os.path.exists(datadir):
cmd="rm -rf %s " % (datadir)
subprocess.call(cmd,shell=True)
os.makedirs(datadir)
print ‘%s:创建%s目录‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),datadir)
if os.path.exists(basedir):
cmd="rm -rf %s " % (basedir)
subprocess.call(cmd,shell=True)
os.makedirs(basedir)
print ‘%s:创建%s目录‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),basedir)
print ‘%s:解压mysql.tar.gz‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()))
tar()
if os.path.exists(binlog):
cmd="rm -rf %s " % (binlog)
subprocess.call(cmd,shell=True)
os.makedirs(binlog)
print ‘%s:创建%s目录‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),binlog)
if os.path.exists(tmpdir):
cmd="rm -rf %s " % (tmpdir)
subprocess.call(cmd,shell=True)
os.makedirs(tmpdir)
print ‘%s:创建%s目录‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),tmpdir)
cmd="chown -R mysql:mysql %s %s %s %s" % (datadir,basedir,binlog,tmpdir)
subprocess.call(cmd,shell=True)
# 判断用户是否
def user():
cmd="more /etc/passwd | grep -i mysql | wc -l"
num=subprocess.Popen(cmd,shell=True,stderr=open(‘/dev/null‘,‘w‘),stdout=subprocess.PIPE).stdout.read()
if int(num) < 1 :
cmd="groupadd mysql ;useradd -g mysql mysql "
print ‘%s:%s‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),cmd)
subprocess.call(cmd,shell=True)
# 解压 mysql.tar.gz
def tar():
os.chdir(_dir)
cmd="tar -xvf mysql.tar.gz -C %s" % (basedir)
subprocess.call(cmd,shell=True,stdout=open(‘/dev/null‘,‘w‘))
# 配置mysql
def config():
shutil.copy(‘%s/%s‘ % (_dir,_config),‘/etc/my.cnf‘)
cmd=" more /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -i IPADDR | awk -F ‘=‘ ‘{print $2}‘"
serverid=subprocess.Popen(cmd,shell=True,stderr=open(‘/dev/null‘,‘w‘),stdout=subprocess.PIPE).stdout.read().replace(‘.‘,‘‘).replace(‘\n‘,‘‘)
cmd="sed -i ‘s/123456789/ %s " %(serverid) + " /g‘ /etc/my.cnf"
subprocess.call(cmd,shell=True)
# 安转并 启动mysql
def startmysql():
os.chdir(basedir)
cmd=‘scripts/mysql_install_db --basedir=%s --datadir=%s --user=mysql --force‘ % (basedir,datadir)
subprocess.call(cmd,shell=True,stdout=open(‘/dev/null‘,‘w‘))
cmd="cp support-files/mysql.server /etc/init.d/mysql"
subprocess.call(cmd,shell=True)
cmd="chkconfig mysql on"
subprocess.call(cmd,shell=True)
cmd="service mysql start"
subprocess.call(cmd,shell=True)
# 检查mysql 状态
def checkstatus():
cmd="ps aux | grep -i mysql | grep -v ‘grep‘ | awk ‘{print $2}‘ | wc -l "
thread=subprocess.Popen(cmd,shell=True,stderr=open(‘/dev/null‘,‘w‘),stdout=subprocess.PIPE).stdout.read().replace("\n","")
if int(thread) > 0 :
e="mysql 安装成功"
else:
e="mysql 安装失败"
print ‘%s:%s‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),e)
#安装mysql客服端
def installclient():
str = "%s/bin/mysql" % (basedir)
shutil.copy(str,‘/usr/bin/mysql‘)
if __name__ == "__main__":
try:
file_exists()
mysqlserver()
user()
dir_data()
config()
startmysql()
checkstatus()
installclient()
except Exception,e:
print e
本文出自 “SQLServer MySQL” 博客,请务必保留此出处http://dwchaoyue.blog.51cto.com/2826417/1860162
一键安装mysql
标签:python mysql 安装