批量查看mysql多从状态和修改多从主库指向
本脚本主要解决批量查看mysql多从状态和修改多从主库指向,并打印出执行结果。适用于主库没有做高可用或是做高可用但是V-IP没有漂移到新的主库上的问题。代码如
本脚本主要解决批量查看mysql多从状态和修改多从主库指向,,并打印出执行结果。适用于主库没有做高可用或是做高可用但是V-IP没有漂移到新的主库上的问题。代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- import MySQLdb,sys,os,threading,time user = 'root' passwd = '1q2w3e4r' #mysql执行change master命令的用户名和密码 def log_w(text):#写日志 logfile = "slave_res.txt" f = open(logfile,'a+') f.write(text) f.close() def db_conn(host,res,flag): text = "###################_____%s_____###################\n\n" % host try: conn = MySQLdb.connect(host = host,port = 6006,user = user,passwd = passwd,charset="utf8",connect_timeout = 5) cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) sql = '''show slave status''' cursor.execute(sql)#查看当前同步信息 alldata = cursor.fetchall() if len(alldata) != 0:#如果没有同步信息则抛错,退出 if alldata[0]['Master_Log_File']==alldata[0]['Relay_Master_Log_File'] and alldata[0]['Read_Master_Log_Pos']==alldata[0]['Exec_Master_Log_Pos']: text = text + "OK" + '\t' + 'Master_Host:' + str(alldata[0]['Master_Host']) + ' ' + str(alldata[0]['Master_Log_File']) + ' ' + str(alldata[0]['Relay_Master_Log_File']) + ' ' + str(alldata[0]['Read_Master_Log_Pos']) + ' ' + str(alldata[0]['Exec_Master_Log_Pos']) + ' ' + str(alldata[0]['Seconds_Behind_Master'])+'\n' if flag == '1': try: sql = "stop slave;" cursor.execute(sql)#停止从库同步 except Exception, e: pass sql = '''change master to master_host='192.10.100.100',master_user='rep_slave',master_password='rEeMAKEreplication6210',master_port=6006,master_log_file='mysql-bin.000100',master_log_pos=300;''' cursor.execute(sql)#执行change master语句 sql = "start slave;" cursor.execute(sql)#开启同步 sql = 'show slave status' cursor.execute(sql)#查看最新的同步信息 alldata = cursor.fetchall() if (alldata[0]['Slave_IO_Running'] == 'Yes') and (alldata[0]['Slave_SQL_Running'] == 'Yes'): text = text + "OK" + '\t' + 'Master_Host:' + str(alldata[0]['Master_Host']) + ' ' + str(alldata[0]['Master_Log_File']) + ' ' + str(alldata[0]['Relay_Master_Log_File']) + ' ' + str(alldata[0]['Read_Master_Log_Pos']) + ' ' + str(alldata[0]['Exec_Master_Log_Pos']) + ' ' + str(alldata[0]['Seconds_Behind_Master'])+'\n' else: text = text + "Start Slave Error" + '\t' + 'Master_Host:' + str(alldata[0]['Master_Host']) + '\t' + 'Slave_IO_Running: '+str(alldata[0]['Slave_IO_Running']) + '\t' + 'Slave_SQL_Running:' + str(alldata[0]['Slave_SQL_Running']) + '\n' else: text = text + "Slave Error" + ' ' + 'Master_Host:' + str(alldata[0]['Master_Host']) + ' ' + str(alldata[0]['Master_Log_File']) + ' ' + str(alldata[0]['Relay_Master_Log_File']) + ' ' + str(alldata[0]['Read_Master_Log_Pos']) + ' ' + str(alldata[0]['Exec_Master_Log_Pos']) + str(alldata[0]['Seconds_Behind_Master'])+'\n' else: text = text + "Error,This host not set slave information" cursor.close() conn.close() except Exception, e: text = text + "Error" + '\t' + str(e) res.append(text) def start(flag): threads = [] res = [] host_list = ['192.168.1.114','192.168.1.120'] for host in host_list: t = threading.Thread(target=db_conn,args=(host,res,flag)) t.setDaemon(True) threads.append(t) for i in range(len(threads)): threads[i].start() time.sleep(0.1) for i in range(len(threads)): threads[i].join() for i in res: if "Error" in i: print "\033[1;31;40m%s\033[0m" % i else: print i log_w(i) if flag == '1': text = "\nChange master finished" print text log_w(text) else: text = "\nSHOW SLAVE STATUS complete" print text log_w(text) text = "\n\n################### %s ###################\n\n" % time.strftime("%Y-%m-%d %H:%M:%S") print text log_w(text) def main(): print print "请选择操作类型:\n\n0:查看所有从库的同步状态\n1:改变所有从库的主库指向\n"#.decode("utf-8").encode("GBK") for i in range(3): choose = raw_input('Your choose : ') if choose == '0' or choose == '1': start(choose) break else: print "Error,please Enter right noumber again ." print if __name__=='__main__': main()
熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

MySQL和phpMyAdmin是強大的數據庫管理工具。 1)MySQL用於創建數據庫和表、執行DML和SQL查詢。 2)phpMyAdmin提供直觀界面進行數據庫管理、表結構管理、數據操作和用戶權限管理。

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。MySQL以其高性能、可扩展性和跨平台支持著称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

在開發一個小型應用時,我遇到了一個棘手的問題:需要快速集成一個輕量級的數據庫操作庫。嘗試了多個庫後,我發現它們要么功能過多,要么兼容性不佳。最終,我找到了minii/db,這是一個基於Yii2的簡化版本,完美地解決了我的問題。

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

MySQL的基本操作包括創建數據庫、表格,及使用SQL進行數據的CRUD操作。 1.創建數據庫:CREATEDATABASEmy_first_db;2.創建表格:CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY,titleVARCHAR(100)NOTNULL,authorVARCHAR(100)NOTNULL,published_yearINT);3.插入數據:INSERTINTObooks(title,author,published_year)VA

在使用Thelia開發電商網站時,我遇到了一個棘手的問題:MySQL模式設置不當,導致某些功能無法正常運行。經過一番探索,我找到了一個名為TheliaMySQLModesChecker的模塊,它能夠自動修復Thelia所需的MySQL模式,徹底解決了我的困擾。
