shell系统171115-部署mysql主从

新朋友请点上方shell系统171115-部署mysql主从
 蓝字“阿铭linux”关注

用shell脚本实现,部署mysql主从架构。

思路是这样的:

1)master.sh脚本用来安装master的mysql

2)然后通过expect脚本+rsync工具把slave.sh脚本、/etc/my.cnf、 /etc/init.d/mysqld 还有mysqldump下来的all.sql,以及在master下载下来的mysql二进制安装包传到slave上

3)通过expect脚本来运行slave.sh的脚本来安装,并且配置好主从,期间,用slave.tmp来记录master机子的binlog的状态,以便于传到slave后用命令添加进去。

答案明日公布。

习题171114-自动增加公钥==参考答案==

#!/bin/bash

read -p "Input IP: " ip

ping $ip -w 2 -c 2 >> /dev/null

## 查看ip是否可用

while [ $? -ne 0 ]

do

read -p "your ip may not useable, Please Input your IP: " ip

ping $ip -w 2 -c 2 >> /dev/null

done

read -p "Input root\"s password of this host: " password

## 检查命令子函数

check_ok() {

if [ $? != 0 ]

then

echo "Error!."

exit 1

fi

}

## yum需要用到的包

myyum() {

if ! rpm -qa |grep -q "$1"

then

yum install -y $1

check_ok

else

echo $1  already installed

fi

}

for p in openssh-clients openssh expect

do

myyum $p

done

## 在主机A上创建密钥对

if [ ! -f ~/.ssh/id_rsa ] || [ ! -f ~/.ssh/id_rsa.pub ]

then

    if [ -d ~/.ssh ]

    then

        mv ~/.ssh/  ~/.ssh_old

    fi

    echo -e "\n" | ssh-keygen -t rsa -P ""

    check_ok

fi

## 传私钥给主机B

if [ ! -d /usr/local/sbin/rsync_keys ]

then

    mkdir /usr/local/sbin/rsync_keys

fi

cd /usr/local/sbin/rsync_keys

if [ -f rsync.expect ]

then

    d=`date +%F-%T`

    mv rsync.expect $d.expect

fi

#创建远程同步的expect文件

cat >  rsync.expect <<EOF

#!/usr/bin/expect

set host [lindex \$argv 0]

#主机B的密码

set passwd [lindex \$argv 1]

spawn rsync -av /root/.ssh/id_rsa.pub root@\$host:/tmp/tmp.txt

expect {

"yes/no" { send "yes\r"; exp_continue}

"password:" { send "\$passwd\r" }

}

expect eof

spawn ssh root@\$host

expect {

"password:" { send "\$passwd\r" }

}

expect "]*"                         

send "\[ -f /root/.ssh/authorized_keys \] && cat /tmp/tmp.txt >>/root/.ssh/authorized_keys \r"

expect "]*"

send "\[ -f /root/.ssh/authorized_keys \] || mkdir -p /root/.ssh/ \r"            

send "\[ -f /root/.ssh/authorized_keys \] || mv /tmp/tmp.txt /root/.ssh/authorized_keys\r"            

expect "]*"

send "chmod 700 /root/.ssh; chmod 600 /root/.ssh/authorized_keys\r"

expect "]*"

send "exit\r"

EOF

check_ok

/usr/bin/expect /usr/local/sbin/rsync_keys/rsync.expect $ip $password

echo "OK,this script is successful. ssh $ip  to test it"



- END -

出品 | 阿铭linux

shell系统171115-部署mysql主从

加私人微信:81677956  获取免费学习资料

shell系统171115-部署mysql主从

提升自己,才是世界上最稳健的投资

shell系统171115-部署mysql主从