mysql5.6.35的安装脚本
#!/bin/bash
function help() ( cat << EOF $0 [-h] $0 -c <CharaterSet> EOF exit 1 ) ------------------------------------------- # 判断用户是否存在,不存在就新增mysql用户 check_and_add_user() { # create a user if the specified user doesn't exists local userCnt userCnt=$(cat /etc/passwd|grep ${MYSQL_USER}|grep -v grep|wc -l) if [ ${userCnt} -eq 0 ];then /usr/sbin/groupadd ${MYSQL_GROUP} /usr/sbin/useradd ${MYSQL_USER} -g ${MYSQL_GROUP} echo "user ${MYSQL_USER} added" else echo "user ${MYSQL_USER} already exists, skipping this step !" fi userCnt=$(cat /etc/passwd|grep ${AGENT_USER}|grep -v grep|wc -l) if [ ${userCnt} -eq 0 ];then /usr/sbin/groupadd ${AGENT_GROUP} /usr/sbin/useradd -s /bin/false -g ${AGENT_GROUP} ${AGENT_USER} echo "user ${AGENT_USER} added" else echo "user ${AGENT_USER} already exists, skipping this step !" fi } 运行结果如下: check rpm done at Tue Jan 15 19:53:28 CST 2019 ...... check user ......starting at Tue Jan 15 19:53:28 CST 2019 user mysql added user agent added check user done at Tue Jan 15 19:53:29 CST 2019 ---------------------------------------------------- #检查目录是否新建,若无则建立目录:/data/mysql_data,/data/mysql_data/binlog,/data/mysql_data/tmpdir check_directory() { # get DATA directory get_data_dir if [ -d ${DATA_DIR} ];then echo "directory ${DATA_DIR} already exists, please check and remove this directory and rerun this script " echo "exiting " echo "mysql installation failed !" exit fi mkdir -p ${INSTALL_DIR} mkdir -p ${DATA_DIR}/binlog mkdir -p ${DATA_DIR}/tmpdir } 运行结果如下: ...... check directory ......starting at Tue Jan 15 19:53:29 CST 2019 check directory done at Tue Jan 15 19:53:29 CST 2019 ...... install mysql ......starting at Tue Jan 15 19:53:29 CST 2019 ------------------------------------------ get_data_dir() { for DF in $(df -h|awk '{print $6}'|grep -e "^/");do if [ ${DF} == "${DIR_KEY}" ];then DATA_DIR=${DF}/$(basename ${DATA_DIR}) TOTAL_SIZE=$(df -k|awk '$6== "'${DIR_KEY}'" {print $2}') # DIR_KEY="/data1" 这个变量先已经定义了 break else TOTAL_SIZE=$(df -k|awk '$6== "/data" {print $2}') fi done } ---------------------------------------------------------------------------------------------------- # 安装基础依赖包:gcc gcc-c++ make cmake autoconf automake ncurses-devel bison perl-Number-Format perl-Statistics-Descriptive perl-TermReadKey perl-DBD-MySQL perl-Statistics-Basic perl-Sys-Statistics-Linux perl perl-devel libaio libaio-devel perl-Time-HiRes numactl.x86_64 libev.x86_64 perl-IO-Socket-SSL bc check_rpm() { rmpList=(gcc gcc-c++ make cmake autoconf automake ncurses-devel bison perl-Number-Format perl-Statistics-Descriptive perl-TermReadKey perl-DBD-MySQL perl-Statistics-Basic perl-Sys-Statistics-Linux perl perl-devel libaio libaio-devel perl-Time-HiRes numactl.x86_64 libev.x86_64 perl-IO-Socket-SSL bc) for thisRpm in ${rmpList[*]};do installFlag=$(rpm -q ${thisRpm}|grep "not installed"|wc -l) if [ ${installFlag} -ne 0 ];then echo "${thisRpm} not installed" yum install -y ${thisRpm} fi done } 安装结果如下: ...... check rpm ......starting at Tue Jan 15 19:50:32 CST 2019 cmake not installed Installed: cmake.x86_64 0:2.8.12.2-4.el6 Dependency Installed: libarchive.x86_64 0:2.8.3-7.el6_8 perl-Number-Format not installed Installed: perl-Number-Format.noarch 0:1.73-3.el6 perl-Statistics-Descriptive not installed Installed: perl-Statistics-Descriptive.noarch 0:3.0200-1.el6 perl-TermReadKey not installed Installed: perl-TermReadKey.x86_64 0:2.30-13.el6 perl-DBD-MySQL not installed Installed: perl-DBD-MySQL.x86_64 0:4.013-3.el6 Dependency Installed: perl-DBI.x86_64 0:1.609-4.el6 perl-Statistics-Basic not installed Installed: perl-Statistics-Basic.noarch 0:1.6611-3.el6 perl-Sys-Statistics-Linux not installed Installed: perl-Sys-Statistics-Linux.noarch 0:0.59-3.el6 Dependency Installed: perl-Time-HiRes.x86_64 4:1.9721-144.el6 Dependency Updated: perl.x86_64 4:5.10.1-144.el6 perl-Compress-Raw-Zlib.x86_64 1:2.021-144.el6 perl-Compress-Zlib.x86_64 0:2.021-144.el6 perl-IO-Compress-Base.x86_64 0:2.021-144.el6 perl-IO-Compress-Zlib.x86_64 0:2.021-144.el6 perl-Module-Pluggable.x86_64 1:3.90-144.el6 perl-Pod-Escapes.x86_64 1:1.04-144.el6 perl-Pod-Simple.x86_64 1:3.13-144.el6 perl-libs.x86_64 4:5.10.1-144.el6 perl-version.x86_64 3:0.77-144.el6 perl-devel not installed Installed: perl-devel.x86_64 4:5.10.1-144.el6 Dependency Installed: perl-ExtUtils-MakeMaker.x86_64 0:6.55-144.el6 perl-ExtUtils-ParseXS.x86_64 1:2.2003.0-144.el6 perl-Test-Harness.x86_64 0:3.17-144.el6 libaio-devel not installed Installed: libaio-devel.x86_64 0:0.3.107-10.el6 libev.x86_64 not installed Installed: libev.x86_64 0:4.03-3.el6 perl-IO-Socket-SSL not installed Installed: perl-IO-Socket-SSL.noarch 0:1.31-3.el6_8.2 Dependency Installed: perl-Net-LibIDN.x86_64 0:0.12-3.el6 perl-Net-SSLeay.x86_64 0:1.35-10.el6_8.1 --------------------------------------------------------------------------------------------------------- # 安装当前目录下的percona的RPM包:percona-release-0.1-4.noarch percona-toolkit-2.2.20-1.noarch percona-xtrabackup-24-2.4.5-1.el6.x86_64 install_percona() { ptRpmList=(percona-release-0.1-4.noarch percona-toolkit-2.2.20-1.noarch percona-xtrabackup-24-2.4.5-1.el6.x86_64) for ptRpm in ${ptRmpList[*]};do installFlag=$(rpm -q ${ptRpm}|grep "not installed"|wc -l) if [ ${installFlag} -ne 0 ];then echo "${thisRpm} not installed" rpm -ivh ${ptRpm}.rpm fi done } ----------------------------------------------------------------------------------------------------------- # 正式安装MySQL install_mysql() { cd ${PWDDIR} collation=`awk '$1 ~ /^'${charset}'$/ {print $2}' ${CHARSET_COLLATION_FILE}` rm -rf "mysql-${MYSQL_VERSION}" tar zxvf ${MYSQL_FILE} cd "mysql-${MYSQL_VERSION}" cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \ -DMYSQL_DATADIR=${DATA_DIR} \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DENABLED_LOCAL_INFILE=1 \ -DDEFAULT_CHARSET=${charset} \ -DDEFAULT_COLLATION=${collation} \ -DWITH_DEBUG=0 \ -DENABLE_DOWNLOADS=1 \ -DMYSQL_UNIX_ADDR=${SOCK_FILE} make -j 4 && make install # init mysql chmod +x scripts/mysql_install_db scripts/mysql_install_db --basedir=${INSTALL_DIR} --datadir=${DATA_DIR} --user=${MYSQL_USER} # change ownership /bin/chown -R ${MYSQL_USER}:${MYSQL_GROUP} ${INSTALL_DIR} /bin/chown -R ${MYSQL_USER}:${MYSQL_GROUP} ${DATA_DIR} } ------------------------------------------------- get_mysql_parameters() { > ${MYSQL_PARA_FILE} # set max_conntions and innodb buffer size MEM_SIZE=$(free -g|grep Mem|awk '{print $2}') SSD_FLAG=$(MegaCli -PDList -aAll|grep "SSD"|wc -l) INNODB_BUFFER=$(echo ${MEM_SIZE}*0.75|bc) INNODB_BUFFER=${INNODB_BUFFER%.*} if [ ${SSD_FLAG} -gt 0 ];then # for SSD INNODB_IO_CAPACITY=2000 INNODB_IO_CAPACITY_MAX=6000 else # for SAS INNODB_IO_CAPACITY=500 INNODB_IO_CAPACITY_MAX=6000 fi echo "<MAX_CONNECTION>=${MAX_CONNECTION}" >> ${MYSQL_PARA_FILE} echo "<INNODB_BUFFER>=${INNODB_BUFFER}" >> ${MYSQL_PARA_FILE} echo "<INNODB_IO_CAPACITY>=${INNODB_IO_CAPACITY}" >> ${MYSQL_PARA_FILE} echo "<INNODB_IO_CAPACITY_MAX>=${INNODB_IO_CAPACITY_MAX}" >> ${MYSQL_PARA_FILE} # set server id SERVER_ID=$(/sbin/ifconfig|grep inet|head -1|awk '{print $2}'|awk -F: '{print $2}'|awk -F. '{print $3$4}') echo "<SERVER_ID>=${SERVER_ID}" >> ${MYSQL_PARA_FILE} #set data directory echo "<DATA_DIR>=${DATA_DIR}" >> ${MYSQL_PARA_FILE} } replace_parameters() { cp ${TEMPLATE_CONF_FILE} ${TMP_CONF_FILE} while read LINE;do KEY=$(echo ${LINE}|awk -F= '{print $1}'|sed 's/\//\\\//g') VALUE=$(echo ${LINE}|awk -F= '{print $2}'|sed 's/\//\\\//g') sed -i 's/'${KEY}'/'${VALUE}'/g' ${TMP_CONF_FILE} done < ${MYSQL_PARA_FILE} # set innodb_flush_method MEM_SIZE=$(cat /proc/meminfo |grep MemTotal|awk '{print $2}') if [ ${MEM_SIZE} -lt 30000000 ];then sed -i 's/innodb_flush_method/#innodb_flush_method/g' ${TMP_CONF_FILE} fi if [ ${charset} != "utf8" ] then sed -i 's/init_connect/#init_connect/g' ${TMP_CONF_FILE} fi } set_mysql_parameters() { cd ${PWDDIR} get_data_dir get_mysql_parameters replace_parameters cp ${TMP_CONF_FILE} ${INSTALL_DIR} } -------------------------------------------------------- # 上传mysqld的启动文件到/etc/init.d/目录下面并启动数据库 do_post_tasks() { # configuration file rm -f /etc/my.cnf cd /etc ln -s ${INSTALL_DIR}/my.cnf cd /usr/local ln -s ${INSTALL_DIR} zftmysql # /usr/local/mysql/bin/mysqld_safe & cd ${PWDDIR} cp mysqld /etc/init.d chmod +x /etc/init.d/mysqld /etc/init.d/mysqld start } ---------------------------------------------------------------- 安装db_agent, install_db_agent() { local userCnt /usr/bin/killall -9 db_agent cd ${PWDDIR} tar zxvf db_agent.tar.gz -C /usr/local cp db_agentd /etc/init.d chmod +x /etc/init.d/db_agentd chkconfig db_agentd on /bin/chown -R ${AGENT_USER}.${AGENT_GROUP} /usr/local/db_agent sleep 3 /usr/local/db_agent/bin/db_agent -c /usr/local/db_agent/etc/db_agent.cfg -d /usr/local/mysql/bin/mysql -e "grant usage,select,replication client,replication slave,PROCESS on *.* to db_monitor@10.20.126.32 identified by 'Mo0219oi1pqX';" /usr/local/mysql/bin/mysql -e "grant usage,select,replication client,replication slave,PROCESS on *.* to db_monitor@10.20.126.33 identified by 'Mo0219oi1pqX';" /usr/local/mysql/bin/mysql -e "grant select,insert,update,delete,super,reload on *.* to db_failover@10.20.126.44 identified by 'FKbnqxEw6htuDBSk';" /usr/local/mysql/bin/mysql -e "grant select,insert,update,delete,super,reload on *.* to db_failover@10.20.126.43 identified by 'FKbnqxEw6htuDBSk';" /usr/local/mysql/bin/mysql -e "grant select,insert,update,delete,super,reload on *.* to db_failover@10.20.126.49 identified by 'FKbnqxEw6htuDBSk';" userCnt=$(cat /etc/sudoers|grep ${AGENT_USER}|grep -v grep|wc -l) if [ ${userCnt} -eq 0 ];then echo "agent ALL=(root) NOPASSWD:/usr/local/db_agent/libexec/check_raid.pl,/usr/sbin/MegaCli,/usr/local/db_agent/libexec/check_dirsize11.sh,/usr/local/db_agent/libexec/check_logfiles" >> /etc/sudoers echo "sudo user ${AGENT_USER} added" else echo "sudo user ${AGENT_USER} already exists, skipping this step !" fi } echo_start() { local STR="$1" echo "...... ${STR} ......starting at $(date)" } echo_end() { local STR="$1" echo "${STR} done at $(date)" } main() { # set env charset=utf8 MYSQL_USER=mysql MYSQL_GROUP=mysql AGENT_USER=agent AGENT_GROUP=agent DIR_KEY="/data1" INSTALL_DIR="/usr/local/mysql" DATA_DIR="/data/mysql_data" SOCK_FILE="/tmp/mysql.sock" CNF_DIR=${DATA_DIR} MYSQL_VERSION="5.6.35" CUR_DIR=$(dirname $0) MYSQL_FILE="mysql-${MYSQL_VERSION}.tar.gz" MYSQL_PARA_FILE=${CUR_DIR}/conf/para.txt TMP_CONF_FILE=${CUR_DIR}/conf/my.cnf TEMPLATE_CONF_FILE=${CUR_DIR}/conf/my.cnf.template CHARSET_COLLATION_FILE=${CUR_DIR}/charset_collation.txt cd ${CUR_DIR} PWDDIR=$(pwd) echo_start "check rpm" check_rpm echo_end "check rpm" echo_start "check user" check_and_add_user echo_end "check user" echo_start "check directory" check_directory echo_end "check directory" echo_start "install mysql" install_mysql echo_end "install mysql" echo_start "set mysql parameters" set_mysql_parameters echo_end "set mysql parameters" echo_start "post tasks" do_post_tasks echo_end "post tasks" echo_start "install db agent" install_db_agent echo_end "install db agent" echo_start "install percona tools" install_percona echo_end "install percona tools" echo "###### all done ########" } D_CHAR=utf8 while getopts :hc: OPTION;do case $OPTION in h) help exit 1 ;; c) D_CHAR=$OPTARG #echo ${D_CHAR} ;; \?) help exit 1 ;; esac done main $1