1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 1#!/usr/bin/env python
2
3import os
4import sys
5import re
6
7base_dir = '/opt/software/mysql-5.7.17-linux-glibc2.5-x86_64'
8
9os.chdir('/opt/software')
10tar_result = os.system('tar xvf %s &>/dev/null' % sys.argv[1])
11if tar_result == 0:
12 os.chdir('/usr/local')
13 os.system('ln -s %s mysql' % base_dir)
14else:
15 print "uncompress wrong"
16
17user_result = os.system('id mysql')
18if user_result==0:
19 print "user exist"
20else:
21 os.system('groupadd mysql')
22 os.system('useradd -g mysql -M -s /sbin/login mysql')
23
24os.system('mkdir -p /data/mysql/mysql3306/{data,logs,tmp}')
25os.system('chown -R mysql:mysql /data/mysql/mysql3306/')
26os.system('chown -R mysql:mysql /opt/software/mysql-5.7.17-linux-glibc2.5-x86_64')
27os.system('chown -R mysql:mysql /usr/local/mysql')
28os.chdir('/opt/software')
29os.system('cp my.cnf /etc/my.cnf')
30os.chdir('/usr/local/mysql')
31init = os.system('./bin/mysqld --initialize')
32if init == 0:
33 with open('/data/mysql/mysql3306/data/error.log') as fobj:
34 for line in fobj:
35 if 'root@localhost' in line:
36 m = re.search('(root@localhost:)(.+)',line)
37 if m:
38 passwd = m.group(2)
39 print "password:%s" % passwd
40os.system('cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld')
41os.system('export PATH=$PATH:/usr/local/mysql/bin')
42with open('/etc/profile','a+') as profile:
43 profile.write('\nPATH=$PATH:/usr/local/mysql/bin')
44
二进制安装MySQL步骤
1.下载MySQL二进制安装包,并解压
2.软连接到/usr/local/mysql
3.创建MySQL组和用户
4.创建配置文件
5.基于配置文件创建基本目录
6.初始化数据库
7.得到密码
8.创建启动脚本
9.添加环境变量
10.修改root密码
其它的操作看之前的文章 二进制安装MySQL
脚本不完善的地方请大家伙帮忙改进下