Python脚本安装MySQL5.7

#!/usr/bin/env python

import os
import sys
import re

base_dir = '/opt/software/mysql-5.7.17-linux-glibc2.5-x86_64'

os.chdir('/opt/software')
tar_result = os.system('tar xvf %s &>/dev/null' % sys.argv[1])
if tar_result == 0:
    os.chdir('/usr/local')
    os.system('ln -s %s  mysql' % base_dir)
else:
    print "uncompress wrong"

user_result = os.system('id mysql')
if user_result==0:
    print "user exist"
else:
    os.system('groupadd mysql')
    os.system('useradd -g mysql -M -s /sbin/login mysql')

os.system('mkdir -p /data/mysql/mysql3306/{data,logs,tmp}')
os.system('chown -R mysql:mysql /data/mysql/mysql3306/')
os.system('chown -R mysql:mysql /opt/software/mysql-5.7.17-linux-glibc2.5-x86_64')
os.system('chown -R mysql:mysql /usr/local/mysql')
os.chdir('/opt/software')
os.system('cp my.cnf /etc/my.cnf')
os.chdir('/usr/local/mysql')
init = os.system('./bin/mysqld --initialize')
if init == 0:
    with open('/data/mysql/mysql3306/data/error.log') as fobj:
        for line in fobj:
            if 'root@localhost' in line:
                m = re.search('(root@localhost:)(.+)',line)
                if m:
                    passwd = m.group(2)
                    print "password:%s" % passwd
os.system('cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld')
os.system('export PATH=$PATH:/usr/local/mysql/bin')
with open('/etc/profile','a+') as profile:
    profile.write('\nPATH=$PATH:/usr/local/mysql/bin')

二进制安装MySQL步骤

1.下载MySQL二进制安装包,并解压

2.软连接到/usr/local/mysql

3.创建MySQL组和用户

4.创建配置文件

5.基于配置文件创建基本目录

6.初始化数据库

7.得到密码

8.创建启动脚本

9.添加环境变量

10.修改root密码

其它的操作看之前的文章  二进制安装MySQL

脚本不完善的地方请大家伙帮忙改进下

 

posted @ 2017-03-29 16:56  Time.catcher  阅读(1856)  评论(0编辑  收藏  举报