zoukankan      html  css  js  c++  java
  • shell脚本 mysql主从

    #!/bin/bash
    systemctl stop firewalld
    setenforce 0
    user="tom"
    password="123"
    slave_ipaddr="192.168.52.34"   #这里写自己从的IP地址
    master_ipaddr="192.168.52.33"   #这里写自己主的IP地址
    yum -y install openssh-clients   #安装ssh检测工具
    yum -y install mariadb mariadb-server  #安装mysql及其依赖文件
    sed -i '/^[mysqld]$/aserver-id=1' /etc/my.cnf   #在配置文件my.cnf添加
    sed -i '/^[mysqld]$/alog-bin=mysql-bin' /etc/my.cnf   #在配置文件my.cnf添加
    sed -i '/^[mysqld]$/a
    elay-log=mysql-relay' /etc/my.cnf   #在配置文件my.cnf添加
    systemctl restart mariadb   #启动mysql
    mysql -e "grant all on *.* to '$user'to'$slave_ipaddr' identified by '$password';"  #给用户授权
    master_status=`mysql -e "show master status;"`
    echo "$master_status"  #打印master_status
    master_file=`echo "$master_status" | grep "bin" | awk '{print $1}'`
    echo "$master_file"
    master_pos=`echo "$master_status" | grep "bin"  | awk '{print $2}'`
    echo "$master_pos"
    ssh root@$slave_ipaddr 2>&1 <<eof
    systemctl stop firewalld
    setenforce 0
    yum -y install mariadb mariadb-server
    sed -i '/^[mysqld]$/aserver-id=2' /etc/my.cnf
    sed -i '/^[mysqld]$/alog-bin=mysql-bin' /etc/my.cnf
    sed -i '/^[mysqld]$/a
    elay-log=mysql-relay' /etc/my.cnf
    systemctl restart mariadb
    mysql -e "change  master to master_host='$master_ipaddr', master_password='$password', master_user='$user', master_log_file='$master_file', master_log_pos=$master_pos;"
    mysql -e "fluesh privileges;"
    mysql -e "start slave;"
    mysql -e "show slave status G;"
    check=`mysql -e "show slave status G;" | grep Yes | wc -l`
    if [ $check == 1 ]; then
            echo "仅输出一个yes"
    elif [ $check == 2 ]; then
            echo "输出俩个yes"
    else
            echo "mariadb错误"
    fi
    eof
  • 相关阅读:
    [C++] const 限定符
    [国嵌笔记][028][Bootloader设计蓝图]
    [国嵌笔记][027][ARM协处理器访问指令]
    [国嵌笔记][026][ARM伪指令]
    [国嵌笔记][025][ARM指令分类学习]
    [国嵌笔记][024][ARM汇编编程概述]
    [国嵌笔记][023][ARM寻址方式]
    [国嵌笔记][021-022][ARM处理器工作模式]
    [国嵌笔记][020][ARM家族大检阅]
    [国嵌笔记][019][Eclipse集成开发环境]
  • 原文地址:https://www.cnblogs.com/Zrecret/p/11995354.html
Copyright © 2011-2022 走看看