Linux/Python学习论坛-京峰教育

 找回密码
 立即注册

一键登录:

搜索
热搜: 活动 交友 discuz
查看: 2106|回复: 2

shell-scripts通过yum搭建LAMP和mysql主从

[复制链接]

22

主题

69

帖子

1630

积分

金牌会员

Rank: 6Rank: 6

积分
1630
发表于 2015-5-13 15:35:53 | 显示全部楼层 |阅读模式
本帖最后由 kongzi68 于 2015-5-13 15:38 编辑

脚本功能:1、初始化系统环境;2、yum搭建LAMP;3、搭建mysql主从
----------------------------------------------------------------------------------
  1. #!/bin/bash
  2. #select menu yum install lamp and mysql master_slave
  3. #by colink in 2015-04-28,05-13
  4. #version v.c.m.0.3

  5. BACKUP_DIR="/data/backup/`date +%Y%m%d`/"
  6. IPADDR=`ifconfig eth0|grep Bcast |awk '{print $2}'|sed 's/addr://g'`

  7. #判断IP是否符合标准规则
  8. function judge_ip(){
  9.     #这里local $1出错,用2>/dev/null屏蔽掉错误,暂未发现影响输出结果
  10.     local $1 2>/dev/null
  11.     TMP_TXT='/tmp/iptmp.txt'
  12.     echo $1 > ${TMP_TXT}
  13.     IP_ADDR=`grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' ${TMP_TXT}`
  14.     #判断有没有符合***.***.***.***规则的IP
  15.     if [ ! -z "${IP_ADDR}" ];then
  16.         local j=0;
  17.         #通过循环来检测每个点之前的数值是否符合要求
  18.         for ((i=1;i<=4;i++))
  19.         do
  20.             local IP_NUM=`echo "${IP_ADDR}" |awk -F. "{print [        DISCUZ_CODE_0        ]quot;$i"}"`
  21.             #判断IP_NUM是否在0与255之间
  22.             if [ "${IP_NUM}" -ge 0 -a "${IP_NUM}" -le 255 ];then
  23.                 ((j++));
  24.             else
  25.                 return 1
  26.             fi
  27.         done
  28.         #通过j的值来确定是否继续匹配规则,循环四次,若都正确j=4.
  29.         if [ "$j" -eq 4 ];then
  30.             #确认是否为自己想要输入的IP地址
  31.             read -n 1 -p "输入IP地址是${IP_ADDR},确认输入:Y|y;重新输入:R|r:" OK
  32.             case ${OK} in
  33.                 Y|y) rm -rf ${TMP_TXT} ; return 0;;
  34.                 R|r) return 1;;
  35.                 *) return 1;;
  36.             esac
  37.         else
  38.             return 1
  39.         fi
  40.     else
  41.         return 1
  42.     fi
  43. }

  44. #初始化系统,安装vi等,关闭防火墙和selinux
  45. function init_system_environment(){
  46.         echo -e "\033[32mNow,Will begin initialization system,Please wait...\033[0m"
  47.         yum -y install cmake vim wget lrzsz unzip man ntpdate gcc* \
  48. autoconf libtool python-devel libXpm-devel ncurses-devel git
  49.         echo "alias vi='vim'" >>/root/.bashrc ; source /root/.bashrc ;
  50.         chkconfig --level 3 iptables off ; chkconfig --level 3 ip6tables off
  51.         ntpdate pool.ntp.org
  52.         sed -i "/^SELINUX=enforcing/s/enforcing/disabled/g" /etc/selinux/config
  53.         if [ $? -eq 0 ];then
  54.                 echo -e "\033[32mINIT ststem done. Will reboot system, Input Y|y or N|n :\033[0m"
  55.                 read -n 1 do_reboot
  56.                 case ${do_reboot} in
  57.                         Y|y) init 6 ;;
  58.                         N|n) break ;;
  59.                 esac
  60.         fi
  61. }
  62. #定义依赖包安装函数是否运行的状态
  63. DEPEND_STATUS=1
  64. #DEPEND_STATUS=0时,表示已经运行过;等于1时,表示未运行
  65. #yum安装lamp需要的各种包函数
  66. function install_depend(){
  67.     yum -y install zlib zlib-devel libpng libpng-devel freetype \
  68. freetype-devel libart_lgpl libart_lgpl-devel libxml2 libxml2-devel \
  69. cairo cairo-devel pango pango-devel perl-devel \
  70. cjkuni-ukai-fonts.noarch  cjkuni-uming-fonts.noarch
  71. }
  72. #YUM 安装apache
  73. function install_apache(){
  74.         if [ ${DEPEND_STATUS} -eq 1 ];then
  75.                 install_depend;
  76.                 if [ $? -eq 0 ];then
  77.                         ${DEPEND_STATUS}=0;
  78.                 fi
  79.         fi
  80.         echo -e "\033[32mWill yum install apache,Please wait...\033[0m"
  81.         yum -y install apr apr-devel httpd httpd-devel
  82.         if [ $? -eq 0 ];then
  83.                 echo -e "\033[32mThe apache was installed successfully...\033[0m"
  84.         else
  85.                 echo -e "\033[31mThe apache was installed failed,Please check...\033[0m"
  86.     fi
  87. }
  88. #YUM 安装PHP
  89. function install_php(){
  90.     if [ ${DEPEND_STATUS} -eq 1 ];then
  91.         install_depend;
  92.         if [ $? -eq 0 ];then
  93.             ${DEPEND_STATUS}=0;
  94.         fi
  95.     fi
  96.     echo -e "\033[32mWill yum install php,Please wait...\033[0m"
  97.     yum -y install gd gd-devel php php-devel php-mysql php-gd \
  98. php-mbstring php-pear php-pecl* php-xml php-xmlrpc php-snmp php-soap
  99.     if [ $? -eq 0 ];then
  100.         echo -e "\033[32mThe PHP was installed successfully...\033[0m"
  101.     else
  102.         echo -e "\033[31mThe PHP was installed failed,Please check...\033[0m"
  103.     fi
  104. }
  105. #YUM 安装mysql
  106. function install_mysql(){
  107.     echo -e "\033[32mWill yum install mysql,Please wait...\033[0m"
  108.         yum -y install mysql mysql-server mysql-devel
  109.     if [ $? -eq 0 ];then
  110.         echo -e "\033[32mThe mysql was installed successfully...\033[0m"
  111.                 service mysqld restart;
  112.     else
  113.         echo -e "\033[31mThe mysql was installed failed,Please check...\033[0m"
  114.     fi
  115. }
  116. #整合apache与PHP的配置函数
  117. HTTPD_CONF='/etc/httpd/conf/httpd.conf'
  118. HTTPD_HTML_DIR='/var/www/html/'
  119. CHECK_PHP_FILES='phpinfo.php'
  120. function mod_httpd_conf(){
  121.         echo -e "\033[32mWill Modify httpd.conf Profile,Please wait...\033[0m"
  122.         sleep 3
  123.         if [ ! -d ${BACKUP_DIR} ];then
  124.                 mkdir -p ${BACKUP_DIR}
  125.         fi
  126.         cp ${HTTPD_CONF} ${BACKUP_DIR}httpd`date +%Y%m%d%k%M%S`.conf
  127.         sed -i "/#ServerName/s/#//g" ${HTTPD_CONF} ;
  128.         sed -i "/Options Indexes FollowSymLinks/s/Indexes//g" ${HTTPD_CONF} ;
  129.         sed -i "/DirectoryIndex index.html index.html.var/s/DirectoryIndex/\
  130. DirectoryIndex index.php/g" ${HTTPD_CONF} ;
  131.         echo "Addtype application/x-httpd-php  .php  .phtml" >> ${HTTPD_CONF} ;
  132.         echo -e "\033[32mModify httpd.conf profile done.\033[0m"
  133.         #create phpinfo.php for check PHP
  134.         cat >${HTTPD_HTML_DIR}${CHECK_PHP_FILES} <<EOF
  135. <?php
  136. phpinfo();
  137. ?>
  138. EOF
  139.         if [ -f ${HTTPD_HTML_DIR}${CHECK_PHP_FILES} ];then
  140.                 /etc/init.d/httpd restart 2>&1
  141.                 if [ $? -eq 0 ];then
  142.                         echo -e "\033[32mPlease check PHP. Usage:  \
  143. http://${IPADDR}/${CHECK_PHP_FILES}\033[0m"
  144.                 fi
  145.         fi
  146. }

  147. #mysql主从服务搭建的函数
  148. MYSQL_USER='root'
  149. MYSQL_PASSWORD='123456'
  150. SYNC_USER='tongbu'
  151. SYNC_PASSWORD='123456'
  152. MYSQL_CONF='/etc/my.cnf'
  153. BINLOG_NODE='/tmp/binlog.txt'
  154. #define mysql master function
  155. function mysql_master(){
  156.         MYSQL_RUN_STATUS=`ps -ef |grep "mysql" |grep -Ev "grep|lamp" |wc -l`
  157.         echo -e "\033[32m=============================================\033[0m"
  158.         echo -e "\033[32mPlease waiting,ntpdate is working...\033[0m"
  159.         ntpdate pool.ntp.org
  160.         sleep 1
  161.     if [ "${MYSQL_RUN_STATUS}" -eq 0 ];then
  162.         echo -e "\033[32mThe mysql didn't installed,Now running install_mysql ...\033[0m"
  163.                 install_mysql;
  164.     fi
  165.         if [ ! -d ${BACKUP_DIR} ];then
  166.         mkdir -p ${BACKUP_DIR}
  167.     fi
  168.     cp ${MYSQL_CONF} ${BACKUP_DIR}my`date +%Y%m%d%k%M%S`.conf
  169.         #导入my.cnf配置文件内容
  170.         cat >${MYSQL_CONF} <<EOF
  171. [mysqld]
  172. datadir=/var/lib/mysql
  173. socket=/var/lib/mysql/mysql.sock
  174. user=mysql
  175. symbolic-links=0
  176. log-bin=mysql-bin
  177. server-id=1
  178. auto_increment_offset=1
  179. auto_increment_increment=2
  180. [mysqld_safe]
  181. log-error=/var/log/mysqld.log
  182. pid-file=/var/run/mysqld/mysqld.pid
  183. replicate-do-db=all
  184. EOF
  185.         echo -e "\033[32mThe mysql master is starting,please wait...\033[0m"
  186.         sleep 1
  187.         rm -rf ${BINLOG_NODE}
  188.         find /var/lib/mysql/ -name "*mysql-bin*" -exec rm -rf {} \;;
  189.         /etc/init.d/mysqld restart
  190.         if [ "${MYSQL_RUN_STATUS}" -gt 0 ];then
  191.             echo -e "\033[32mThe mysql master was started successfully...\033[0m"
  192.         else
  193.                 echo -e "\033[31mThe mysql master was started failed, Please check...\033[0m"
  194.             exit;
  195.         fi
  196.         sleep 1
  197.         mysqladmin -u${MYSQL_USER} password ${MYSQL_PASSWORD}
  198.         mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "delete from mysql.user where User='';"
  199.         mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "grant replication slave on *.* \
  200. to '$SYNC_USER'@'%' identified by '$SYNC_PASSWORD';flush privileges;"
  201.         #定义binlog日志变量
  202.         BINLOGNAME=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show master \
  203. status \G" |grep File |awk '{print $2}'`
  204.         BINLOGNODE=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show master \
  205. status \G" |grep Position |awk '{print $2}'`
  206.         echo "BINLOGNAME=$BINLOGNAME" >> ${BINLOG_NODE}
  207.         echo "BINLOGNODE=$BINLOGNODE" >> ${BINLOG_NODE}
  208.         echo "MASTER_IP=`ifconfig eth0|grep Bcast |awk '{print $2}'|sed 's/addr://g'`" >> ${BINLOG_NODE}
  209.         echo -e "\033[32m拷贝binlog相关信息到slave主机上,请按提示输入相应的内容。\033[0m"
  210.         read -p "Please input mysql-slave IPADDR: " SLAVE_IP
  211.         #判断输入的IP是否合法
  212.         judge_ip ${SLAVE_IP};
  213.         until [ $? -eq 0 ];do
  214.                 read -p "Error IPADDR, Please input mysql-slave IPADDR again: " SLAVE_IP
  215.                 judge_ip ${SLAVE_IP};
  216.         done
  217.         echo
  218.         scp ${BINLOG_NODE} /root/$0 root@${SLAVE_IP}:/root/
  219.         if [ $? -eq 0 ];then
  220.             echo
  221.             echo -e "\033[32m已成功拷贝到mysql-slave,IP:${SLAVE_IP}的/root/下\033[0m"
  222.         fi
  223. }
  224. #define mysql-slave function
  225. function mysql_slave(){
  226.     MYSQL_RUN_STATUS=`ps -ef |grep "mysql" |grep -Ev "grep|lamp" |wc -l`
  227.     echo -e "\033[32m=============================================\033[0m"
  228.     echo -e "\033[32mPlease waiting,ntpdate is working...\033[0m"
  229.     ntpdate pool.ntp.org
  230.     sleep 1
  231.     if [ "${MYSQL_RUN_STATUS}" -eq 0 ];then
  232.         echo -e "\033[32mThe mysql didn't installed,Now running install_mysql ...\033[0m"
  233.         install_mysql;
  234.     fi
  235.     if [ ! -d ${BACKUP_DIR} ];then
  236.                 mkdir -p ${BACKUP_DIR}
  237.     fi
  238.     cp ${MYSQL_CONF} ${BACKUP_DIR}my`date +%Y%m%d%k%M%S`.conf
  239.     #导入my.cnf配置文件内容
  240.     cat >${MYSQL_CONF} <<EOF
  241. [mysqld]
  242. datadir=/var/lib/mysql
  243. socket=/var/lib/mysql/mysql.sock
  244. user=mysql
  245. symbolic-links=0
  246. #log-bin=mysql-bin
  247. server-id=2
  248. auto_increment_offset=2
  249. auto_increment_increment=2
  250. [mysqld_safe]
  251. log-error=/var/log/mysqld.log
  252. pid-file=/var/run/mysqld/mysqld.pid
  253. replicate-do-db=all
  254. EOF
  255.     echo -e "\033[32mThe mysql-slave is starting,please wait...\033[0m"
  256.     sleep 1
  257.     /etc/init.d/mysqld restart
  258.     if [ "${MYSQL_RUN_STATUS}" -gt 0 ];then
  259.         echo -e "\033[32mThe mysql-slave was started successfully...\033[0m"
  260.     else
  261.         echo -e "\033[31mThe mysql-slave was started failed, Please check...\033[0m"
  262.                 exit;
  263.     fi
  264.     sleep 1
  265.     mysqladmin -u${MYSQL_USER} password ${MYSQL_PASSWORD}
  266.         mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "delete from mysql.user where User='';"
  267.     BINLOGNAME=`cat /root/binlog.txt |grep "BINLOGNAME" | awk '{print $1}' |sed 's/BINLOGNAME=//g'`
  268.     BINLOGNODE=`cat /root/binlog.txt |grep "BINLOGNODE" | awk '{print $1}' |sed 's/BINLOGNODE=//g'`
  269.     MASTER_IP=`cat /root/binlog.txt |grep "MASTER_IP" | awk '{print $1}' |sed 's/MASTER_IP=//g'`
  270.     mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "change master to master_host='$MASTER_IP',\
  271. master_user='$SYNC_USER',master_password='$SYNC_PASSWORD',master_log_file='$BINLOGNAME',\
  272. master_log_pos=$BINLOGNODE;start slave;"
  273.     #check start status;
  274.     SLAVE_IO_STATUS=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show slave status \
  275. \G" |grep Slave_IO_Running | awk '{print $2}'`
  276.     SLAVE_SQL_STATUS=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show slave status \
  277. \G" |grep Slave_SQL_Running | awk '{print $2}'`
  278.     echo
  279.     echo -e "\033[32m\n--------------------------\033[0m"
  280.     if [ "$SLAVE_IO_STATUS" = 'Yes' -a "$SLAVE_SQL_STATUS" = 'Yes' ];then
  281.          echo -e "\033[32mThe mysql master-slave was installed sucessfuly!\033[0m"
  282.     else
  283.          echo -e "\033[32mThe mysql master-slave can't start,Please check ... \033[0m"
  284.     fi
  285. }
  286. #功能选择菜单
  287. menu=(
  288.         init_system_environment
  289.         install_apache
  290.         install_php
  291.         install_mysql
  292.         mod_httpd_conf
  293.         mysql_master
  294.         mysql_slave
  295.         exit_menu
  296.         help_menu
  297. )
  298. #因为习惯性的把help放在最后,所以这里用${#menu[@]}
  299. PS3="Please select menu will running to do (Need help,Please input: ${#menu[@]} ): "
  300. select i in ${menu[@]}
  301. do
  302.         case $i in
  303.         ${menu[0]}) ${menu[0]} ;;
  304.         ${menu[1]}) ${menu[1]} ;;
  305.         ${menu[2]}) ${menu[2]} ;;
  306.         ${menu[3]}) ${menu[3]} ;;
  307.         ${menu[4]}) ${menu[4]} ;;
  308.         ${menu[5]}) ${menu[5]} ;;
  309.         ${menu[6]}) ${menu[6]} ;;
  310.         ${menu[7]}) exit ;;
  311.         ${menu[8]})
  312.                 echo -e "\033[32m=========帮助菜单内容如下==========\033[0m"
  313.                 for ((i=0;i<"${#menu[@]}";i++))
  314.                 do
  315.                         echo -e "\033[33m `expr $i + 1`) ${menu[i]} \033[0m"
  316.                 done
  317.                 ;;
  318.         esac
  319. done
复制代码
------------------------
QQ截图20150513152938.jpg
------------------------------ yum_lamp_mysql_ms.rar (3.93 KB, 下载次数: 28)
回复

使用道具 举报

20

主题

77

帖子

1043

积分

超级版主

Rank: 8Rank: 8

积分
1043
QQ
发表于 2015-5-14 16:57:50 | 显示全部楼层
看起来好复杂呀。很强大
回复 支持 反对

使用道具 举报

1

主题

34

帖子

1507

积分

金牌会员

Rank: 6Rank: 6

积分
1507
发表于 2015-6-5 09:21:57 | 显示全部楼层
看不懂呀,大神
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|京峰教育,只为有梦想的人 ( 京ICP备15013173号 )

GMT+8, 2017-5-29 00:26 , Processed in 0.043913 second(s), 21 queries , Apc On.

快速回复 返回顶部 返回列表