MySQL自动安装脚本
以下是小弟在测试环境写的一个MySQL自动安装脚本
#!/bin/bash
#############################################
# 1.create by zyshun #
# 2.2015-11-12 #
# 3.install mysql script #
#############################################
source /root/.bash_profile
echo -n "Please input you new MySQL instance port:"
read port
echo -n "Please input you new MySQL instance version(example:5.6.16):"
read mysqlversion
primarydir=/data01
primaryname=data01
user=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}
fi
chmod 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_init
if [ $? -eq 0 ];then
echo "The func environment_init was executed successful!" >> ${install_log}
else
echo "The func environment_init was executed unsuccessful!" >> ${install_log}
exit
fi
create_user_group
if [ $? -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}
exit
fi
create_directory
if [ $? -eq 0 ];then
echo "The func create_directory was executed successful!" >> ${install_log}
else
echo "The func create_directory was executed unsuccessful!" >> ${install_log}
exit
fi
alter_os_file
if [ $? -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}
exit
fi
tar_source_mysql
if [ $? -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}
exit
fi
compile_mysql
if [ $? -eq 0 ];then
echo "The func compile_mysql was executed successful!" >> ${install_log}
else
echo "The func compile_mysql was executed unsuccessful!" >> ${install_log}
fi
config_my_cnf
if [ $? -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}
exit
fi
init_mysql_instance
if [ $? -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}
exit
fi
start_mysql_instance
if [ $? -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}
exit
fi
部署自启动脚本
#!/bin/bash
#created by zyshun at 2015-11-02
#Used for mysql start,stop,restart
################################################
#chkconfig: 2345 10 90
primarydir=/data01
mysqlversion=5.6.27
port=3306
mysql_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 function
function_stop_mysql()
{
printf "Stoping MySQL...\n"
${cmdpath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${primarydir}/my_${port}/run/mysqld.socket shutdown
printf "Stoped MySQL...\n"
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 5
function_start_mysql
printf "Restarted MySQL...\n"
}
case $1 in
start)
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