mariadb主从复制脚本

之前做主从复制实验的时候写的,有些bug,懵!!!!!!!!!!

求大佬指点

使用方法 ./脚本  主节点IP  从节点IP  root密码

#!/bin/bash
cat > /etc/motd <<EOF 
     ##        ##           ##      #########  ########## ######### #########
    ####      ####         ####     #              ##     ##        #       #
   ##  ##    ##  ##       ##  ##    #########      ##     ##        #########
  ##    ##  ##    ##     ########           #      ##     ######### ###
 ##      ####      ##   ##      ##          #      ##     ##        #  ##
##        ##        ## ##        ## #########      ##     ######### #    ####
EOF

# -----1

export master_ip=$1
export node_ip=$2
export root_password=$3

# -----2

# 写入hosts文件
host() {
echo  "$master_ip master"  >> /etc/hosts
echo  "$node_ip node"  >> /etc/hosts

systemctl stop firewalld >> /dev/null
yum remove firewalld -y
yum install expect -y 
}
host
echo "基础环境配置完成"
# -----3

# 配置免秘钥登录

/usr/bin/expect << EOF
   set time 5
     spawn ssh-keygen -t rsa
     expect {
            "*" {
                send "\r"
                }
   expect eof
EOF
/usr/bin/expect << EOF
 set time 5
        spawn ssh-copy-id $master_ip;
        expect {
                "*yes/no" {
                             send "yes\r"; exp_continue 
                            }
                "*password:" {
                              send "$root_password\r"
                             }
               }
   expect eof
EOF
/usr/bin/expect << EOF
 set time 5
        spawn ssh-copy-id $node_ip;
        expect {
                "*yes/no" {
                             send "yes\r"; exp_continue
                            }
                "*password:" {
                              send "$root_password\r"
                             }
               }
   expect eof
EOF

echo "已完成免密钥登陆配置"
-----4

#安装mariadb
mariadb() {

yum install mariadb mariadb-server -y
/usr/bin/expect << EOF
 set time 5
        spawn ssh-copy-id $node_ip;
        expect {
                "Disallow root login remotely?" {
                                                  send "n\r"
                                                 }
                "*y/n" {
                             send "y\r"; exp_continue
                            }
                "*password:" {
                              send "$root_password\r"
                             }
                "(enter for none)"{
                                  send "\r"
                                  }
               }
   expect eof
EOF

mariadb
}

#构建mariadb初始化脚本

cat >> /root/mariadb.sh << EOF
#!/bin/bash

mariadb() {

yum install mariadb mariadb-server -y
/usr/bin/expect << EOF
 set time 5
        spawn ssh-copy-id $node_ip;
        expect {
                "Disallow root login remotely?" {
                                                  send "n\r"
                                                 }
                "*y/n" {
                             send "y\r"; exp_continue
                            }
                "*password:" {
                              send "$root_password\r"
                             }
                "(enter for none)"{
                                  send "\r"
                                  }
               }
   expect eof
EOF

mariadb
}
EOF

scp /root/mariadb.sh $master_ip:/root/
rm -f /root/mariadb.sh
ssh root@$node_ip chmod +x mariadb.sh
ssh root@$node_ip source /root/mariadb
sleep 20
ssh root@$node_ip rm -f /root/mariadb.sh
sleep 2

-----5

# 赋权

mysql -uroot -p$root_password -e"grant all privileges on *.* to root@'%' identified by'$root_password';"

echo "已完成"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值