使用python腳本部署mariadb主從架構(gòu)
時間:2020-07-15 14:57:09
手機看文章
掃描二維碼
隨時隨地手機看文章
[導讀]原創(chuàng)地址:https://www.cnblogs.com/zzzynx/p/11125892.html 原創(chuàng)作者:四次元豬肉 環(huán)境準備 一個腳本自動部署master服務 另一個部署slave服務 關閉主從節(jié)點的防火墻 以及事先設置好root遠程登陸的權限。? grant all on *.* to root@'%' identified by 'root'
原創(chuàng)地址:https://www.cnblogs.com/zzzynx/p/11125892.html
原創(chuàng)作者:四次元豬肉
環(huán)境準備
grant all on *.* to root@'%' identified by 'root' ;
master
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.253.168',port=22,username='root',password='root')
a="sed -i -e '12aserver_id=1' -e '13alog_bin=mysql_bin' /etc/my.cnf.d/server.cnf"
b= 'systemctl restart mariadb'
c='''mysql -uroot -proot -e "grant replication slave on *.* to 'slave'@'%' identified by 'slave'"'''
d='''mysql -uroot -proot -e "show master status" '''
當然也可以將abcd四條命令寫入一個列表,使用for語句循環(huán)出來放入下面命令執(zhí)行。
stdin,stderr,stdout=ssh.exec_command(d) #將abcd分別執(zhí)行
res = stdout.read().decode('utf-8') + stderr.read().decode('utf-8')
print(res)
運行顯示結(jié)果:
sed -i -e '12aserver_id=1' -e '13alog_bin=mysql_bin' /etc/my.cnf.d/server.cnf
systemctl restart mariadb
mysql -uroot -p1 -e "grant replication slave on *.* to 'slave'@'%' identified by 'slave'"
mysql -uroot -p1 -e "show master status"
File Position Binlog_Do_DB Binlog_Ignore_DB
mysql_bin.000012 887350
mysql -uroot -p1 -e
此命令可以使用paramiko模塊直接執(zhí)行sql語句。e是edit的意思。
slave
Slave_IO_Running: No
Slave_SQL_Running: No
或者一個YES一個connecting的話,這是因為主節(jié)點的master狀態(tài)發(fā)生了變化或者兩臺主機的某一臺防火墻沒有關閉。
master_ip ='192.168.253.168'
log_file='mysql_bin.000012'
pos=887350
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.253.167',port=22,username='root',password='root')
a="sed -i '12aserver_id=2' /etc/my.cnf.d/server.cnf"
b='systemctl restart mariadb'
c='''mysql -uroot -proot -e "CHANGE MASTER TO MASTER_HOST='%s', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s" '''%(master_ip,log_file,pos)
d="mysql -uroot -proot -e 'start slave'"
stdin,stderr,stdout=ssh.exec_command(d) #將abcd分別執(zhí)行
res = stdout.read().decode('utf-8')+ stderr.read().decode('utf-8')
print(res)
如果報錯:The server is not configured as slave; fix in config file or with CHANGE MASTER TO
mysql -uroot -p1 -e "CHANGE MASTER TO MASTER_HOST='%s', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s" ''' % (master_ip,log_file,pos)
推薦閱讀
免責聲明:本文內(nèi)容由21ic獲得授權后發(fā)布,版權歸原作者所有,本平臺僅提供信息存儲服務。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!