MySQL One-click Install Shell Script

Source: Internet
Author: User
Tags egrep

#!/bin/bash#data:20171104#qq:654996537#name:zhang# determine if root user if [ $ (id -u)  !=  "0"  ]; then    echo  " Not the root user! Try  using sudo command !  "    exit 1fi# Install dependency package yum -y  groupinstall  "Desktop platform development" Yum install  -y  gd-devel   libjpeg-devel  libpng-devel  libxml2-devel bzip2-devel libcurl-devel   libmcrypt  libmcrypt-devel  zlib-devel  net-snmp-devel openssl   cmake  ncurses-devel  git  gcc  libevent-devel# Custom source package Name package_type= ". tar.gz" package_version= "mysql-5.6.31" install_package= $Package _version$package_typesetup _path= "/root/" #自定义用户和用户组Group_Name = "MySQL" user_name= "mysql" #自定义RPM包RPM = "$User _name" #自定义MySql数据库保存路径 # # # # #Mydata = "/mydata/{data,bin-log}" #mkdir  /Mydata#mkdir /mydata/data#mkdir /mydata/bin-logmkdir /mydatamkdir /mydata/{data,bin-log} Datadir= "/mydata/data" #自定义MySql配置文件路径MySql_Conf = "/etc/mysql" #自定义MySql安装路径Install_prefix = "/usr/local/mysql" # Determine if the RPM package is installed Function rpm_install () {rpm -qa | egrep  "$RPM"  >>/dev/nullif  [ $? -eq 0 ]thenecho  "$RPM  is install yes." else echo  "$RPM  is not install." fi}rpm_install# Judging users and user groups Add_group_user () {egrep  "^ $Group _name"  /etc/group >& /dev/ nullif [ $? -ne 0 ]then    echo  "mysql  user group is being added."     groupadd  $Group _nameelse    echo  " The  $Group _ Name user group already exists. "     echo  "mysql  user group has been added." fi# determine if the MySQL user exists egrep  "^ $User _name"  /etc/passwd >& /dev/nullif [ $? -ne 0 ]then    echo  "mysql  user is adding."     useradd -g  $Group _name  $User _nameelse    echo  "mysql  user has been added."     echo  " The  $User _name user already exists." fi}add_group_user# determine if the source package file exists if [ -e  $Package _name ]then         echo  "$Package _name the package exists." else        echo  "$Package _name the package does  not exist. " fi# Compile and install mysqlcd  $Setup _pathtar -zxvf  $Install _packagecd  $Package _versionconfigure_opts= (  -dcmake_intall_prefix= $Install _prefix -dsysconfdir= $MySql _conf  -dmysql_datadir= $DataDir  -dwith_innobase_storage_engine=1  -dwith_archive_storaage_engine=1  -dwith_readline= 1  -dwith_openssl=system  -dwith_zlIb=system  -dwith_libwrap=0  -dmysql_unix_addr=/tmp/mysql.sock   -ddefault_ CHARSET=UTF8&NBSP;-DDEFAULT_COLLATION=UTF8_GENERAL_CI) cmake . ${configure_opts[@]} #判断是否编译成功if  [ [ $? -eq 0 ]] thenmake && make installelseecho  "compilation failed, recompile"  && exit 1fi# Modify MySQL group and data Catalog cd  $Install _prefixchown :mysql ./* -rchown -r mysql:mysql $ datadirchown -r mysql:mysql /mydata/bin-log# Initializing Database $install_prefix/scripts/mysql_install_db  --user=mysql --datadir= $DataDir # To determine if the initialization was successful, the directory is not empty, the directory is empty if [  "$ (ls -a $ DataDir) " ]; thenecho " $DataDir  is not empty "elseecho " $DataDir  is  empty "fi# provides service script cp  $Install _prefix/support-files/mysql.server /etc/rc.d/init.d/mysqldchmod  +x /etc/rc.d/init.d/mysqldchkconfig --add mysqld# provides configuration file #mv /etc/my.cnf /etc/my.cnf.bak#mv /etc/my.cnf.d /etc/my.cnf.d.bakif [ -e /etc/my.cnf ]thenecho  " File exists. " mv /etc/my.cnf /etc/my.cnf.bakelseecho  "File does not exist." fi#if [ -e /etc/my.cnf.d ] #then #        echo  "File exists." #        mv /etc/my.cnf.d /etc/my.cnf.d.bak#else#         echo  "File does not exist." #fimkdir   $MYSQL _confcp  $Install _prefix/support-files/my-default.cnf  /etc/mysql/my.cnfsed  -i  '/datadir/a\datadir = /mydata/data '  /etc/mysql/my.cnfsed -i  '/log_bin/a \log_bin = /mydata/bin-log/mysql-bin '  /etc/mysql/my.cnfsed -i  ' $ a\innodb_file_ Per_table = 1 '  /etc/mysql/my.cnf# Modify environment variables cat >/etc/profile.d/mysql.sh <<  eofexport path=/usr/local/mysql/bin/: $PATHEOFsource  /etc/profile.d/mysql.shsource /etc/profileman -m /usr/ local/mysql/man/#启动MySql服务 #centos6 Start mode #service mysqld start#centos7 start Mode #systemctl start  mysqld.service/etc/rc.d/init.d/mysqld start# determine if MySQL starts mysql_running ()  {startup_file= "/etc/rc.d/ Init.d/mysqld "#通过查看进程判断是否正常启动proc = ' ps -ef | grep mysql | grep -v grep|  wc -l ' #通过监听3306端口进行判断是否启动port = ' netstat -lntup|grep 3306|wc -l ' if [  $proc  -eq 2 ] & [  $port  -eq 1 ]   then         echo  "mysql is running." echo  "' Ss -tnlp | grep mysql '" else         echo  "mysql is no running." #        $Startup _file startfi}mysql_running


This article is from a "self-made" blog, be sure to keep this source http://bosszhang.blog.51cto.com/2580498/1978995

MySQL One-click Install Shell Script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.