一键安装MySQL python 程序脚本
主要功能:一键安装mysl服务器端,mysql client 端。
在运行该脚本之前需要两个必要的文件
mysql.tar.gz:该文件是编译完成后的文件压缩包,需要注意的是这个压缩包不是对目录的压缩,目录下面的文件以及子目录压缩.
例如,basedir 目录是/usr/local/mysql ,不需要对整个mysql目录进行压缩, 压缩里面的文件和子目录就行了,
cd /usr/local/mysql tar -zcvf mysql.tar.gz *
解压之后是许多的文件和目录,而不是一个目录
my.cnf:配置文件模板,这个模板可以自己定义
[client]
port=3306
socket=/opt/mysql/data/mysql.sock
[mysqld]
innodb_buffer_pool_size = 512M
log_bin = /opt/mysql/binlog/binlog
join_buffer_size = 1M
sort_buffer_size = 1M
read_rnd_buffer_size = 1M
datadir= /opt/mysql/data
basedir = /usr/local/mysql
tmpdir = /opt/mysql/tmp
sql_mode=‘‘
innodb_log_file_size = 1G
以下是具体实现脚本
#!/usr/bin/env python
#coding=utf-8
import os
import sys
import subprocess
import shutil
import time
os.environ["PATH"]="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
# 配置文件,源码文件路径
_dir=‘/opt/files‘ # 存放一些必要文件,例如,mysql.tar.gz my.cnd
_config=‘my.cnf‘
_mysqlfile=‘mysql.tar.gz‘
datadir=‘/opt/mysql/data‘ # 数据目录 需要和my.cnf 中的datadir一致
basedir=‘/usr/local/mysql‘ # 程序安装目录 需要和my.cnf中的basedir一致
binlog=‘/opt/mysql/binlog‘ # 这是binlog的存放目录
tmpdir=‘/opt/mysql/tmp‘ # mysql的临时目录
# 判断 mysql.tar.gz ,my.cnf
def file_exists():
if not os.path.exists("%s/%s" % (_dir,_config) ) or not os.path.exists("%s/%s" % (_dir,_mysqlfile)):
e="缺少 mysql.tar.gz 或者 my.cnf"
print ‘%s:%s‘ % (time.strftime(‘%Y-%M-%d %H:%M:%S‘,time.localtime()),e)
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
原文地址:http://dwchaoyue.blog.51cto.com/2826417/1860162