本帖最后由 krven 于 2016-8-9 09:05 编辑
- <div class="blockcode">#!/usr/bin/python
- #encoding:utf-8
- import urllib
- import os,string
- import sys
- import shutil
- import re
- import time
- #import MySQLdb
- raw_input_A = raw_input('''请输入需要执行的操作:
- 1,请输入阿拉伯数字1进行数据库初始创建相应的文件夹:
- 2,请输入阿拉伯数字2进行数据库配置文件更改目录:
- 3,请输入阿拉伯数字3进行数据库下载:
- 4,请输入阿拉伯数字4进行数据库解压缩和移动到相应目录:
- 5,请输入阿拉伯数字5进行数据库初始化:
- 6,请输入阿拉伯数字6进行开启数据库:
- 7,请输入阿拉伯数字7进行密码更改:
- 8,请输入阿拉伯数字8进行全新安装:
- 9,请输入quit退出''')
- def installstart():
- print '欢迎安装mysql'
- os.chdir('/etc/')
- f = open('passwd')
- l = f.read()
- if 'mysql' in l:
- print 'the user is created'
- else:
- print 'the user is not created'
- os.system("useradd %s -s /bin/bash" % 'mysql')
- f.close()
- os.chdir('/data/')
- if not os.path.exists('mysql'):
- os.mkdir('mysql')
- elif not os.path.exists('/jiaoben'):
- os.mkdir('/jiaoben')
- raw_input_A
- def mycnf():
- os.chdir('/etc/')
- if os.path.exists('my.cnf'):
- shutil.move("my.cnf", '/etc/my.cnf.bak')
- shutil.copy('/jiaoben/my.cnf','/etc/')
- __mycnf= open('/etc/my.cnf', 'r+')
- peizhi = __mycnf.read()
- if 'basedir =' in peizhi:
- __mycnf.seek(0)
- __mycnf.write(peizhi.replace('basedir =','basedir=/usr/local/mysql555').replace('datadir =','datadir=/data/mysql'))
- raw_input_A
- def wgetmysql():
- os.chdir('/opt')
- if os.path.exists('mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz'):
- print 'the file is ok'
- else:
- def Schedule(a,b,c):
- percent = int(a*b*100/c)
- sys.stdout.write("\r%d%%" % percent + ' complete')
- sys.stdout.flush()
- # def Schedule(a,b,c):
- # per = 100.0 * a * b / c
- # for per in xrange(0, 100):
- # per = 100
- # print '\r%.2f%%' %per
- # sys.stdout.flush()
- url = 'http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz'
- local = os.path.join('/opt/','mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz')
- urllib.urlretrieve(url,local,Schedule)
- #############################################################
- def mysqlnew():
- print 'input the opt'
- os.chdir("/opt/")
- if os.path.isdir('mysql-5.7.13-linux-glibc2.5-x86_64'):
- print '开始移动数据库到/usr/local下请等待。。。。'
- shutil.move("mysql-5.7.13-linux-glibc2.5-x86_64","/usr/local/mysql555")
- else:
- os.chdir("/opt/")
- try:
- print '开始解压缩数据库'
- os.system('tar zxvf %s' % 'mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz')
- except Exception,ex:
- print Exception,":",ex
- print 'tar scuccess'
- try:
- print '开始移动数据库到/usr/local下请等待。。。。'
- shutil.move('mysql-5.7.13-linux-glibc2.5-x86_64','/usr/local/mysql555')
- except Exception,ex:
- print Exception,":",ex
- print '数据创建成功'
- raw_input_A
- ########################################################################
- def MySqlin():
- print 'kai shi'
- os.chdir("/usr/local/")
- if os.path.isdir("mysql555"):
- print 'the mysql555 is ok'
- try:
- os.chdir("mysql555")
- except Exception,ex:
- print Exception,":",ex
- try:
- print '初始化数据库'
- os.system('./bin/mysqld --initialize --user=mysql --datadir=/data/mysql 2>>/jiaoben/mypassword.txt')
- except Exception,ex:
- print Exception,":",ex
- print '替换mysql.server下的basedir和datadir'
- os.chdir('/usr/local/')
- if os.path.isdir("mysql555"):
- os.chdir('mysql555')
- os.system('./bin/mysql_ssl_rsa_setup --datadir=/data/mysql')
- __myserver= open('/usr/local/mysql555/support-files/mysql.server', 'r+')
- peiserver = __myserver.read()
- __myserver.seek(0)
- __myserver.write(peiserver.replace('basedir=','basedir=/usr/local/mysql555', 2).replace('datadir=','datadir=/data/mysql', 1))
- raw_input_A
- def mysqlstart():
- print '数据库启动'
- os.system('/usr/local/mysql555/support-files/mysql.server start')
- raw_input_A
- def mysqlpasswd():
- print '更改密码'
- os.chdir("/jiaoben/")
- mypasswd = open('mypassword.txt','r+')
- pass12 = mypasswd.readlines()
- for i in pass12:
- i = "".join(i.strip())
- i = i.replace(" ","")
- if 'password' in i:
- pspilt = i.split(':')
- pa1 = pspilt[-1]
- os.system("/usr/local/mysql5.7.9/bin/mysqladmin -h localhost -u root password '123#@!' -p'%s'" % pa1)
- raw_input_A
- if raw_input_A == str(1):
- installstart()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- elif raw_input_A == str(2):
- mycnf()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- elif raw_input_A == str(3):
- wgetmysql()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- elif raw_input_A == str(4):
- mysqlnew()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- elif raw_input_A == str(5):
- MySqlin()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- elif raw_input_A == str(6):
- mysqlstart()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- elif raw_input_A == str(7):
- mysqlpasswd()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- elif raw_input_A == str(8):
- installstart()
- mycnf()
- wgetmysql()
- mysqlnew()
- MySqlin()
- mysqlstart()
- mysqlpasswd()
- os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
- #mysqlnew()
- #MySqlin()
- #mysqlstart()
- #mysqlpasswd()
- # #db = MySQLdb.connect(host="localhost",user="root",passwd=)
- # cursor = db.cursor()
- # sql = "set password = password('mypass');"
- # try:
- # cursor.execute(sql)
- # db.commit()
- # except:
- # # Rollback in case there is any error
- # db.rollback()
- # db.close()
- #else:
- # mysqlnew()
编辑回复