mysql批量安装_mysql 批量安装脚本

#!/bin/bash

################################################

#

# mysql安装脚本V2.0-支持远程安装

#

################################################

#variables

md5num='6ad260ef2a31bcfd712b0c6cf615c8a2'

DEST='/usr/local/mysql'

DATA='/data/mysql'

SRC='/usr/local/src'

MS='master'

PASS=''

err=0

DEST_IP=''

#==============================================

#    mysql install check func

# flag=0 it is OK

# flag=1 is is some problem

#==============================================

function mysql_ins_check()

{

echo "mysql install envionment variables check:"

echo "*************************************"

echo 'checking group \"mysql\" exist...'

echo "*************************************"

mysql_grp_exist=$(ssh $DEST_IP "cat /etc/group|grep \"mysql\"")

if [ -z $mysql_grp_exist ];then #如果为空则代表没有生成mysql用户组

flag_grp_exist=1

echo "mysql_grp_exist="$mysql_grp_exist

echo 'it is right !!!mysql group not exist'

echo

else

flag_grp_exist=0

echo -e "\E[33;40m"

echo 'mysql group is already exist'

tput sgr0

echo

err=2

fi

echo "*************************************"

echo 'checking user \"mysql\" exist...'

echo "*************************************"

user_exist=$(ssh $DEST_IP "cat /etc/passwd|grep mysql")

if [ -z $user_exist ] ;then

echo "user \"mysql\" is not exist"

echo

flag_user=0

else

echo -e "\E[33;40m"

echo "user \" mysql\" is already exist"

tput sgr0

echo

flag_user=1

err=2

echo "*************************************"

echo 'checking user group is \"mysql\" ...'

echo "*************************************"

mysql_group=$(echo $user_exist | awk -F: '{print $4}')

echo "in /etc/passwd,group NO for username \"mysql\" = "$mysql_group

mysql_group_no=$(ssh $DEST_IP "cat /etc/group|grep \"mysql\"|awk -F: '{print \$3}'")

echo "in /etc/group, group NO for group \"mysql\" = "$mysql_group_no

if [ $mysql_group = $mysql_group_no ]; then

echo 'It is right'

echo

flag_group=0

else

echo -e "\E[31;40m"

echo 'It is not right'

tput sgr0

echo

flag_group=1

err=1

fi

echo "*************************************"

echo 'checking user login set ...'

echo "*************************************"

mysql_login=$(echo $user_exist | awk -F: '{print $7}')

echo "user_login = "$mysql_login

if [ $mysql_login != '/sbin/nologin' ];then

echo -e "\E[31;40m"

echo "It is not right, execing usermod -s /sbin/nologin mysql"

tput sgr0

echo

flag_login_shell=1

ssh $DEST_IP "usermod -s /sbin/nologin mysql"

else

echo "It is right"

echo

flag_login_shell=0

fi

fi

echo "*************************************"

echo 'checking mysqld service ...'

echo "*************************************"

if ssh $DEST_IP "[ -x '/etc/init.d/mysqld' ]" ; then

echo -e "\E[31;40m"

echo "mysqld service is already stay here and can be exec,please checking"

tput sgr0

echo

flag_mysqld=1

err=1

else

if ssh $DEST_IP "[ -f '/etc/init.d/mysqld' ]" ; then

echo -e "\E[31;40m"

echo "mysqld service is already stay here but can not exec!!!"

tput sgr0

flag_mysqld=1

err=1

else

echo "it is right!!!mysqld service is not install..."

flag_mysqld=0

fi

fi

echo

echo "*************************************"

echo 'checking mysql directory ...'

echo "*************************************"

if ssh $DEST_IP "test -d $DEST " ;then

echo -e "\E[31;40m"

echo "mysql directory is already install in $DEST_IP:$DEST"

tput sgr0

echo

flag_dir=1

err=1

else

echo "it is right!!!mysql is not install in $DEST_IP:$DEST"

echo

flag_dir=0

fi

echo "*************************************"

echo 'checking mysql data directory ...'

echo "*************************************"

if ssh $DEST_IP "test -d $DATA ";then

echo -e "\E[31;40m"

echo "mysql data directory is already exist in $DEST_IP:$DATA,please checking"

tput sgr0

echo

flag_data_dir=1

err=1

else

echo "It is right! $DEST_IP:$DATA not exist"

echo

flag_data_dir=0

fi

echo "************************************"

echo "checking remote /etc/my.cnf already exist?"

echo "************************************"

if ssh $DEST_IP " test -f '/etc/my.cnf' " ;then

echo -e "\E[31;40m"

echo "/etc/my.cnf already exist,please check..."

tput sgr0

echo

err=3

else

echo "it is right!!!"

echo

fi

}

##################################

#

# mysql_ins_file_check( )

#

##################################

function mysql_ins_file_check()

{

clear

echo "*****************************"

echo 'checking src file...'

echo "*****************************"

get_md5=$(md5sum $SRC'/mysql-5.1.34-linux-i686-glibc23.tar.gz'|awk '{print $1 }')

echo "md5num in cfg file="$md5num

echo "md5num shell get in file="$get_md5

if [ -f $SRC'/mysql-5.1.34-linux-i686-glibc23.tar.gz' ];then

echo 'mysql-5.1.34-linux-i686-glibc23.tar.gz is exist !!! OK.'

if [ $md5num = $get_md5 ]; then

echo 'it is right!!!file md5num ok'

echo

else

echo -e "\E[31;40m"

echo 'file md5num check is failed!!!'

tput sgr0

echo

err=1

fi

else

echo -e "\E[31;40m"

echo 'mysql-5.1.34-linux-i686-glibc23.tar.gz is not exist in $SRC!!! please check.'

tput sgr0

echo

err=1

fi

}

##############################

#

# create_my_cnf( )

#

##############################

function create_my_cnf

{

echo 'please waiting for create /etc/my.cnf...'

ssh $DEST_IP "cat > /etc/my.cnf

port        = 3306

socket        = /tmp/mysql.sock

[mysqld]

port        = 3306

socket        = /tmp/mysql.sock

skip-locking

key_buffer_size = 256M

max_allowed_packet = 4M

table_cache = 1024

#skip-innodb

sort_buffer_size = 8M

net_buffer_length = 16384

read_buffer_size = 16M

read_rnd_buffer_size = 32M

myisam_sort_buffer_size = 128M

slow_query_log_file=slowquery.log

long_query_time=3

#log=query_log

thread_cache_size = 16

query_cache_size = 32M

skip-name-resolve

tmp_table_size = 256M

thread_concurrency = 32

max_connections=64

datadir=$DATA

#default-storage-engine=INNODB

log-bin-trust-function-creators=1

log-bin=mysql-bin

binlog-format=mix

###innodb

innodb_buffer_pool_size = 800M

#innodb_buffer_pool_size = 2000M

innodb_additional_mem_pool_size = 24M

#innodb_additional_mem_pool_size = 128M

innodb_thread_concurrency=16

innodb_max_dirty_pages_pct = 90

innodb_file_io_threads = 4

innodb_log_file_size = 512M

innodb_log_buffer_size = 16M

innodb_flush_log_at_trx_commit = 2

innodb_lock_wait_timeout = 50

innodb_file_per_table

EOF"

if [ $MS = 'master' ];then

ssh $DEST_IP "cat >> /etc/my.cnf

EOF"

fi

if [ $MS = 'slave' ];then

ssh $DEST_IP "cat >> /etc/my.cnf

EOF"

fi

ssh $DEST_IP "cat >> /etc/my.cnf

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[isamchk]

key_buffer = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

[myisamchk]

key_buffer = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

EOF"

}

function mysql_ins_result()

{

case "$err" in

1) echo "something wrong happenning ,please checking...";

exit 1;;

2) echo "mysql user or group is already exist,skip creating user or group ";;

3) echo "my.cnf it is already exist,please check";

exit 1;;

0) echo "all OK!!!\n Start install...";

echo "soure path = $SRC \ndest path = $DEST \nDATA path=$DATA \npassword=$PASS\nMaster or Slave=$MS ";;

*) exit 1;;

esac

}

function grp_ins()

{

echo \#shell start

if [ $flag_grp_exist = 1 ];then

echo 'add group mysql'

ssh $DEST_IP "groupadd mysql"

fi

}

function usr_ins()

{

if [ $flag_user = 1 ];then

echo 'add user mysql in group mysql with nologin!!!'

useradd -g mysql mysql -s /sbin/nologin

fi

}

##############################

#

# 判断远端文件是否存在

# 并且判断文件MD5是否正确

#

##############################

function remote_file_check()

{

if ssh $DEST_IP "test -f $SRC'/mysql-5.1.34-linux-i686-glibc23.tar.gz'" ; then

echo 'source is already in remote src'

get_r_md5=$( ssh $DEST_IP "md5sum $SRC'/mysql-5.1.34-linux-i686-glibc23.tar.gz'|awk '{print \$1 }'")

if [ $md5num = $get_r_md5 ]; then

echo 'file md5num ok'

echo

else

echo 'file md5num check is failed!!!so system will restart copy resource to remote...'

echo

scp $SRC'/mysql-5.1.34-linux-i686-glibc23.tar.gz' $DEST_IP':'$SRC'/'

fi

else

echo "mysql-5.1.34-linux-i686-glibc23.tar.gz is not exist in $DEST_IP:$SRC!!! copy it to remote!! starting copy..."

echo

scp $SRC'/mysql-5.1.34-linux-i686-glibc23.tar.gz' $DEST_IP':'$SRC'/'

fi

}

##############################

#

# 解压缩远端文件

#

#############################

function xgz()

{

echo 'extract src...'

ssh $DEST_IP "tar zxf $SRC'/mysql-5.1.34-linux-i686-glibc23.tar.gz' -C /root"

}

###################################

#

# 将目录移动至目标目录

#

###################################

function mv_to_dest()

{

echo "mv -f /root/mysql-5.1.34-linux-i686-glibc23 $DEST"

ssh $DEST_IP "mv -f /root/mysql-5.1.34-linux-i686-glibc23 $DEST"

#################################

#

# 修改目录所有者所属组mysql:mysql

#

################################

echo    "chown -R mysql:mysql $DEST"

ssh $DEST_IP "chown -R mysql:mysql $DEST"

}

###################################

#

# 建立目标数据目录

#

##################################

function data_manage()

{

echo    "mkdir -p $DATA"

ssh $DEST_IP "mkdir -p $DATA"

###################################

#

# 改变数据目录的用户和组mysql:mysql

#

##################################

echo    "chown -R mysql:mysql $DATA"

ssh $DEST_IP "chown -R mysql:mysql $DATA"

}

#########################################

#

# 安装数据库初始化数据至指定数据目录

#

#########################################

function ins_db()

{

echo 'install DB data'

echo    "/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=$DEST --datadir=$DATA"

ssh $DEST_IP "/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=$DEST --datadir=$DATA"

}

########################################

#

# 制作mysql服务

#

#######################################

function create_service()

{

echo 'make mysql as service'

echo    'cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld'

ssh $DEST_IP "cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld"

echo

echo    'chkconfig mysqld on'

ssh $DEST_IP "chkconfig mysqld on"

echo    'service mysqld start'

ssh $DEST_IP "service mysqld start"

}

#####################################

#

# 生成root密码 默认为空

#

#####################################

function reset_pass()

{

echo    "/usr/local/mysql/bin/mysqladmin -u root password '$PASS'"

ssh $DEST_IP "/usr/local/mysql/bin/mysqladmin -u root password '$PASS'"

}

#######################################

#

#    Main Part

#

#######################################

USAGE="usage:./my_ins.sh -s source_path -d dest_path -D data_path -S [master|slave] -p password -P dest_ip

\n-s soure_path [ default /usr/local/src]

\n-d dest_path [default /usr/local/mysql]

\n-D data_path [default /data/mysql]

\n-S master|slave [default master]

\n-p password [default '']

\n\E[31;40m-P dest_ip or hostname Must input!!!\E[37;40m"

#如果没参数的话,则显示帮助

if [ -z $1 ] ; then

echo -e $USAGE

tput sgr0

exit 1

fi

#进行参数判断

while getopts s:d:p:S:D:P:h OPTION; do

case "$OPTION" in

s) SRC="$OPTARG";

echo 'src='$SRC;;

d) DEST="$OPTARG";

echo 'dest='$DEST;;

p) PASS="$OPTARG";

echo 'pass='$PASS;;

S) MS="$OPTARG";

echo 'master or slave='$MS;;

D) DATA="$OPTARG";

echo 'DATA path='$DATA;;

P) DEST_IP="$OPTARG";

echo 'DEST_IP or DEST_HOSTNAME='$DEST_IP;;

h) echo -e "$USAGE";

tput sgr0;

exit 1 ;;

*)echo -e "$USAGE";

tput sgr0;

exit 1;;

esac

done

if [ -z $DEST_IP ]; then

echo -e "\E[31;40m-P dest_ip or hostname Must input!!!\E[37;40m"

tput sgr0

exit 1

fi

mysql_ins_file_check

mysql_ins_check

mysql_ins_result

create_my_cnf

grp_ins

remote_file_check

xgz

mv_to_dest

data_manage

ins_db

create_service

reset_pass

##########################################

#

# 配置mysql环境变量,path etc

#

##########################################

my_path=$(ssh $DEST_IP "cat /etc/profile |grep 'PATH'|grep \"$DEST/bin\"")

if [ "$my_path" = '' ]

then

ssh $DEST_IP "echo 'export PATH=$DEST/bin:\$PATH' >> /etc/profile'"

echo ssh $DEST_IP "echo 'export PATH=\$DEST/bin:\$PATH' >> /etc/profile"

else

echo "it is already exist"

fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值