Tuesday, January 27, 2015

MySQL: Kill sleeping connections using script

Kill Sleep Queries

It’s all too common for me to come across a box that is hopelessly overloaded with Apache requests and MySQL queries. Sometimes, it takes some brute force to keep the whole box from going under. When all else fails, I use the following one-liner to kill off all of those queries that have been running for too long.

mysql> select concat('KILL ',id,';') from information_schema.processlist where Command =    'Sleep'   and Time > 60 into outfile '/tmp/sleep_processes.txt'"
mysql> source /tmp/sleep_processes.txt

Slave Type Conversions - ER SLAVE CONVERSION FAILED

Replication with Differing Table Definitions on Master and Slave

Today, I have increased the size column varchar(5000) to varchar(10000) on slave for table.

alter table CASE_ACTIVITY modify remarks VARCHAR(10000)  DEFAULT NULL;

And, start getting below error and replication is stoped,

[ERROR] Slave SQL: Column 6 of table 'db.slave' cannot be converted from type 'varchar(5000)' to type 'varchar(10000)', Error_code: 1677


MySQL error code 1677 (ER SLAVE CONVERSION FAILED): Columns of table '%-.192s.%-.192s' cannot be converted from type '%-.32s' to type '%-.32s'

As a solution I changes slave_type_conversions

show global variables like '%slave_type_conversions%';

MySQL>  set global slave_type_conversions='ALL_NON_LOSSY'

And, it worked :)

Controlling what conversions are allowed is controlled with a new server variable SLAVE_TYPE_CONVERSIONS, which is of the type SET('ALL_LOSSY','ALL_NON_LOSSY'), that is, it is a set of allowed conversions. The default for this variable is the empty set, meaning that no conversions are allowed at all.

If the ALL_NON_LOSSY constant is in the set, all conversions (within each group) that do not lose any information are allowed. For example, replicating from CHAR(32) to TINYTEXT is allowed since the conversion goes to a wider field (even if it is a different type).
If the ALL_LOSSY constant is in the set, all conversions (again, within the same group) that could potentially lose information is allowed. For example, conversion to a narrower field on the slave, such as CHAR(32) to CHAR(16) is allowed. Note that non-lossy conversions are not automatically allowed when ALL_LOSSY is set.

Tuesday, January 13, 2015

MySQL Slave relay log corrupted

There is issues like “Relay log read failure: Could not parse relay log event entry” many times on MySQLslave node. Most of time in our cases, it’s due to Linux Server crashed/restarted unexpectedly.

The detail error is like this:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 146.23.1.34
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.002032
          Read_Master_Log_Pos: 537558834
               Relay_Log_File: mysql-relay-bin.003601
                Relay_Log_Pos: 461487606
        Relay_Master_Log_File: mysql-bin.002032
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1594
                   Last_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 461487460
              Relay_Log_Space: 537764119
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1594
               Last_SQL_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 120
1 row in set (0.00 sec)


In this case, we can check if the bin log at master node is still available.
   
Relay_Master_Log_File: mysql-bin.002032
...

Exec_Master_Log_Pos: 461487460

If it’s still available at master node, we can reset slave to that point, and let it recover by itself as follows:

Be careful, using Relay_Master_Log_File value in “show slave status\G” for master_log_file, not Master_Log_File value. They’re the same in this case.


mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_log_file='mysqlbin.002032',master_log_pos=461487460;
Query OK, 0 rows affected (0.30 sec)

Check slave status again and again, make sure Exec_Master_Log_Pos is increasing. Seconds_Behind_Master should be decreasing. If master is too busy, the delay may still increase, but it’ll catch up sooner or later.

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Queueing master event to the relay log
                  Master_Host: 146.23.1.34
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.002032
          Read_Master_Log_Pos: 496939207
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 56472
        Relay_Master_Log_File: mysql-bin.002032
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 461543679
              Relay_Log_Space: 35452156
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 2782
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 120
1 row in set (0.00 sec)

Optimizing the operating system (OS) for MySQL server

 Optimizing the operating system (OS) for MySQL server Optimizing the operating system (OS) for a MySQL server involves configuring various ...