python MySQL自动安装

回复 收藏
本帖最后由 krven 于 2016-8-9 09:05 编辑
  1. <div class="blockcode">#!/usr/bin/python
  2. #encoding:utf-8
  3. import urllib
  4. import os,string
  5. import sys
  6. import shutil
  7. import re
  8. import time
  9. #import MySQLdb
  10. raw_input_A = raw_input('''请输入需要执行的操作:
  11. 1,请输入阿拉伯数字1进行数据库初始创建相应的文件夹:
  12. 2,请输入阿拉伯数字2进行数据库配置文件更改目录:
  13. 3,请输入阿拉伯数字3进行数据库下载:
  14. 4,请输入阿拉伯数字4进行数据库解压缩和移动到相应目录:
  15. 5,请输入阿拉伯数字5进行数据库初始化:
  16. 6,请输入阿拉伯数字6进行开启数据库:
  17. 7,请输入阿拉伯数字7进行密码更改:
  18. 8,请输入阿拉伯数字8进行全新安装:
  19. 9,请输入quit退出''')
  20. def installstart():
  21.     print '欢迎安装mysql'
  22.     os.chdir('/etc/')
  23.     f = open('passwd')
  24.     l = f.read()
  25.     if 'mysql' in l:
  26.         print 'the user is created'
  27.     else:
  28.         print 'the user is not created'
  29.         os.system("useradd %s -s /bin/bash" % 'mysql')
  30.     f.close()
  31.     os.chdir('/data/')
  32.     if not os.path.exists('mysql'):
  33.         os.mkdir('mysql')
  34.     elif not os.path.exists('/jiaoben'):
  35.         os.mkdir('/jiaoben')
  36.     raw_input_A
  37. def mycnf():
  38.     os.chdir('/etc/')
  39.     if os.path.exists('my.cnf'):
  40.         shutil.move("my.cnf", '/etc/my.cnf.bak')
  41.         shutil.copy('/jiaoben/my.cnf','/etc/')
  42.     __mycnf= open('/etc/my.cnf', 'r+')
  43.     peizhi = __mycnf.read()
  44.     if 'basedir =' in peizhi:
  45.         __mycnf.seek(0)
  46.         __mycnf.write(peizhi.replace('basedir =','basedir=/usr/local/mysql555').replace('datadir =','datadir=/data/mysql'))
  47.     raw_input_A
  48. def wgetmysql():
  49.     os.chdir('/opt')
  50.     if os.path.exists('mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz'):
  51.         print 'the file is ok'
  52.     else:
  53.         def Schedule(a,b,c):
  54.            percent = int(a*b*100/c)
  55.            sys.stdout.write("\r%d%%" % percent + ' complete')
  56.            sys.stdout.flush()
  57. #        def Schedule(a,b,c):
  58. #            per = 100.0 * a * b / c
  59. #            for per in xrange(0, 100):
  60. #                per = 100
  61. #            print '\r%.2f%%' %per
  62. #            sys.stdout.flush()
  63.         url = 'http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz'
  64.         local = os.path.join('/opt/','mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz')
  65.         urllib.urlretrieve(url,local,Schedule)
  66. #############################################################
  67. def mysqlnew():
  68.     print 'input the opt'
  69.     os.chdir("/opt/")
  70.     if os.path.isdir('mysql-5.7.13-linux-glibc2.5-x86_64'):
  71.         print '开始移动数据库到/usr/local下请等待。。。。'
  72.         shutil.move("mysql-5.7.13-linux-glibc2.5-x86_64","/usr/local/mysql555")
  73.     else:
  74.         os.chdir("/opt/")
  75.         try:
  76.             print '开始解压缩数据库'
  77.             os.system('tar zxvf %s' % 'mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz')
  78.         except Exception,ex:
  79.             print Exception,":",ex
  80.             print 'tar scuccess'
  81.         try:
  82.             print '开始移动数据库到/usr/local下请等待。。。。'
  83.             shutil.move('mysql-5.7.13-linux-glibc2.5-x86_64','/usr/local/mysql555')
  84.         except Exception,ex:
  85.             print Exception,":",ex
  86.             print '数据创建成功'
  87.     raw_input_A
  88. ########################################################################
  89. def MySqlin():
  90.     print 'kai shi'
  91.     os.chdir("/usr/local/")
  92.     if os.path.isdir("mysql555"):
  93.         print 'the mysql555 is ok'
  94.         try:
  95.             os.chdir("mysql555")
  96.         except Exception,ex:
  97.             print Exception,":",ex
  98.         try:
  99.             print '初始化数据库'
  100.             os.system('./bin/mysqld  --initialize --user=mysql --datadir=/data/mysql 2&gt;&gt;/jiaoben/mypassword.txt')
  101.         except Exception,ex:
  102.             print Exception,":",ex
  103.     print '替换mysql.server下的basedir和datadir'
  104.     os.chdir('/usr/local/')
  105.     if os.path.isdir("mysql555"):
  106.         os.chdir('mysql555')
  107.         os.system('./bin/mysql_ssl_rsa_setup --datadir=/data/mysql')
  108.         __myserver= open('/usr/local/mysql555/support-files/mysql.server', 'r+')
  109.         peiserver = __myserver.read()
  110.         __myserver.seek(0)
  111.         __myserver.write(peiserver.replace('basedir=','basedir=/usr/local/mysql555', 2).replace('datadir=','datadir=/data/mysql', 1))
  112.     raw_input_A
  113. def mysqlstart():
  114.     print '数据库启动'
  115.     os.system('/usr/local/mysql555/support-files/mysql.server start')
  116.     raw_input_A
  117. def mysqlpasswd():
  118.     print '更改密码'
  119.     os.chdir("/jiaoben/")
  120.     mypasswd = open('mypassword.txt','r+')
  121.     pass12 = mypasswd.readlines()
  122.     for i in pass12:
  123.         i = "".join(i.strip())
  124.         i = i.replace(" ","")
  125.         if 'password' in i:
  126.             pspilt = i.split(':')
  127.             pa1 = pspilt[-1]
  128.             os.system("/usr/local/mysql5.7.9/bin/mysqladmin -h localhost  -u root password '123#@!' -p'%s'" % pa1)
  129.     raw_input_A
  130. if raw_input_A == str(1):
  131.     installstart()
  132.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
  133. elif raw_input_A == str(2):
  134.     mycnf()
  135.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
  136. elif raw_input_A == str(3):
  137.     wgetmysql()
  138.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
  139. elif raw_input_A == str(4):
  140.     mysqlnew()
  141.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
  142. elif raw_input_A == str(5):
  143.     MySqlin()
  144.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
  145. elif raw_input_A == str(6):
  146.     mysqlstart()
  147.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
  148. elif raw_input_A == str(7):
  149.     mysqlpasswd()
  150.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')
  151. elif raw_input_A == str(8):
  152.     installstart()
  153.     mycnf()
  154.     wgetmysql()
  155.     mysqlnew()
  156.     MySqlin()
  157.     mysqlstart()
  158.     mysqlpasswd()
  159.     os.system('/usr/bin/python /root/pythonex/mysqlinstall.py')   
  160. #mysqlnew()
  161. #MySqlin()
  162. #mysqlstart()
  163. #mysqlpasswd()
  164. #      #db = MySQLdb.connect(host="localhost",user="root",passwd=)
  165. #       cursor = db.cursor()
  166. #       sql = "set password = password('mypass');"
  167. #    try:
  168. #       cursor.execute(sql)
  169. #       db.commit()
  170. #    except:
  171. #   # Rollback in case there is any error
  172. #       db.rollback()
  173. #       db.close()
  174. #else:
  175. #    mysqlnew()

2016-08-05 13:34 举报
已邀请:
0

krven

赞同来自:

修改部分代码
28到32行
__mycnf= open('/etc/my.cnf', 'r+')
__peizhi = __mycnf.read()
__mycnf.seek(0)
__mycnf.write(__peizhi.replace('basedir =','basedir=/usr/local/mysql555/').replace('datadir =','datadir=/data/mysql'))

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: