首页 > 数据库 > MySQL > 正文

shell之路--mysql主从自动布局

2024-07-24 12:35:02
字体:
来源:转载
供稿:网友
  #!/bin/bash
 
  #auto make install Mysql AB Relication
 
  #by author max
 
  #date 2017-05-04
 
  MYSQL_SOFT="mysql mysql-server mysql-devel php-mysql"
 
  NUM=`rpm -qa |grep -i mysql |wc -l`
 
  #mysql to start and config
 
  cat >/etc/my.cnf<<EOF
 
  [mysqld]
 
  datadir=/car/lib/mysql
 
  socket=/var/lib/mysql/mysql.sock
 
  user=mysql
 
  symbpolic-links=0
 
  log-bin=mysql-bin
 
  server-id=1
 
  auto_increment_offset=1
 
  auto_increment_increment=2
 
  [mysql_safe]
 
  log-error=/var/log/mysqld.log
 
  pid-file=/var/run/mysql/mysqld.pid
 
  replicate-do-db=all
 
  EOF
 
  /etc/init.d/mysqld restart
 
  ps -ef |grep mysql
 
  function MYSQL_CONFIG(){
 
  #master config mysql
 
  mysql -e "grant replication slave on *.* to 'max'@'%' identified by 'zhaomiao';"
 
  MASTER_FILE=`mysql -e "show master status;"|tail -1|awk '{print $1}'`
 
  MASTER_POS=`mysql -e "show master status;"|tail -1|awk '{print $2}'`
 
  MASTER_IPADDR=`ifconfig eth0 |grep "Bcast"|awk '{print $2}'|cut -d: -f2`
 
  read -p "please enter input slave ip address": SLAVE_IPADDR
 
  #slave config mysql
 
  ssh -l root $SLAVE_IPADDR "sed -i 's#server-id = 1#server-id = 2#g' /etc/my.cnf"
 
  ssh -l root $SLAVE_IPADDR "/etc/init.d/mysqld restart"
 
  ssh -l root $SLAVE_IPADDR "mysql -e /"change master to master_host='$MASTER_IPADDR',master_user='max',master_password='zhaomiao',master_log_file='$MASTER_FILE',master_log_pos=$MASTER_POS;/""
 
  ssh -l root $SLAVE_IPADDR "mysql -e /"slave start;/""
 
  ssh -l root $SLAVE_IPADDR "mysql -e /"show slave status/G;/""
 
  }
 
  read -p "please ensure your  server is master ? yes or no[y/n]": INPUT
 
  if [ $INPUT == "y" ];then
 
  MYSQL_CONFIG
 
  else
 
  exit 0
 
  fi

(编辑:武林网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表