因线上的生产环境用的大多是mysql5.1.55的版本,故本脚本暂时只支持5.1.55

5.6以上的版本的Mysql使用cmake进行安装,后续完成后会提供。。。。


   脚本提供了一个默认系统版本相同的前提下直接解压缩打好包的mysql的方式,应用于生产环境的快速部署中的,请无视。。。。


#!/bin/bash

#mysql_multi.sh

#Version:0.7

#Author:LvrenX

#Usage:mysql_multi.sh [PORT1] [PORT2]...[PORTN]  Example: mysql_multi.sh 3306 3307

#Description:     [0.2]  Append Puppet Transfer

#                 [0.3]  Append Config Check

#                 [0.4]  Perfect Compiler Options And Unified Version

#                 [0.5]  Change Database Path And Append Config Items   20130216

#                 [0.6]  Config File Remove Repeat  20130219

#                 [0.7]  Support Green Installation     20130220

#Datatime:20130220


#variables

MYSQL_PATH=/usr/local/mysql

CONFIG_FILE=/etc/my.cnf

PASSWORD=passwd

MYSQL_VERSION=mysql-5.1.55.tar.gz

DATA_PATH=/data/mysql



CONFIG=`cat <<EOF

[mysqld_multi]

mysqld     = /usr/local/mysql/bin/mysqld_safe

mysqladmin = /usr/local/mysql/bin/mysqladmin

user       = root

password   = ${PASSWORD}

EOF

`


#check mysql server

if [ -d "${MYSQL_PATH}" ];then

       echo "Service Mysql has Installed!"

       exit 0

else

       if [ -f "${CONFIG_FILE}" ];then

               echo "${CONFIG_FILE} has existed! Renamed It With ${CONFIG_FILE}.bak!"

               mv ${CONFIG_FILE} ${CONFIG_FILE}.bak

       else

               echo "Begin To Install MYSQL With Multi Instances!"

       fi

fi



cd /home

yum -y install ncurses-devel

groupadd mysql

useradd -g mysql -s /sbin/nologin -M mysql


if [ -f "/home/mysql-GI.tar.gz" ];then

       echo "-=-=-=-=-=-=-=-=-=-= Install Mysql With Portable Software Suit =-=-=-=-=-=-=-=-=-=-"

       mv /home/mysql-GI.tar.gz /usr/local/

       cd /usr/local

       tar zxvf mysql-GI.tar.gz

else

       echo "-=-=-=-=-=-=-=-=-=-= Install Mysql With Compile And Install =-=-=-=-=-=-=-=-=-=-"

       #install mysql

       if [ -f "/home/${MYSQL_VERSION}" ];then

         echo "${MYSQL_VERSION} has existed!"

       else

         wget http://downloads.skysql.com/archives/mysql-5.1/${MYSQL_VERSION}

       fi

       tar -zxvf ${MYSQL_VERSION}

       cd mysql-5.1.55

       ./configure --prefix=${MYSQL_PATH} --without-debug --with-unix-socket-path=/tmp/mysql.sock --enable-static  --with-big-tables --with-charset=utf8 --with-extra-charsets=all --with-plugins=all --enable-assembler --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-strtic  --enable-thread-safe-client

       #Optional configuration

       # --without-debug     ##去除debug模式

       # --with-unix-socket-path=/tmp/mysql.sock  ##在unix环境下的套接字链接提高7.5%性能

# --enable-static     ##静态编译,能够提高13%的性能

       # --with-big-tables          ##大表支持,支持更多数据

       # --with-charset=utf8     ##客户端默认连接方式编码

       # --with-extra-charsets=all     ##添加各种字符支持

       # --with-plugins=all     ##存储引擎

       # --enable-assembler     ##使用一些字符函数的汇编版本(优化性能)

       # --with-client-ldflags=-all-static          ##静态方式编译客户端

       # --with-mysqld-ldflags=-all-strtic          ##静态方式编译服务端

       # --enable-thread-safe-client  ##以线程的方式编译客户端

       make && make install

       mkdir ${MYSQL_PATH}/var

       chown mysql.mysql ${MYSQL_PATH}/var

       ${MYSQL_PATH}/bin/mysql_install_db --user=mysql

       ${MYSQL_PATH}/bin/mysqladmin password ${PASSWORD}

       #change config

       sed '/^#/d' ${MYSQL_PATH}/share/mysql/my-medium.cnf >${MYSQL_PATH}/share/mysql/my-multi.cnf

       sed -i '/mysql.sock$/a skip-innodb' ${MYSQL_PATH}/share/mysql/my-multi.cnf

       chown mysql.mysql ${MYSQL_PATH}/share/mysql/my-multi.cnf

fi


if [ `cat /etc/profile|grep -c "${MYSQL_PATH}"` == 0 ];then

       echo "export PATH=\$PATH:${MYSQL_PATH}/bin" >>/etc/profile

       source /etc/profile

else

       echo "The Envionment Variable Exists!"

fi


source /etc/profile



i=$#


if [ ${i} -eq 0 ];then

       echo "Please Input Mysql Port At Least One!"

               exit 0

else

   echo "${CONFIG}"|while read line;do

   echo "${line}" >>${MYSQL_PATH}/share/mysql/my-multi.cnf

   chown mysql.mysql ${MYSQL_PATH}/share/mysql/my-multi.cnf

       #Change Mysql Database Path

       mkdir -p ${DATA_PATH}

       cp -r ${MYSQL_PATH}/var ${DATA_PATH}/

       chown -R mysql.mysql /data

       done

       for ((j=1;j<=i;j++))

       do

       cp -r ${DATA_PATH}/var ${DATA_PATH}/var${j}

       chown -R mysql.mysql ${DATA_PATH}/var${j}


       PORT=`echo $@|awk -v num="$j" '{print $num}'`

       CONFIG_MULTI=`cat <<EOF


       [mysqld${j}]

       skip-name-resolve

       skip-innodb

       socket     = /tmp/mysql.sock${j}

       port       = ${PORT}

       pid-file   = ${DATA_PATH}/var${j}/mysqld.pid${j}

       datadir    = ${DATA_PATH}/var${j}


       log-slow-queries=${DATA_PATH}/var${j}/slowquery.log

       long_query_time=5

       log-queries-not-using-indexes


       #server-id=

       #log-bin=mysql-bin

       #binlog_format=mixed

       #expire_logs_days=

       #replicate|binlog-do-db=

   #replicate|binlog-ignore-db=mysql

       #master-connect-retry=60

       #log-slave-updates

       #slave-skip-errors=all


       skip-external-locking

       key_buffer_size = 384M

       max_allowed_packet = 10M

       table_open_cache = 512

       sort_buffer_size = 2M

       read_buffer_size = 2M

       read_rnd_buffer_size = 8M

       myisam_sort_buffer_size = 64M

       thread_cache_size = 8

       query_cache_size = 32M

       thread_concurrency = 8

       concurrent_insert = 2

       `

       echo "${CONFIG_MULTI}"|while read LINE;do

       echo "${LINE}" >>${MYSQL_PATH}/share/mysql/my-multi.cnf

       done

       ${MYSQL_PATH}/bin/mysqld_multi --defaults-extra-file=${MYSQL_PATH}/share/mysql/my-multi.cnf start ${j}

               if [ `cat /etc/rc.local|grep -c "start ${j}"`  == 0 ];then

                       echo "${MYSQL_PATH}/bin/mysqld_multi --defaults-extra-file=${MYSQL_PATH}/share/mysql/my-multi.cnf start ${j}" >>/etc/rc.local

               else

                       echo "This Startup Item Exists!"

               fi

       done

fi


echo "-=-=-=-=-=-=-=-=-=-= Mysql Install Complete! =-=-=-=-=-=-=-=-=-=-"

#clear

rm -rf ${DATA_PATH}/var

rm -rf /home/${MYSQL_VERSION}

rm -rf /home/mysql-5.1.55

rm -rf $0