本脚本主要解决批量查看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_____###################nn" % 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 "
mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollationsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

mysqloffersechar,varchar,text,and denumforstringdata.usecharforfixed Lengttrings,varcharerforvariable长度,文本forlarger文本,andenumforenforcingDataAntegrityWithaEtofValues。

优化MySQLBLOB请求可以通过以下策略:1.减少BLOB查询频率,使用独立请求或延迟加载;2.选择合适的BLOB类型(如TINYBLOB);3.将BLOB数据分离到单独表中;4.在应用层压缩BLOB数据;5.对BLOB元数据建立索引。这些方法结合实际应用中的监控、缓存和数据分片,可以有效提升性能。

掌握添加MySQL用户的方法对于数据库管理员和开发者至关重要,因为它确保数据库的安全性和访问控制。1)使用CREATEUSER命令创建新用户,2)通过GRANT命令分配权限,3)使用FLUSHPRIVILEGES确保权限生效,4)定期审计和清理用户账户以维护性能和安全。

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

在MySQL中处理字符串数据类型和索引的最佳实践包括:1)选择合适的字符串类型,如CHAR用于固定长度,VARCHAR用于可变长度,TEXT用于大文本;2)谨慎索引,避免过度索引,针对常用查询创建索引;3)使用前缀索引和全文索引优化长字符串搜索;4)定期监控和优化索引,保持索引小巧高效。通过这些方法,可以在读取和写入性能之间取得平衡,提升数据库效率。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版
好用的JavaScript开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1
好用且免费的代码编辑器