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

 找回密码
 立即注册

一键登录:

搜索
热搜: 活动 交友 discuz
查看: 898|回复: 0

shell-script-yum安装配置mysql主从

[复制链接]

22

主题

69

帖子

2156

积分

金牌会员

Rank: 6Rank: 6

积分
2156
发表于 2015-4-28 16:00:44 | 显示全部楼层 |阅读模式
本帖最后由 kongzi68 于 2015-4-28 16:16 编辑
  1. #!/bin/bash
  2. #auto install mysql master and slave
  3. #by colink in 2015-04-28


  4. echo -e "\033[32m---------------------------------\033[0m"

  5. IPADDR=`ifconfig |grep Bcast |awk '{print $2}'|sed 's/addr://g'`
  6. MASTER_IP=192.168.2.230
  7. SYNC_USER='tongbu'
  8. SYNC_PASSWORD='123456'

  9. sleep 2
  10. if [ "$IPADDR" = "192.168.2.230" ] ;then
  11.         echo -e "\033[32mInstall mysql master\033[0m"
  12.         yum -y install ntpdate mysql-server mysql-devel mysql
  13.         echo -e "\033[32mPlease waiting,ntpdate is working...\033[0m"
  14.         ntpdate pool.ntp.org
  15.         sleep 3
  16.         rm -rf /etc/my.cnf
  17.         echo "" > /etc/my.cnf
  18.         sed -i  '1a\[mysqld]\ndatadir=/var/lib/mysql\nsocket=/var/lib/mysql/mysql.sock\nuser=mysql\nsymbolic-links=0\nlog-bin=mysql-bin\nserver-id=1\nauto_increment_offset=1\nauto_increment_increment=2\n\n[mysqld_safe]\nlog-error=/var/log/mysqld.log\npid-file=/var/run/mysqld/mysqld.pid\nreplicate-do-db=all' /etc/my.cnf
  19.         echo -e "\033[32mMysql master is starting,please wait...\033[0m"
  20.         sleep 1
  21.         /etc/init.d/mysqld restart
  22.         if test $? -eq 0;then
  23.             echo -e "\033[32mMysql master was started successfully...\033[0m"
  24.         else
  25.             exit
  26.         fi
  27. #        sleep 1
  28.         echo -e "\033[32m请输入mysql初始密码,密码为空,直接按回车键即可! \033[0m"
  29.         mysqladmin -hlocalhost -uroot -p password 123456
  30.         mysql -hlocalhost -uroot -p123456 -e "grant replication slave on *.* to '$SYNC_USER'@'%' identified by '$SYNC_PASSWORD';flush privileges;"

  31.         #定义binlog日志变量
  32.         BINLOGNAME=`mysql -hlocalhost -uroot -p123456 -e "show master status \G" |grep File |awk '{print $2}'`
  33.         BINLOGNODE=`mysql -hlocalhost -uroot -p123456 -e "show master status \G" |grep Position |awk '{print $2}'`
  34.         echo "BINLOGNAME=$BINLOGNAME" >> /tmp/binlog.txt
  35.         echo "BINLOGNODE=$BINLOGNODE" >> /tmp/binlog.txt
  36.         echo -e "\033[32m拷贝binlog相关信息到slave主机上,请按提示输入yes和密码...\033[0m"
  37.         scp /tmp/binlog.txt /root/$0 root@"192.168.2.231":/root/
  38.         if [ $? -eq 0 ];then
  39.             echo
  40.             echo -e "\033[32m已成功拷贝到192.168.2.231的/root/下\033[0m"
  41.         fi
  42.         rm -rf /tmp/binlog.txt
  43.                         
  44. else
  45.         echo -e "\033[32mInstall mysql slave\033[0m"
  46.         yum -y install ntpdate mysql-server mysql-devel mysql
  47.         echo -e "\033[32mPlease waiting,ntpdate is working...\033[0m"
  48.         ntpdate pool.ntp.org
  49.         sleep 3
  50.         rm -rf /etc/my.cnf
  51.         echo "" > /etc/my.cnf
  52.         sed -i '1a\[mysqld]\ndatadir=/var/lib/mysql\nsocket=/var/lib/mysql/mysql.sock\nuser=mysql\nsymbolic-links=0\nserver-id=2\nauto_increment_offset=2\nauto_increment_increment=2\n\n[mysqld_safe]\nlog-error=/var/log/mysqld.log\npid-file=/var/run/mysqld/mysqld.pid\nreplicate-do-db=all' /etc/my.cnf
  53.         /etc/init.d/mysqld restart
  54.         if [ $? -eq 0 ];then
  55.             echo -e "\033[32mMysql slave was started successfully...\033[0m"
  56.         else
  57.             exit
  58.         fi
  59.         sleep 1
  60.         echo -e "\033[32m请输入mysql初始密码,密码为空,直接按回车键即可! \033[0m"
  61.         mysqladmin -hlocalhost -uroot -p password 123456

  62.         BINLOGNAME=`cat /root/binlog.txt |grep BINLOGNAME | awk '{print $1}' |sed 's/BINLOGNAME=//g'`
  63.         BINLOGNODE=`cat /root/binlog.txt |grep BINLOGNODE | awk '{print $1}' |sed 's/BINLOGNODE=//g'`

  64.         mysql -hlocalhost -uroot -p123456 -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;"
  65.         
  66.         #check start status;
  67.         SLAVE_IO_STATUS=`mysql -hlocalhost -uroot -p123456 -e "show slave status \G" |grep Slave_IO_Running | awk '{print $2}'`
  68.         SLAVE_SQL_STATUS=`mysql -hlocalhost -uroot -p123456 -e "show slave status \G" |grep Slave_SQL_Running | awk '{print $2}'`
  69.         echo
  70.         echo -e "\033[32m\n--------------------------\033[0m"
  71.         if [ "$SLAVE_IO_STATUS" = 'Yes' -a "$SLAVE_SQL_STATUS" = 'Yes' ];then
  72.              echo -e "\033[32mThe mysql master-slave was installed sucessfuly!\033[0m"
  73.         else
  74.              echo -e "\033[32mThe mysql master-slave can't start,Please check ... \033[0m"
  75.         fi

  76. fi
复制代码

用命令yum -y install lrzsz  安装rz和sz命令工具,用rz命令上传脚本到/root/目录下:执行sh 脚本名称    即可。比如:  sh mysql_master_slave.sh
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2019-3-20 09:04 , Processed in 0.036248 second(s), 14 queries , Apc On.

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