One-click installation of MySQL Python program script
Main function: One-click Install MYSL server side, MySQL client side.
Two necessary files are required before running the script
Mysql.tar.gz: The file is compiled after the file compression package, it is important to note that this package is not compressed directory, directory files and sub-directory compression.
For example, the Basedir directory is/usr/local/mysql and does not need to compress the entire MySQL directory, compressing the files and subdirectories inside it,
Cd/usr/local/mysql TAR-ZCVF mysql.tar.gz *
After decompression is a lot of files and directories, not a directory
MY.CNF: Profile template, this template can be defined by itself
[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
The following is a concrete implementation script
#!/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"
# configuration file, source file path
_dir= '/opt/files ' # store some necessary files, for example, mysql.tar.gz MY.CND
_config= ' my.cnf '
_mysqlfile= ' mysql.tar.gz '
Datadir= '/opt/mysql/data ' # Data directory needs to be consistent with DataDir in MY.CNF
Basedir= '/usr/local/mysql ' # program installation directory needs to be consistent with Basedir in MY.CNF
Binlog= '/opt/mysql/binlog ' # This is Binlog's storage directory
Tmpdir= '/opt/mysql/tmp ' # mysql temp directory
# Judge 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= "Missing mysql.tar.gz or my.cnf"
print '%s:%s '% (time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()), E)
Sys.exit ()
# Determine if MySQL is running, force off MySQL uninstall MySQL
Def mysqlserver ():
cmd= "PS aux | Grep-i MySQL | Grep-v ' grep ' | awk ' {print $} '
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)
# Determine if the data directory exists, and the directory owner
Def dir_data ():
If Os.path.exists (datadir):
Cmd= "Rm-rf%s"% (DataDir)
Subprocess.call (Cmd,shell=true)
Os.makedirs (DataDir)
print '%s: Create%s directory '% (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: Create%s directory '% (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()), Basedir)
print '%s: Extract 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: Create%s directory '% (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: Create%s directory '% (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)
# Determine if the user
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)
# Unzip 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 '))
# Configure 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 $} '
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)
# turn and start 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)
# check MySQL status
Def checkstatus ():
cmd= "PS aux | Grep-i MySQL | Grep-v ' grep ' | awk ' {print $} ' | 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 installation succeeded"
Else
E= "MySQL installation Failed"
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
This article is from the "SQL Server MySQL" blog, so be sure to keep this source http://dwchaoyue.blog.51cto.com/2826417/1860162
Install MySQL with one click