[replication] 自动配置slave的shell 脚本

根据输入的master ip地址,还有复制的账号密码,检查master和slave的状态,同时给出配置slave的语句

 

效果如下

 

 

    read -p "please input master ip address :" master_ip
      
    replication_user_name=repl
    replication_password=repl
    
    export PATH=${PATH}:/usr/local/mysql/bin
    
    master_mysql_port=$( ssh root@"$master_ip" netstat -ntlp|grep mysql|awk '{print $4}'|awk 'BEGIN{FS=":"}{print $2}')
    
    # check master tcp/port is fined
  nc -z "$master_ip" "$master_mysql_port"  
    
    if [ $? -eq 0 ] ; then
      
      echo "your connection is OK!" 
      
      master_server_id=$(mysql --protocol=tcp --host="$master_ip" --port="$master_mysql_port" --execute "show variables like 'server_id'" | awk '(NR==2){print $2}')
    
      local_ip=$(ifconfig | awk '(NR==2){print substr($2,6)}')
    
      slave_mysql_port=$(mysql  --execute "show variables like 'port'" | awk '(NR==2){print $2}')
      
      slave_server_id=$(mysql  --execute "show variables like 'server_id'" | awk '(NR==2){print $2}')
    
      master_host_name=$(mysql --protocol=tcp --host="$master_ip" --port="$master_mysql_port" --execute "show variables like '%host%';"|awk '(NR==2){print $2}')
    
      recorded_log_file_name=$(mysql --protocol=tcp --host="$master_ip" --port="$master_mysql_port" --execute "show master status;" | awk '(NR==2){print $1}')
    
      recorded_log_position=$(mysql --protocol=tcp --host="$master_ip" --port="$master_mysql_port" --execute "show master status;" | awk '(NR==2){print $2}')
      
      echo " 
      ------------master information--------------                                    
      master_machine='$master_ip',              
      mysql_port='$master_mysql_port',          
      server_id='$master_server_id',                                 
      MASTER_LOG_FILE='$recorded_log_file_name',              
      MASTER_LOG_POS='$recorded_log_position'                 
                    
      -----------slave information----------------
      server_id='$slave_server_id'              
      mysql_port='$slave_mysql_port'
      
                    
      ------------the salve configration statment--------------
      CHANGE MASTER TO
      MASTER_HOST='$master_host_name',
      MASTER_USER='$replication_user_name',
      MASTER_PASSWORD='$replication_password',
      MASTER_LOG_FILE='$recorded_log_file_name',
      MASTER_LOG_POS='$recorded_log_position';    
      "
     
    if [ "$master_server_id" -eq "$slave_server_id" ];then
                echo "warning!!! master and slave have the same server_id"
    fi
                    
          
    else
      echo "your conection master_ip:master_mysql_port is bad"
    fi

 

posted @ 2016-02-03 22:32  lawrence.li  阅读(232)  评论(0编辑  收藏  举报