Python执行show slave status输出的两个格式

1、元组的方式

输出格式如下:

('Waiting for master to send event', '10.75.19.79', 'mysqlsync', 5580L, 60L, 'mysql-bin.000050', 300947174L, 'relay-bin.000054', 210L, 'mysql-bin.000050', 'Yes', 'Yes', '', '', '', '', '', '', 0L, '', 0L, 300947174L, 463L, 'None', '', 0L, 'No', '', '', '', '', '', 0L, 'No', 0L, '', 0L, '', '', 28585580L)

对应的python脚本:

        def get_all_slave_lag(self):

                for slave in self.slaves_list:
                        conn=MySQLdb.connect(host=slave,user=self.user,passwd=self.password,port=self.port,db=self.db)
                        cursor=conn.cursor()

                        cursor.execute("show slave status")

                        result=cursor.fetchall()

                        for row in result:
                                print row
                                self.lags.append(row[32])

                        cursor.close()
                        conn.close()

有个问题:

  例如获取second_behind_master的值时候,需要考虑对应的顺序,例如5.5版本,对应的顺序为32,可能其他版本的就不一样,需要考虑版本信息。

2、字典模式输出

输出格式如下:

{'Replicate_Wild_Do_Table': '', 'Master_SSL_CA_Path': '', 'Last_Error': '', 'Until_Log_File': '', 'Seconds_Behind_Master': 0L, 'Master_User': 'mysqlsync', 'Master_Port': 5580L, 'Until_Log_Pos': 0L, 'Master_Log_File': 'mysql-bin.000050', 'Read_Master_Log_Pos': 300947174L, 'Replicate_Do_DB': '', 'Master_SSL_Verify_Server_Cert': 'No', 'Exec_Master_Log_Pos': 300947174L, 'Replicate_Ignore_Server_Ids': '', 'Replicate_Ignore_Table': '', 'Master_Server_Id': 28585580L, 'Relay_Log_Space': 463L, 'Last_SQL_Error': '', 'Relay_Master_Log_File': 'mysql-bin.000050', 'Master_SSL_Allowed': 'No', 'Master_SSL_CA_File': '', 'Slave_IO_State': 'Waiting for master to send event', 'Relay_Log_File': 'relay-bin.000054', 'Replicate_Ignore_DB': '', 'Last_IO_Error': '', 'Until_Condition': 'None', 'Replicate_Do_Table': '', 'Last_Errno': 0L, 'Master_Host': '10.75.19.79', 'Master_SSL_Key': '', 'Skip_Counter': 0L, 'Slave_SQL_Running': 'Yes', 'Relay_Log_Pos': 210L, 'Master_SSL_Cert': '', 'Last_IO_Errno': 0L, 'Slave_IO_Running': 'Yes', 'Connect_Retry': 60L, 'Last_SQL_Errno': 0L, 'Replicate_Wild_Ignore_Table': '', 'Master_SSL_Cipher': ''}

对应代码如下:

        def get_all_slave_lag(self):

                for slave in self.slaves_list:
                        conn=MySQLdb.connect(host=slave,user=self.user,passwd=self.password,port=self.port,db=self.db)
                      cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

                        cursor.execute("show slave status")

                        result=cursor.fetchall()

                        for row in result:
                                print row
                                self.lags.append(row['Seconds_Behind_Master'])

                        cursor.close()
                        conn.close()

这样比较方便且通用,不用考虑格式问题。

本例子是对应show slave status是这样使用,对于show status以及show variables 也一样处理

原文地址:https://www.cnblogs.com/gsblog/p/3313325.html