加入收藏 | 设为首页 | 会员中心 | 我要投稿 武汉站长网 (https://www.027zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

如何处理mysql主从复制中产生了锁的问题

发布时间:2021-12-23 12:03:56 所属栏目:MySql教程 来源:互联网
导读:这篇文章给大家分享的是有关如何解决mysql主从复制中产生了锁的问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。 一套主主复制的mysql库产生了死锁,导致主从同步出现问题, 解决办法:是先kill掉了产生锁等待的那个服务器
这篇文章给大家分享的是有关如何解决mysql主从复制中产生了锁的问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
 
一套主主复制的mysql库产生了死锁,导致主从同步出现问题,
 
解决办法:是先kill掉了产生锁等待的那个服务器的mysql的sql进程,然后start slave,就好了。
 
下面具体分析下相关知识点以及解决的过程:
 
一:mysql主从复制的流程,以及相关的进程。
 
1)关于复制的步骤:
 
整体上来说,复制有3个步骤:   
 
        (1)    master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
 
        (2)    slave将master的binary log events拷贝到它的中继日志(relay log);
 
        (3)    slave重做中继日志中的事件,将改变反映它自己的数据。
   
2)关于复制的进程:
 
MySQL 使用3个线程来执行复制功能,其中1个在主服务器上,另两个在从服务器上。当发出START SLAVE时,从服务器创建一个I/O线程,以连接主服务器并让它发送记录在其二进制日志中的语句。
 
主服务器创建一个线程将二进制日志中的内容发送到从服务器。该线程可以识别为主服务器上SHOW PROCESSLIST的输出中的Binlog Dump线程。
 
从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。   
 
第3个线程是SQL线程,是从服务器创建用于读取中继日志并执行日志中包含的更新。
 
有多个从服务器的主服务器创建为每个当前连接的从服务器创建一个线程;每个从服务器有自己的I/O和SQL线程。
 
3)如何找到具体的sql和io进程的id号。
 
system user对应着从库的sql和io进程,因为我们的是主主复制,所以既有主服务器Binlog Dump线程,还有从库的sql和io进程,具体如下:
 
1)我们看到Waiting for master to send event的字样,说明这个进程是从库的io进程,因为从服务器I/O线程就是读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件
 
2)我们看到Slave has read all relay log; waiting for the slave I/O thread to update it,说明这个进程是从库的sql进程,因为从库SQL线程,就是读取中继日志并执行日志中包含的更新。
 
3)我们看到Master has sent all binlog to slave; waiting for binlog to be updated,说明这个进程是主库的Binlog Dump,因为主库的Binlog Dump就是将二进制日志中的内容(binlog)发送到从服务器.并且主库的这个进程一般是由主库新建的特定的用户来完成的,我们这里是info_syncer  用户。
 
mysql> show processlist;  
 
| 13910311 | system user  |                     | NULL          | Connect     |   -1650 | Slave has read all relay log; waiting for the slave I/O thread to update it
 
| 13418520 | system user  |                     | NULL          | Connect     | 3638625 | Waiting for master to send event     
 
| 13409506 | info_syncer  | 192.168.0.243:36191 | NULL          | Binlog Dump | 3690795 | Master has sent all binlog to slave; waiting for binlog to be updated
 
二:关于mysql死锁的查询和解决办法:
 
1)查询相关的锁:
 
在5.5中,information_schema 库中增加了三个关于锁的表(MEMORY引擎):
 
innodb_trx         ## 当前运行的所有事务
 
innodb_locks       ## 当前出现的锁
 
innodb_lock_waits  ## 锁等待的对应关系
 
 
10 rows in set (0.00 sec)
 
root@127.0.0.1 : information_schema 13:28:56> desc innodb_lock_waits;
 
+——————-+————-+——+—–+———+——-+
 
| Field             | Type        | Null | Key | Default | Extra |
 
+——————-+————-+——+—–+———+——-+
 
| requesting_trx_id | varchar(18) | NO   |     |         |       |#请求锁的事务ID(也就是等待锁的id)
 
| requested_lock_id | varchar(81) | NO   |     |         |       |#请求锁的锁ID
 
| blocking_trx_id   | varchar(18) | NO   |     |         |       |#当前拥有锁的事务ID
 
| blocking_lock_id  | varchar(81) | NO   |     |         |       |#当前拥有锁的锁ID
 
+——————-+————-+——+—–+———+——-+
 
4 rows in set (0.00 sec)
 
root@127.0.0.1 : information_schema 13:29:05> desc innodb_trx ;
 
+—————————-+———————+——+—–+———————+——-+
 
| Field                      | Type                | Null | Key | Default             | Extra |
 
+—————————-+———————+——+—–+———————+——-+
 
| trx_id                     | varchar(18)         | NO   |     |                     |       |#事务ID
 
| trx_state                  | varchar(13)         | NO   |     |                     |       |#事务状态:
 
| trx_started                | datetime            | NO   |     | 0000-00-00 00:00:00 |       |#事务开始时间;
 
| trx_requested_lock_id      | varchar(81)         | YES  |     | NULL                |       |#innodb_locks.lock_id
 
| trx_wait_started           | datetime            | YES  |     | NULL                |       |#事务开始等待的时间
 
| trx_weight                 | bigint(21) unsigned | NO   |     | 0                   |       |#
 
| trx_mysql_thread_id        | bigint(21) unsigned | NO   |     | 0                   |       |#事务线程ID
 
| trx_query                  | varchar(1024)       | YES  |     | NULL                |       |#具体SQL语句
 
| trx_operation_state        | varchar(64)         | YES  |     | NULL                |       |#事务当前操作状态
 
| trx_tables_in_use          | bigint(21) unsigned | NO   |     | 0                   |       |#事务中有多少个表被使用
 
| trx_tables_locked          | bigint(21) unsigned | NO   |     | 0                   |       |#事务拥有多少个锁
 
| trx_lock_structs           | bigint(21) unsigned | NO   |     | 0                   |       |#
 
| trx_lock_memory_bytes      | bigint(21) unsigned | NO   |     | 0                   |       |#事务锁住的内存大小(B)
 
| trx_rows_locked            | bigint(21) unsigned | NO   |     | 0                   |       |#事务锁住的行数
 
| trx_rows_modified          | bigint(21) unsigned | NO   |     | 0                   |       |#事务更改的行数
 
| trx_concurrency_tickets    | bigint(21) unsigned | NO   |     | 0                   |       |#事务并发票数
 
| trx_isolation_level        | varchar(16)         | NO   |     |                     |       |#事务隔离级别
 
| trx_unique_checks          | int(1)              | NO   |     | 0                   |       |#是否唯一性检查
 
| trx_foreign_key_checks     | int(1)              | NO   |     | 0                   |       |#是否外键检查
 
| trx_last_foreign_key_error | varchar(256)        | YES  |     | NULL                |       |#最后的外键错误
 
| trx_adaptive_hash_latched  | int(1)              | NO   |     | 0                   |       |#
 
| trx_adaptive_hash_timeout  | bigint(21) unsigned | NO   |     | 0                   |       |#
 
+—————————-+———————+——+—–+———————+——-+
 
22 rows in set (0.01 sec)
 
mysql> show processlist;    ##可以看出来,
 
或者:
 
mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root';
 
+------------------------+
 
| concat('KILL ',id,';') |
 
+------------------------+
 
| KILL 3101;             |
 
| KILL 2946;             |
 
+------------------------+
 
2 rows in set (0.00 sec)
 
 批量kill多个进程。
 
mysql>select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';
 
Query OK, 2 rows affected (0.00 sec)
 
感谢各位的阅读!

(编辑:武汉站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读