MySQL技术之旅--MySQL自动安装脚本

来源:互联网 发布:网络协议怎么那么多 编辑:程序博客网 时间:2024/05/18 12:02

MySQL自动安装脚本

以下是小弟在测试环境写的一个MySQL自动安装脚本

#!/bin/bash#############################################        # 1.create by zyshun                      #  # 2.2015-11-12                            #            # 3.install mysql script                  #                                                            #############################################                                source /root/.bash_profileecho -n "Please input you new MySQL instance port:"read portecho -n "Please input you new MySQL instance version(example:5.6.16):"read mysqlversionprimarydir=/data01primaryname=data01user=mysql  group=myinstall begintime=`date "+%G-%m-%d %H:%M:%S"`install_log=/tmp/install_${mysqlversion}_${port}.log#my.cnf calculate variable cpu_core=`cat /proc/cpuinfo| grep "processor" | wc -l`memory=`free -g | egrep -v "total" | awk {'print $2'} | head -1`buff=`echo "scale=0;${memory}*0.5"|bc`innobuff=`echo ${buff} | awk -F'.' {'print $1'}`serverid1=`ifconfig -a | egrep "inet addr" | head -1 | awk {'print $2'} | awk  -F ':' {'print $2'} | awk -F'.' {'print $3$4'}`serverid=${serverid1}${port}function environment_init() {    rpm -qa|grep MySQL|xargs rpm --allmatches --nodeps -e    rpm -qa|grep mysql|xargs rpm --allmatches --nodeps -e    echo "remove linux OS's MySQL is ok" >> ${install_log}    yum search lrzsz    if [ $? -eq 0 ];then        echo "Linux yum is ok !">> ${install_log}    else        echo "Linux yum  is not ok!">> ${install_log}        exit    fi      yum install -y make    if [ $? -eq 0 ];then        echo "install package make is ok!">> ${install_log}    else        echo "install package make is not ok!">> ${install_log}        exit    fi          yum install -y bison    if [ $? -eq 0 ];then        echo "install package bison is ok!">> ${install_log}    else        echo "install package bison is not ok!">> ${install_log}        exit    fi      yum install -y gcc-c++    if [ $? -eq 0 ];then        echo "install package gcc-c++ is ok!">> ${install_log}    else        echo "install package gcc-c++ is not ok!">> ${install_log}        exit    fi      yum install -y gcc    if [ $? -eq 0 ];then        echo "install package gcc is ok!">> ${install_log}    else        echo "install package gcc is not ok!">> ${install_log}        exit    fi      yum install -y cmake    if [ $? -eq 0 ];then        echo "install package cmake is ok!">> ${install_log}    else        echo "install package cmake is not ok!">> ${install_log}        exit    fi      yum install -y ncurses-devel    if [ $? -eq 0 ];then        echo "install package ncurses-devel is ok!">> ${install_log}    else        echo "install package ncurses-devel is not ok!">> ${install_log}        exit    fi  }function create_user_group() {    egrep "^$group" /etc/group >& /dev/null      if [ $? -ne 0 ];then        groupadd -g 601 ${group}        echo "The $user group $group already be created !" >> ${install_log}    else        echo "The $user user_group $group already exists !" >> ${install_log}    fi    egrep "^$user" /etc/passwd >& /dev/null     if [ $? -ne 0 ];then        useradd -c "Mysql software owner" -g myinstall -G myinstall  -u 600 mysql    echo "The $user user already be created !" >> ${install_log}else    userdel -r mysql     useradd -c "Mysql software owner" -g myinstall -G myinstall  -u 600 mysql    echo "The $user user already be created !" >> ${install_log}fichmod 755 /home/${user}}function create_directory() {    mkdir -p ${primarydir}/my_${port}/{data,log,run,tmp,binlog,iblog}    chown -R ${user}.${group}  ${primarydir}/my_${port}    echo "The create_directory is ok"  >> ${install_log}}function alter_os_file() {    echo "*  soft    nproc   16384" >> /etc/security/limits.conf    echo "*  hard    nproc   16384" >> /etc/security/limits.conf    echo "*  soft    nofile  65536" >> /etc/security/limits.conf    echo "*  hard    nofile  65536" >> /etc/security/limits.conf    echo "edit /etc/security/limits.conf complete!!!"    touch /etc/profile.d/login.sh    echo -e "#added by myinstall\nif [ $USER = "mysql" ]; then\n    if [ $SHELL = "/bin/ksh" ]; then\nulimit -p 16384\nulimit -n 65536\n    else  ulimit -u 16384 -n 65536\n fi\n fi" >> /etc/profile.d/login.sh      echo "The alter os file  is ok"  >> ${install_log}}function tar_source_mysql() {    cp /tmp/autoinstallmysql/mysql-${mysqlversion}.tar.gz ${primarydir}/    cd ${primarydir}    tar -zxvf mysql-${mysqlversion}.tar.gz     chmod a+x  ${primarydir}/mysql-${mysqlversion}/scripts/*    chmod a+x  ${primarydir}/mysql-${mysqlversion}/bin/*    echo "The tar source mysql file  is ok"  >> ${install_log}    }function compile_mysql() {    cd ${primarydir}/mysql-${mysqlversion}    CFLAGS="-O3"     CXX=gcc    CXXFLAGS="-O0 -g -fno-exceptions -fno-rtti -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"    cmake . \    -DCMAKE_INSTALL_PREFIX=${primarydir}/mysql-${mysqlversion} \    -DDEFAULT_CHARSET=utf8 \    -DDEFAULT_COLLATION=utf8_bin \    -DEXTRA_CHARSETS=all\    -DWITH_INNOBASE_STORAGE_ENGINE=1 \    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \    -DWITH_MYISAM_STORAGE_ENGINE=1 \    -DENABLED_LOCAL_INFILE=1 \    -DMYSQL_TCP_PORT=${port} \    -DMYSQL_UNIX_ADDR=${primarydir}/my_${port}/run/mysqld.sock      make -j ${cpu_core}     make install     echo "The compile_mysql file  is ok"  >> ${install_log}}function config_my_cnf() {    cp /tmp/autoinstallmysql/my.cnf  ${primarydir}/my_${port}/my${port}.cnf    sed -i "s/3306/${port}/g"  ${primarydir}/my_${port}/my${port}.cnf    sed -i "s/innodbbufferpoolmycnf/${innobuff}G/g" ${primarydir}/my_${port}/my${port}.cnf    sed -i "s/serveridmycnf/${serverid}/g" ${primarydir}/my_${port}/my${port}.cnf    sed -i "s/data01/${primaryname}/g" ${primarydir}/my_${port}/my${port}.cnf    sed -i "s/5.6.16/${mysqlversion}/g" ${primarydir}/my_${port}/my${port}.cnf    echo "The mysql conf  file  is ok"  >> ${install_log}}function init_mysql_instance() {    ${primarydir}/mysql-${mysqlversion}/scripts/mysql_install_db  --user=mysql --basedir=${primarydir}/mysql-${mysqlversion}  --datadir=${primarydir}/my_${port}/data --defaults-file=${primarydir}/my_${port}/my${port}.cnf}function start_mysql_instance() {       ${primarydir}/mysql-${mysqlversion}/bin/mysqld_safe --defaults-file=${primarydir}/my_${port}/my${port}.cnf --user=mysql &}environment_initif [ $? -eq 0 ];then    echo "The func environment_init was executed successful!"  >> ${install_log}else    echo "The func environment_init was executed unsuccessful!"  >> ${install_log}    exitficreate_user_groupif [ $? -eq 0 ];then    echo "The func create_user_group was executed successful!"  >> ${install_log}else    echo "The func create_user_group was executed unsuccessful!"  >> ${install_log}    exitficreate_directoryif [ $? -eq 0 ];then    echo "The func create_directory  was executed successful!"  >> ${install_log}else    echo  "The func create_directory  was executed unsuccessful!"  >> ${install_log}    exitfialter_os_fileif [ $? -eq 0 ];then    echo "The func alter_os_file  was executed successful!"  >> ${install_log}else    echo  "The func alter_os_file  was executed unsuccessful!"  >> ${install_log}    exitfitar_source_mysqlif [ $? -eq 0 ];then    echo "The func tar_source_mysql  was executed successful!"  >> ${install_log}else    echo "The func tar_source_mysql  was executed unsuccessful!"  >> ${install_log}    exitficompile_mysqlif [ $? -eq 0 ];then    echo "The func compile_mysql  was executed successful!"  >> ${install_log}else    echo "The func compile_mysql  was executed unsuccessful!"  >> ${install_log}ficonfig_my_cnfif [ $? -eq 0 ];then    echo "The func config_my_cnf  was executed successful!"  >> ${install_log}else    echo "The func config_my_cnf  was executed unsuccessful!"  >> ${install_log}    exitfiinit_mysql_instanceif [ $? -eq 0 ];then    echo "The func init_mysql_instance  was executed successful!"  >> ${install_log}else    echo "The func init_mysql_instance  was executed unsuccessful!"  >> ${install_log}    exitfistart_mysql_instanceif [ $? -eq 0 ];then    echo "The func start_mysql_instance  was executed successful!"  >> ${install_log}else    echo "The func start_mysql_instance  was executed unsuccessful!"  >> ${install_log}    exitfi

部署自启动脚本

#!/bin/bash#created by zyshun at 2015-11-02#Used for mysql start,stop,restart#################################################chkconfig: 2345 10 90 primarydir=/data01mysqlversion=5.6.27port=3306mysql_user="root"mysql_pwd="1qaz2wsx"cmdpath="/data01/mysql-${mysqlversion}/bin"function_start_mysql(){printf "Starting MySQL...\n"${cmdpath}/mysqld_safe --defaults-file=${primarydir}/my_${port}/my${port}.cnf --user=mysql 2>&1 > /dev/null &printf "Started MySQL...\n"}#stop functionfunction_stop_mysql(){printf "Stoping MySQL...\n"${cmdpath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${primarydir}/my_${port}/run/mysqld.socket shutdownprintf "Stoped MySQL...\n"}#restart functionfunction_restart_mysql(){printf "Restarting MySQL...\n"function_stop_mysqlsleep 5function_start_mysqlprintf "Restarted  MySQL...\n"}case $1 instart)function_start_mysql;;stop)function_stop_mysql;;restart)function_restart_mysql;;*)printf "Usage: ${primarydir}/my_${port}/bin/mysqld  {start|stop|restart}\n"esac

将脚本复制到/etc/rc.d/init.d

添加到chkconfig 中
chkconfig --add mysql3306

0 0
原创粉丝点击