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

-----------------------

#!/bin/bash
#select menu yum install lamp and mysql master_slave
#by colink in 2015-04-28,05-13
#version v.c.m.0.3

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

#判断IP是否符合标准规则
function judge_ip(){
    #这里local $1出错,用2>/dev/null屏蔽掉错误,暂未发现影响输出结果
    local $1 2>/dev/null
    TMP_TXT=‘/tmp/iptmp.txt‘
    echo $1 > ${TMP_TXT}
    IP_ADDR=`grep -Eo ‘([0-9]{1,3}\.){3}[0-9]{1,3}‘ ${TMP_TXT}`
    #判断有没有符合***.***.***.***规则的IP
    if [ ! -z "${IP_ADDR}" ];then
        local j=0;
        #通过循环来检测每个点之前的数值是否符合要求
        for ((i=1;i<=4;i++))
        do
            local IP_NUM=`echo "${IP_ADDR}" |awk -F. "{print $"$i"}"`
            #判断IP_NUM是否在0与255之间
            if [ "${IP_NUM}" -ge 0 -a "${IP_NUM}" -le 255 ];then
                ((j++));
            else
                return 1
            fi
        done
        #通过j的值来确定是否继续匹配规则,循环四次,若都正确j=4.
        if [ "$j" -eq 4 ];then
            #确认是否为自己想要输入的IP地址
            read -n 1 -p "输入IP地址是${IP_ADDR},确认输入:Y|y;重新输入:R|r:" OK
            case ${OK} in
                Y|y) rm -rf ${TMP_TXT} ; return 0;;
                R|r) return 1;;
                *) return 1;;
            esac
        else
            return 1
        fi
    else
        return 1
    fi
}

#初始化系统,安装vi等,关闭防火墙和selinux
function init_system_environment(){
        echo -e "\033[32mNow,Will begin initialization system,Please wait...\033[0m"
        yum -y install cmake vim wget lrzsz unzip man ntpdate gcc* autoconf libtool python-devel libXpm-devel ncurses-devel git
        echo "alias vi=‘vim‘" >>/root/.bashrc ; source /root/.bashrc ;
        chkconfig --level 3 iptables off ; chkconfig --level 3 ip6tables off
        ntpdate pool.ntp.org
        sed -i "/^SELINUX=enforcing/s/enforcing/disabled/g" /etc/selinux/config
        if [ $? -eq 0 ];then
                echo -e "\033[32mINIT ststem done. Will reboot system, Input Y|y or N|n :\033[0m"
                read -n 1 do_reboot
                case ${do_reboot} in
                        Y|y) init 6 ;;
                        N|n) break ;;
                esac
        fi
}
#定义依赖包安装函数是否运行的状态
DEPEND_STATUS=1
#DEPEND_STATUS=0时,表示已经运行过;等于1时,表示未运行
#yum安装lamp需要的各种包函数
function install_depend(){
    yum -y install zlib zlib-devel libpng libpng-devel freetype freetype-devel libart_lgpl libart_lgpl-devel libxml2 libxml2-devel cairo cairo-devel pango pango-devel perl-devel cjkuni-ukai-fonts.noarch  cjkuni-uming-fonts.noarch
}
#YUM 安装apache
function install_apache(){
        if [ ${DEPEND_STATUS} -eq 1 ];then
                install_depend;
                if [ $? -eq 0 ];then
                        ${DEPEND_STATUS}=0;
                fi
        fi
        echo -e "\033[32mWill yum install apache,Please wait...\033[0m"
        yum -y install apr apr-devel httpd httpd-devel
        if [ $? -eq 0 ];then
                echo -e "\033[32mThe apache was installed successfully...\033[0m"
        else
                echo -e "\033[31mThe apache was installed failed,Please check...\033[0m"
    fi
}
#YUM 安装PHP
function install_php(){
    if [ ${DEPEND_STATUS} -eq 1 ];then
        install_depend;
        if [ $? -eq 0 ];then
            ${DEPEND_STATUS}=0;
        fi
    fi
    echo -e "\033[32mWill yum install php,Please wait...\033[0m"
    yum -y install gd gd-devel php php-devel php-mysql php-gd php-mbstring php-pear php-pecl* php-xml php-xmlrpc php-snmp php-soap
    if [ $? -eq 0 ];then
        echo -e "\033[32mThe PHP was installed successfully...\033[0m"
    else
        echo -e "\033[31mThe PHP was installed failed,Please check...\033[0m"
    fi
}
#YUM 安装mysql
function install_mysql(){
    echo -e "\033[32mWill yum install mysql,Please wait...\033[0m"
        yum -y install mysql mysql-server mysql-devel
    if [ $? -eq 0 ];then
        echo -e "\033[32mThe mysql was installed successfully...\033[0m"
                service mysqld restart;
    else
        echo -e "\033[31mThe mysql was installed failed,Please check...\033[0m"
    fi
}
#整合apache与PHP的配置函数
HTTPD_CONF=‘/etc/httpd/conf/httpd.conf‘
HTTPD_HTML_DIR=‘/var/www/html/‘
CHECK_PHP_FILES=‘phpinfo.php‘
function mod_httpd_conf(){
        echo -e "\033[32mWill Modify httpd.conf Profile,Please wait...\033[0m"
        sleep 3
        if [ ! -d ${BACKUP_DIR} ];then
                mkdir -p ${BACKUP_DIR}
        fi
        cp ${HTTPD_CONF} ${BACKUP_DIR}httpd`date +%Y%m%d%k%M%S`.conf
        sed -i "/#ServerName/s/#//g" ${HTTPD_CONF} ;
        sed -i "/Options Indexes FollowSymLinks/s/Indexes//g" ${HTTPD_CONF} ;
        sed -i "/DirectoryIndex index.html index.html.var/s/DirectoryIndex/DirectoryIndex index.php/g" ${HTTPD_CONF} ;
        echo "Addtype application/x-httpd-php  .php  .phtml" >> ${HTTPD_CONF} ;
        echo -e "\033[32mModify httpd.conf profile done.\033[0m"
        #create phpinfo.php for check PHP
        cat >${HTTPD_HTML_DIR}${CHECK_PHP_FILES} <<EOF
<?php
phpinfo();
?>
EOF
        if [ -f ${HTTPD_HTML_DIR}${CHECK_PHP_FILES} ];then
                /etc/init.d/httpd restart 2>&1
                if [ $? -eq 0 ];then
                        echo -e "\033[32mPlease check PHP. Usage:  http://${IPADDR}/${CHECK_PHP_FILES}\033[0m"
                fi
        fi
}

#mysql主从服务搭建的函数
MYSQL_USER=‘root‘
MYSQL_PASSWORD=‘123456‘
SYNC_USER=‘tongbu‘
SYNC_PASSWORD=‘123456‘
MYSQL_CONF=‘/etc/my.cnf‘
BINLOG_NODE=‘/tmp/binlog.txt‘
#define mysql master function
function mysql_master(){
        MYSQL_RUN_STATUS=`ps -ef |grep "mysql" |grep -Ev "grep|lamp" |wc -l`
        echo -e "\033[32m=============================================\033[0m"
        echo -e "\033[32mPlease waiting,ntpdate is working...\033[0m"
        ntpdate pool.ntp.org
        sleep 1
    if [ "${MYSQL_RUN_STATUS}" -eq 0 ];then
        echo -e "\033[32mThe mysql didn‘t installed,Now running install_mysql ...\033[0m"
                install_mysql;
    fi
        if [ ! -d ${BACKUP_DIR} ];then
        mkdir -p ${BACKUP_DIR}
    fi
    cp ${MYSQL_CONF} ${BACKUP_DIR}my`date +%Y%m%d%k%M%S`.conf
        #导入my.cnf配置文件内容
        cat >${MYSQL_CONF} <<EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
log-bin=mysql-bin
server-id=1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db=all
EOF
        echo -e "\033[32mThe mysql master is starting,please wait...\033[0m"
        sleep 1
        rm -rf ${BINLOG_NODE}
        find /var/lib/mysql/ -name "*mysql-bin*" -exec rm -rf {} \;;
        /etc/init.d/mysqld restart
        if [ "${MYSQL_RUN_STATUS}" -gt 0 ];then
            echo -e "\033[32mThe mysql master was started successfully...\033[0m"
        else
                echo -e "\033[31mThe mysql master was started failed, Please check...\033[0m"
            exit;
        fi
        sleep 1
        mysqladmin -u${MYSQL_USER} password ${MYSQL_PASSWORD}
        mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "delete from mysql.user where User=‘‘;"
        mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "grant replication slave on *.* to ‘$SYNC_USER‘@‘%‘ identified by ‘$SYNC_PASSWORD‘;flush privileges;"
        #定义binlog日志变量
        BINLOGNAME=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show master status \G" |grep File |awk ‘{print $2}‘`
        BINLOGNODE=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show master status \G" |grep Position |awk ‘{print $2}‘`
        echo "BINLOGNAME=$BINLOGNAME" >> ${BINLOG_NODE}
        echo "BINLOGNODE=$BINLOGNODE" >> ${BINLOG_NODE}
        echo "MASTER_IP=`ifconfig eth0|grep Bcast |awk ‘{print $2}‘|sed ‘s/addr://g‘`" >> ${BINLOG_NODE}
        echo -e "\033[32m拷贝binlog相关信息到slave主机上,请按提示输入相应的内容。\033[0m"
        read -p "Please input mysql-slave IPADDR: " SLAVE_IP
        #判断输入的IP是否合法
        judge_ip ${SLAVE_IP};
        until [ $? -eq 0 ];do
                read -p "Error IPADDR, Please input mysql-slave IPADDR again: " SLAVE_IP
                judge_ip ${SLAVE_IP};
        done
        echo 
        scp ${BINLOG_NODE} /root/$0 [email protected]${SLAVE_IP}:/root/
        if [ $? -eq 0 ];then
            echo 
            echo -e "\033[32m已成功拷贝到mysql-slave,IP:${SLAVE_IP}的/root/下\033[0m"
        fi
}
#define mysql-slave function
function mysql_slave(){
    MYSQL_RUN_STATUS=`ps -ef |grep "mysql" |grep -Ev "grep|lamp" |wc -l`
    echo -e "\033[32m=============================================\033[0m"
    echo -e "\033[32mPlease waiting,ntpdate is working...\033[0m"
    ntpdate pool.ntp.org
    sleep 1
    if [ "${MYSQL_RUN_STATUS}" -eq 0 ];then
        echo -e "\033[32mThe mysql didn‘t installed,Now running install_mysql ...\033[0m"
        install_mysql;
    fi
    if [ ! -d ${BACKUP_DIR} ];then
                mkdir -p ${BACKUP_DIR}
    fi
    cp ${MYSQL_CONF} ${BACKUP_DIR}my`date +%Y%m%d%k%M%S`.conf
    #导入my.cnf配置文件内容
    cat >${MYSQL_CONF} <<EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
#log-bin=mysql-bin
server-id=2
auto_increment_offset=2
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db=all
EOF
    echo -e "\033[32mThe mysql-slave is starting,please wait...\033[0m"
    sleep 1
    /etc/init.d/mysqld restart
    if [ "${MYSQL_RUN_STATUS}" -gt 0 ];then
        echo -e "\033[32mThe mysql-slave was started successfully...\033[0m"
    else
        echo -e "\033[31mThe mysql-slave was started failed, Please check...\033[0m"
                exit;
    fi
    sleep 1
    mysqladmin -u${MYSQL_USER} password ${MYSQL_PASSWORD}
        mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "delete from mysql.user where User=‘‘;"
    BINLOGNAME=`cat /root/binlog.txt |grep "BINLOGNAME" | awk ‘{print $1}‘ |sed ‘s/BINLOGNAME=//g‘`
    BINLOGNODE=`cat /root/binlog.txt |grep "BINLOGNODE" | awk ‘{print $1}‘ |sed ‘s/BINLOGNODE=//g‘`
    MASTER_IP=`cat /root/binlog.txt |grep "MASTER_IP" | awk ‘{print $1}‘ |sed ‘s/MASTER_IP=//g‘`
    mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "change master to master_host=‘$MASTER_IP‘,master_user=‘$SYNC_USER‘,master_password=‘$SYNC_PASSWORD‘,master_log_file=‘$BINLOGNAME‘,master_log_pos=$BINLOGNODE;start slave;"
    #check start status;
    SLAVE_IO_STATUS=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show slave status \G" |grep Slave_IO_Running | awk ‘{print $2}‘`
    SLAVE_SQL_STATUS=`mysql  -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e "show slave status \G" |grep Slave_SQL_Running | awk ‘{print $2}‘`
    echo 
    echo -e "\033[32m\n--------------------------\033[0m"
    if [ "$SLAVE_IO_STATUS" = ‘Yes‘ -a "$SLAVE_SQL_STATUS" = ‘Yes‘ ];then
         echo -e "\033[32mThe mysql master-slave was installed sucessfuly!\033[0m"
    else
         echo -e "\033[32mThe mysql master-slave can‘t start,Please check ... \033[0m"
    fi
}
#功能选择菜单
menu=(
        init_system_environment
        install_apache
        install_php
        install_mysql
        mod_httpd_conf
        mysql_master
        mysql_slave
        exit_menu
        help_menu
)
#因为习惯性的把help放在最后,所以这里用${#menu[@]}
PS3="Please select menu will running to do (Need help,Please input: ${#menu[@]} ): "
select i in ${menu[@]}
do
        case $i in
        ${menu[0]}) ${menu[0]} ;;
        ${menu[1]}) ${menu[1]} ;;
        ${menu[2]}) ${menu[2]} ;;
        ${menu[3]}) ${menu[3]} ;;
        ${menu[4]}) ${menu[4]} ;;
        ${menu[5]}) ${menu[5]} ;;
        ${menu[6]}) ${menu[6]} ;;
        ${menu[7]}) exit ;;
        ${menu[8]})
                echo -e "\033[32m=========帮助菜单内容如下==========\033[0m"
                for ((i=0;i<"${#menu[@]}";i++))
                do
                        echo -e "\033[33m `expr $i + 1`) ${menu[i]} \033[0m"
                done
                ;;
        esac
done

-----------------------

----------------------------

shell-scripts通过yum搭建LAMP和mysql主从
http://www.jfedu.net/forum.php?mod=viewthread&tid=1791&fromuid=100

时间: 2024-11-08 20:36:42

shell-scripts通过yum搭建LAMP和mysql主从的相关文章

linux下Yum搭建lamp网站架构

在上节课我用了源代码和免安装软件的两种方式搭建了LAMP架构的网站,今天我们对上一节做个简单的总结: 源代码安装软件是我们Linux下常用的安装方法共分四个步骤: 在上一节我们的数据库就是免安装的.直接解压就可以使用了.是不是很简单,只要我们把前面的命令都掌握了,后面的服务搭建都不是问题. 今天我们讲解用yum搭建lamp网站架构.用yum命令安装软件也是我们Linux下一种常用的,因为他更简单,但是移植性差.好啦开始我们的操作了. 实验环境: 一台单机服务器,操作系统centos6.6.注意服

基于源码编译和yum安装的LNP+MYSQL主从实战

1.基于源码编译安装的LNP+MYSQL主从实战准备3台服务器,其中一台作为Nginx WEB服务器+PHP-FPM(FastCGI),另外两台作为MYSQL主从服务器,服务器IP信息:? 10.10.10.4-Linux+Nginx+PHP? 10.10.10.5-MYSQL Master:? 10.10.10.6-MYSQL Slave:? 1) Nginx安装配置? #安装PCRE库和基础库支持? yum install pcre-devel pcre gcc-c++ openssl op

CentOS7 yum搭建lamp

简易搭建lamp 环境说明: server:CentOS7-192.168.230.202 client: win8.1-192.168.230.59 Apache/2.4.6 php Version 5.4.16 5.5.52-MariaDB yum group install Development Tools #安装开发工具,GCC... yum install httpd mariadb mariadb-server php php-mysql php-gd libjpeg* php-i

CentOS 6.6 yum 搭建LAMP环境

CentOS 查看操作系统版本 [[email protected] ~]# cat /etc/redhat-releaseCentOS release 6.6 (Final) 参考linux centos yum安装LAMP环境 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器. 基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软

Centos yum搭建lamp环境

首先安装Apache2 一条命令即可: yum -y install httpd 回车后,yum会提示当前已经安装的httpd版本,并自动更新:如果没有安装,则会自动安装.注意在yum安装过程中输入"Y"以确认安装. Apache安装完成后,手动启动Apache2: /etc/init.d/httpd start 这时,直接在 浏览器中输入服务器的IP地址,即可看到Apache2的默认页面.此时的Apache仅仅能提供HTTP服务,不能执行php.也不能连接MySQL 数据库. 安装M

yum 搭建LAMP

yum install -y  httpd php php-mysql mysql mysql-server mysql-devel php-gd  libjpeg libjpeg-devel libpng libpng-devel /etc/init.d/httpd start    启动httpd服务 /etc/init.d/mysqld start  启动mysql服务 vim /etc/httpd/conf/httpd.conf   编辑httpd配置文件 找到ServerName Lo

centos6.3利用yum搭建lamp环境

用putty连上服务器后,先更新: [[email protected] /]# yum update 然后安装LAMP环境: [[email protected] /]# yum -y install httpd php mysql mysql-server php-mysql httpd-manual mod_ssl mod_perl mod_auth_mysql php-mcrypt php-gd php-xml php-mbstring php-ldap php-pear php-xml

Centos7.2(1151)使用Yum搭建LAMP+phpMyAdmin

一.实验环境 操作系统 IP地址 相关软件包 Centos7.2_x64 10.0.0.10 http.php.mariadb Windows10_enterprise 10.0.0.2 Google浏览器 二.服务搭建 2.安装Apache 2.1.安装Apache [[email protected] ~]# yum -y install httpd httpd-devel 2.2.启动Apache服务 [[email protected] ~]# systemctl start httpd

Centos7下通过yum搭建apache+php+mysql服务器

1.关闭防火漆 systemctl stop firewalld.service#关闭防火墙 systemctl disable firewalld.service#禁止防火墙开机启动 2.安装apache yum install httpd -y systemctl start httpd.service 3.安装php 更新软件仓库  rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm  rpm -Uvh https:/