部署MySQL主从、管理docker、安装配置samba、批量查看堕胎机器负载、自动挂云盘

本文详细介绍了一种在Linux环境下通过Shell脚本自动化部署MySQL主从复制环境的方法,包括配置主从服务器、传输配置脚本、远程执行命令及解锁表等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

#!/bin/bash
master_ip=192.168.100.12
slave_ip=192.168.100.13
mysqlc="mysql -uroot -paminglinux"

check_ok()
{
    if [ $? -ne 0 ]
    then
        echo "$1 出错了。"
        exit 1
    fi
}

f_exist()
{
    d=`date +%F%T`
    if [ -f $1 ]
    then
        mv $1 $1_$d
    fi
}

## 设置主mysql配置
if ! grep '^server-id' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\server-id = 1001' /etc/my.cnf
fi

if ! grep '^log-bin.*=.*' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\log-bin = aminglinux' /etc/my.cnf
fi

sed -i '/^log-bin.*/a\binlog-ignore-db = mysql ' /etc/my.cnf

/etc/init.d/mysqld restart
check_ok "主上重启mysql"

## 登录mysql,授权用户、锁表以及show master status。
$mysqlc <<EOF
    grant replication slave on *.* to 'repl'@$slave_ip identified by 'yourpassword';
    flush tables with read lock;
EOF
$mysqlc -e "show master status" > /tmp/master.log
file=`tail -1 /tmp/master.log|awk '{print $1}'`
pos=`tail -1 /tmp/master.log|awk '{print $2}'`

## 创建在从上配置和操作的脚本
f_exist /tmp/slave.sh

cat > /tmp/slave.sh << EOF
#!/bin/bash
if ! grep '^server-id' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\server-id = 1002' /etc/my.cnf
fi

/etc/init.d/mysqld restart
check_ok "从上重启mysql"

$mysqlc  <<EOF
    stop slave;
    change master to master_host="$master_ip", master_user="repl", master_password="yourpassword", master_log_file="$file", master_log_pos=$pos;
    start slave;
EOF 
EOF

## 创建传输slave.sh的expect脚本
f_exist /tmp/rs_slave.expect

cat > /tmp/rs_slave.expect <<EOF
#!/usr/bin/expect
set passwd "aminglinux"
spawn rsync -a /tmp/slave.sh root@$slave_ip:/tmp/slave.sh
expect {
    "yes/no" { send "yes\r"}
    "password:" { send "\$passwd\r" }
}
expect eof
EOF

## 执行expect脚本
chmod +x /tmp/rs_slave.expect
/tmp/rs_slave.expect
check_ok "传输slave.sh"

## 创建远程执行命令的expect脚本
f_exist /tmp/exe.expect

cat > /tmp/exe.expect <<EOF
#!/usr/bin/expect
set passwd "aminglinux"
spawn ssh root@$slave_ip
expect {
    "yes/no" { send "yes\r"}
    "password:" { send "\$passwd\r" }
}
expect "]*"
send "/bin/bash /tmp/slave.sh\r"
expect "]*"
send "exit\r"
EOF

## 执行expect脚本
chmod +x /tmp/exe.expect
/tmp/exe.expect
check_ok "远程执行slave.sh"

## 主上解锁表
$mysqlc -e "unlock tables"

在这里插入图片描述

while true
do
    read -p "请输入你要执行的操作:(stop/start/rm) " opt
    if [ -z "$opt" ]
    then
        echo "请输入要执行的操作。"
        continue
    else
        break
    fi
done

docker ps -a |awk '{print $1}' > /tmp/id.txt
case $opt in
  stop)
    for id in `cat /tmp/id.txt`
    do
        docker stop $id
    done
    ;;
  start)
    for id in `cat /tmp/id.txt`
    do
        docker start $id
    done
  rm)
    for id in `cat /tmp/id.txt`
    do
        read -p "将要删除容器$id,是否继续?(y|n)" c
        case $c in
          y|Y)
            docker rm -f $id
            ;;
          n|N)
            echo "容器$id不会被删除。"
            ;;
          *)
            echo "你只能输入'y'或者'n'。"
            ;;
        esac
    done
  *)
    echo "你只能输入start/stop/rm。"
    ;;
esac

在这里插入图片描述

if [ "$#" -ne 1 ]
then
    echo "运行脚本的格式为:$0 /dir/"
    exit 1
else
    if ! echo $1 |grep -q '^/.*'
    then
        echo "请提供一个绝对路径。"
        exit 1
    fi
fi

if ! rpm -q samba >/dev/null
then
    echo "将要安装samba"
    sleep 1
    yum install -y samba
    if [ $? -ne 0 ]
    then
        echo "samba安装失败"
        exit 1
    fi
fi

cnfdir="/etc/samba/smb.conf"
cat >> $cnfdir <<EOF
[share]
        comment = share all
        path = $1
        browseable = yes
        public = yes
        writable = no
EOF

if [ ! -d $1 ]
then
    mkdir -p $1
fi

chmod 777 $1
echo "test" > $1/test.txt

#假设系统为CentOS7
systemctl start smb
if [ $? -ne 0 ]
then
    echo "samba服务启动失败,请检查配置文件是否正确。"
else
    echo "samba配置完毕,请验证。"
fi

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值