一键安装mysql
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了一键安装mysql,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4534字,纯文字阅读大概需要7分钟。
内容图文
![一键安装mysql](/upload/InfoBanner/zyjiaocheng/471/e966dac24bbc459aa02335430abfa6c9.jpg)
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 安装
本文系统来源:http://dwchaoyue.blog.51cto.com/2826417/1860162
内容总结
以上是互联网集市为您收集整理的一键安装mysql全部内容,希望文章能够帮你解决一键安装mysql所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。