之前做主从复制实验的时候写的,有些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 "已完成"