shell指令碼 mysql主從

2022-09-14 08:15:12 字數 2190 閱讀 9901

#!/bin/bash

systemctl stop firewalld

setenforce

0user="

tom"

password="

123"

sl**e_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\]$/a\server-id=1

' /etc/my.cnf #在配置檔案my.cnf新增

sed -i '

/^\[mysqld\]$/a\log-bin=mysql-bin

' /etc/my.cnf #在配置檔案my.cnf新增

sed -i '

/^\[mysqld\]$/a\relay-log=mysql-relay

' /etc/my.cnf #在配置檔案my.cnf新增

systemctl restart mariadb #啟動mysql

mysql -e "

grant all on *.* to '$user'to'$sl**e_ipaddr' identified by '$password';

"#給使用者授權

master_status=`mysql -e "

show master status;"`

echo

"$master_status

"#列印master_status

master_file=`echo "

$master_status

" | grep "

bin" | awk ''`

echo

"$master_file

"master_pos=`echo "

$master_status

" | grep "

bin" | awk ''`

echo

"$master_pos

"ssh root@$sl**e_ipaddr

2>&1

setenforce

0yum -y install mariadb mariadb-server

sed -i '

/^\[mysqld\]$/a\server-id=2

' /etc/my.cnf

sed -i '

/^\[mysqld\]$/a\log-bin=mysql-bin

' /etc/my.cnf

sed -i '

/^\[mysqld\]$/a\relay-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 sl**e;

"mysql -e "

show sl**e status \g;

"check=`mysql -e "

show sl**e status \g;

" | grep yes | wc -l`

if [ $check == 1

]; then

echo

"僅輸出乙個yes

"elif [ $check == 2

]; then

echo

"輸出倆個yes

"else

echo

"mariadb錯誤"fi

eof

Shell指令碼實戰 DNS主從同步指令碼例項

ps 兩個伺服器起好後最好兩個服務都重啟一下 bin bash dns主從同步 主伺服器 rpm q bindif ne 0 then yum install bind y systemctl start named fi 判斷dns服務是否安裝 read p 網域名稱 請帶字尾 yu quanju...

編寫MYSQL主從指令碼 centos6

主從centos7鏈結 執行之前配好ssh免密 和關閉防火牆 vim aa.sh bin bash yum y install mysql mysql server 主安裝mysql sed i 0 a server id 1 etc my.cnf sed修改主配置 sed i 1 a log bi...

mysql備份shell指令碼

bin bash mysql server login info muser root mpass root mhost localhost mysql which mysql mysqldump which mysqldump dbname test bak backup mysql gzip w...