strace 命令分析mysql的相关的问题

strace 命令分析mysql的相关的问题

使用方式: strace -p pid or strace command

本篇博文主要简单介绍下面关于MySQL的5个问题:

利用strace观察mysqld对my.cnf 配置文件的加载顺序
MySQL启动后默认会启动多少线程
如何匹配conn_id和os_thread_id
利用strace观察client的SQL执行
利用strace观察server端执行

1.strace观察mysqld对my.cnf 配置文件的加载顺序

命令如下:

strace -T -tt -s 100 -o start.log /usr/local/mysql/bin/mysqld

[root@mgr04 ~]#  cat -n start.log |sed -n ‘152,165p‘
   152  03:43:41.399411 futex(0x7fb060f058b8, FUTEX_WAKE_PRIVATE, 2147483647) = 0 <0.000013>
   153  03:43:41.400839 stat("/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=11064, ...}) = 0 <0.000015>
   154  03:43:41.400951 open("/etc/my.cnf", O_RDONLY) = 3 <0.000024>
   155  03:43:41.401026 fstat(3, {st_mode=S_IFREG|0644, st_size=11064, ...}) = 0 <0.000009>
   156  03:43:41.401068 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb061d94000 <0.000016>
   157  03:43:41.401116 read(3, "[client]\nport            = 3306\n\n[mysql]\nauto-rehash\nprompt=\"\\\\u@\\\\h [\\\\d]>\"\n#pager=\"less -i -n -S\"\n"..., 4096) = 4096 <0.000016>
   158  03:43:41.401211 read(3, " #    0\n#rpl_semi_sync_master_timeout        =1000                          #    1000(1 second)\n#plu"..., 4096) = 4096 <0.000011>
   159  03:43:41.401303 read(3, "       #\ton\ninnodb_change_buffering                         =all                        #\tall\ninnodb"..., 4096) = 2872 <0.000011>
   160  03:43:41.401376 read(3, "", 4096)       = 0 <0.000010>
   161  03:43:41.401423 close(3)                = 0 <0.000052>
   162  03:43:41.401564 munmap(0x7fb061d94000, 4096) = 0 <0.000080>
   163  03:43:41.401777 stat("/etc/mysql/my.cnf", 0x7fff5ec1c170) = -1 ENOENT (No such file or directory) <0.000022>
   164  03:43:41.402070 stat("/usr/local/mysql/etc/my.cnf", 0x7fff5ec1c170) = -1 ENOENT (No such file or directory) <0.000016>
   165  03:43:41.402134 stat("/root/.my.cnf", 0x7fff5ec1c170) = -1 ENOENT (No such file or directory) <0.000012>

2. 查看mysql启动的线程数

操作系统查看:
ps -T pidof mysqld

[root@mgr04 opt]# ps -T `pidof mysqld`|sed ‘1d‘
 59592  59592 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59593 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59594 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59595 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59596 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59597 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59598 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59599 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59600 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59601 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59602 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59603 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59604 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59607 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59608 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59609 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59610 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59611 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59612 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59613 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59614 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59615 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59616 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59617 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59618 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59619 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld
 59592  59620 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld

利用pstack

pstack `pidof mysql`  或者pstack `pidof mysqld` 2>&1|tee  /tmp/pstack.log

下面的这个过程特别的慢。会导致mysql夯筑,不要线上直接使用
MySQL默认的启动线程数是28

推荐MySQL 5.7以上的版本查看mysql默认启动的线程数为28个

select thread_id, name from performance_schema.threads;

select name,count(*) from  performance_schema.threads group by name;
root@localhost [(none)]>select name,count(*) from  performance_schema.threads group by name;
+----------------------------------------+----------+
| name                                   | count(*) |
+----------------------------------------+----------+
| thread/innodb/buf_dump_thread          |        1 |
| thread/innodb/dict_stats_thread        |        1 |
| thread/innodb/io_ibuf_thread           |        1 |
| thread/innodb/io_log_thread            |        1 |
| thread/innodb/io_read_thread           |        4 |
| thread/innodb/io_write_thread          |        4 |
| thread/innodb/page_cleaner_thread      |        1 |
| thread/innodb/srv_error_monitor_thread |        1 |
| thread/innodb/srv_lock_timeout_thread  |        1 |
| thread/innodb/srv_master_thread        |        1 |
| thread/innodb/srv_monitor_thread       |        1 |
| thread/innodb/srv_purge_thread         |        1 |
| thread/innodb/srv_worker_thread        |        3 |
| thread/sql/compress_gtid_table         |        1 |
| thread/sql/main                        |        1 |
| thread/sql/one_connection              |        1 |
| thread/sql/signal_handler              |        1 |
| thread/sql/thread_timer_notifier       |        1 |
+----------------------------------------+----------+
18 rows in set (0.00 sec)
root@localhost [(none)]>select thread_id, name from performance_schema.threads;
+-----------+----------------------------------------+
| thread_id | name                                   |
+-----------+----------------------------------------+
|         1 | thread/sql/main                        |
|         2 | thread/sql/thread_timer_notifier       |
|         3 | thread/innodb/io_ibuf_thread           |
|         4 | thread/innodb/io_read_thread           |
|         5 | thread/innodb/io_read_thread           |
|         6 | thread/innodb/io_read_thread           |
|         7 | thread/innodb/io_read_thread           |
|         8 | thread/innodb/io_log_thread            |
|         9 | thread/innodb/io_write_thread          |
|        10 | thread/innodb/io_write_thread          |
|        11 | thread/innodb/io_write_thread          |
|        12 | thread/innodb/io_write_thread          |
|        13 | thread/innodb/page_cleaner_thread      |
|        16 | thread/innodb/srv_lock_timeout_thread  |
|        17 | thread/innodb/srv_error_monitor_thread |
|        18 | thread/innodb/srv_monitor_thread       |
|        19 | thread/innodb/srv_master_thread        |
|        20 | thread/innodb/srv_purge_thread         |
|        21 | thread/innodb/srv_worker_thread        |
|        22 | thread/innodb/srv_worker_thread        |
|        23 | thread/innodb/srv_worker_thread        |
|        24 | thread/innodb/buf_dump_thread          |
|        25 | thread/innodb/dict_stats_thread        |
|        26 | thread/sql/signal_handler              |
|        27 | thread/sql/compress_gtid_table         |
|        29 | thread/sql/one_connection              |
+-----------+----------------------------------------+
26 rows in set (0.00 sec)

如何匹配conn_id和os_thread_id

MySQL 5.6时代采用的方法:

gdb attach  11646
strace -T -tt -s 100 -p11646

MySQL 5.7 & 8.0时代采用的方法:

可以利用sys.processlist 和performance_schema.threads进行关联

select thd_id, conn_id, thread_os_id, name from sys.processlist a ,performance_schema.threads b where a.thd_id =b.thread_id and conn_id >0;

查询报错:

 root@localhost [sys]>select thd_id, conn_id, thread_os_id, name from sys.processlist a , performance_schema.threads b where a.thd_id =b.thread_id and conn_id >0;
ERROR 1104 (42000): The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay

解决办法:

root@localhost [sys]>
root@localhost [sys]>SET SQL_BIG_SELECTS=1 ;
Query OK, 0 rows affected (0.00 sec)

root@localhost [sys]>select thd_id, conn_id, thread_os_id, name from sys.processlist a , performance_schema.threads b where a.thd_id =b.thread_id and conn_id >0;
+--------+---------+--------------+--------------------------------+
| thd_id | conn_id | thread_os_id | name                           |
+--------+---------+--------------+--------------------------------+
|     26 |       1 |        13068 | thread/sql/compress_gtid_table |
|     27 |       2 |        13071 | thread/sql/one_connection      |
+--------+---------+--------------+--------------------------------+
2 rows in set (0.07 sec)
root@localhost [sys]>

strace追踪当前连接的线程ID,来查看对系统os的操作

strace -T -tt -e trace=write,access,read,open,pwrite64,pread64 -p 13071 2>&1 |tee >/tmp/strace.log

演示:
数据库操作:

show databases;
use test01;
use tets02;
create table tutorials_tbl(
tutorial_id INT NOT NULL AUTO_INCREMENT,
 tutorial_title VARCHAR(100) NOT NULL,
 tutorial_author VARCHAR(40) NOT NULL,
submission_date DATE,
PRIMARY KEY ( tutorial_id )
 );;
insert into tutorials_tbl(tutorial_id,tutorial_title,tutorial_author,submission_date) values(‘1‘,‘作文‘,‘三毛‘,‘2019-03-10‘);
select * from tutorials_tbl;

利用strace观察client端SQL执行,也就是跟踪进程输出:

strace -T -tt -e trace=recvfrom,write,access,read,open,pwrite64,pread64 -p 13071
[root@mgr04 ~]# strace -T -tt -e trace=recvfrom,write,access,read,open,pwrite64,pread64 -p 13071
strace: Process 13071 attached

06:05:20.074087 recvfrom(59, "\22\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000138>
06:05:20.074539 recvfrom(59, "\3SELECT DATABASE()", 18, MSG_DONTWAIT, NULL, NULL) = 18 <0.000119>
06:05:20.074891 write(35, "2019-03-17T06:05:20.074757+08:00"..., 63) = 63 <0.000345>
06:05:20.075510 recvfrom(59, "\7\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000097>
06:05:20.075749 recvfrom(59, "\2test01", 7, MSG_DONTWAIT, NULL, NULL) = 7 <0.000109>
06:05:20.076098 access("./test01", F_OK) = 0 <0.000102>
06:05:20.076372 write(35, "2019-03-17T06:05:20.076273+08:00"..., 54) = 54 <0.000115>
06:05:20.076877 recvfrom(59, 0x7f2e48007990, 4, MSG_DONTWAIT, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000217>
06:05:46.672935 recvfrom(59, "\17\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000117>
06:05:46.673241 recvfrom(59, "\3show databases", 15, MSG_DONTWAIT, NULL, NULL) = 15 <0.000099>
06:05:46.673547 write(35, "2019-03-17T06:05:46.673430+08:00"..., 60) = 60 <0.000189>
06:05:46.675841 recvfrom(59, 0x7f2e48007990, 4, MSG_DONTWAIT, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000117>
06:06:05.768163 recvfrom(59, "\22\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000260>
06:06:05.768871 recvfrom(59, "\3SELECT DATABASE()", 18, MSG_DONTWAIT, NULL, NULL) = 18 <0.000376>
06:06:05.769551 write(35, "2019-03-17T06:06:05.769412+08:00"..., 63) = 63 <0.000137>
06:06:05.770088 recvfrom(59, "\7\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000130>
06:06:05.770362 recvfrom(59, "\2test02", 7, MSG_DONTWAIT, NULL, NULL) = 7 <0.000096>
06:06:05.770599 access("./test02", F_OK) = 0 <0.000114>
06:06:05.770882 write(35, "2019-03-17T06:06:05.770766+08:00"..., 54) = 54 <0.000191>
06:06:05.772482 recvfrom(59, "\17\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000167>
06:06:05.772843 recvfrom(59, "\3show databases", 15, MSG_DONTWAIT, NULL, NULL) = 15 <0.000095>
06:06:05.773231 write(35, "2019-03-17T06:06:05.773085+08:00"..., 60) = 60 <0.000115>
06:06:05.775112 recvfrom(59, "\f\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000274>
06:06:05.775585 recvfrom(59, "\3show tables", 12, MSG_DONTWAIT, NULL, NULL) = 12 <0.000100>
06:06:05.775908 write(35, "2019-03-17T06:06:05.775782+08:00"..., 57) = 57 <0.000290>
06:06:05.776987 recvfrom(59, 0x7f2e48007990, 4, MSG_DONTWAIT, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000198>
06:07:14.887093 recvfrom(59, "\324\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000016>
06:07:14.887219 recvfrom(59, "\3create table tutorials_tbl(\n   "..., 212, MSG_DONTWAIT, NULL, NULL) = 212 <0.000016>
06:07:14.887395 write(35, "2019-03-17T06:07:14.887364+08:00"..., 257) = 257 <0.000050>
06:07:14.887562 access("./test02/tutorials_tbl.frm", F_OK) = -1 ENOENT (No such file or directory) <0.000306>
06:07:14.888180 access("./test02/tutorials_tbl.frm", F_OK) = -1 ENOENT (No such file or directory) <0.000144>
06:07:14.888411 open("./test02/tutorials_tbl.frm", O_RDWR|O_CREAT|O_TRUNC, 0640) = 83 <0.000877>
06:07:14.889915 write(83, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 4096) = 4096 <0.000226>
06:07:14.890372 write(83, "//\0", 3)    = 3 <0.000135>
06:07:14.890799 write(83, "\0 \0\0", 4) = 4 <0.000024>
06:07:14.890906 write(83, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 4096) = 4096 <0.000035>
06:07:14.890993 pwrite64(83, "\376\1\n\f\3\0\0\20\1\0\0000\0\0i\1\257\1\0\0\0\0\0\0\0\0\0\2!\0\t\0"..., 64, 0) = 64 <0.000020>
06:07:14.891073 pwrite64(83, "\1\1\0\0\n\0\0\0\4\0\1\0\0\0\1\200\2\0\0\33\0\4\0\377PRIMARY\377"..., 33, 4096) = 33 <0.000019>
06:07:14.891183 write(83, "\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 431) = 431 <0.000020>
06:07:14.891448 write(83, "\0\0", 2)    = 2 <0.000028>
06:07:14.891732 write(83, "\6\0", 2)    = 2 <0.000023>
06:07:14.891814 write(83, "InnoDB", 6)  = 6 <0.000019>
06:07:14.891881 write(83, "\0\0\0\0\0\0", 6) = 6 <0.000018>
06:07:14.891948 write(83, "\r\0\0\0\0\0\0\0\0\0\0\0\0", 13) = 13 <0.000018>
06:07:14.892015 write(83, "\0\0", 2)    = 2 <0.000019>
06:07:14.892078 write(83, "\0\0", 2)    = 2 <0.000205>
06:07:14.892358 write(83, "\30\2\0\20\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 288) = 288 <0.000039>
06:07:14.892453 write(83, "w\0\5\4\2\24)                         "..., 119) = 119 <0.000023>
06:07:14.892526 write(83, "\4\f\v\v\0\2\0\0\33\0\17\0\0\3!\0\0", 17) = 17 <0.000281>
06:07:14.892864 write(83, "\5\17,,\1\6\0\0\0@\0\0\0\17!\0\0", 17) = 17 <0.000027>
06:07:14.892949 write(83, "\6\20?x\0004\1\0\0@\0\0\0\17!\0\0", 17) = 17 <0.000022>
06:07:14.893021 write(83, "\7\20\n\n\0\255\1\0p\200\0\0\0\16\10\0\0", 17) = 17 <0.000022>
06:07:14.893093 write(83, "\377", 1)    = 1 <0.000021>
06:07:14.893162 write(83, "tutorial_id\377", 12) = 12 <0.000188>
06:07:14.893407 write(83, "tutorial_title\377", 15) = 15 <0.000027>
06:07:14.893491 write(83, "tutorial_author\377", 16) = 16 <0.000021>
06:07:14.893559 write(83, "submission_date\377\0", 17) = 17 <0.000376>
06:07:14.911739 open("./test02/", O_RDONLY) = 84 <0.000037>
06:07:14.912031 open("./test02/tutorials_tbl.frm", O_RDONLY) = 83 <0.000026>
06:07:14.912188 read(83, "\376\1\n\f\3\0\0\20\1\0\0000\0\0i\1\257\1\0\0\0\0\0\0\0\0\0\2!\0\t\0"..., 64) = 64 <0.000036>
06:07:14.912772 read(83, "//\0\0 \0\0", 7) = 7 <0.000227>
06:07:14.913170 read(83, "\30\2\0\20\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 288) = 288 <0.000018>
06:07:14.913272 read(83, "\1\1\0\0\n\0\0\0\4\0\1\0\0\0\1\200\2\0\0\33\0\4\0\377PRIMARY\377"..., 33) = 33 <0.000015>
06:07:14.913485 pread64(83, "\0\0\6\0InnoDB\0\0\0\0\0\0\r\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 33, 4888) = 33 <0.000019>
06:07:14.913579 pread64(83, "\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 431, 4457) = 431 <0.000252>
06:07:14.914061 read(83, "w\0\5\4\2\24)                         "..., 248) = 248 <0.000137>
06:07:14.914505 open("./test02/tutorials_tbl.ibd", O_RDWR|O_CREAT|O_EXCL, 0640) = 83 <0.000603>
06:07:14.915312 pwrite64(83, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 65536, 0) = 65536 <0.000124>
06:07:14.925026 pwrite64(83, "\315\331\225\240\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 16384, 0) = 16384 <0.000227>
06:07:14.929051 open("./test02/tutorials_tbl.ibd", O_RDWR) = 83 <0.000251>
06:07:14.929985 pwrite64(83, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 32768, 65536) = 32768 <0.000175>
06:07:14.932909 pwrite64(4, "\200\0\24\211\2\0\0A\0\0\0^)\242\0\0\1\25\1A\\\215q\213\0\0\0\0\0\0\0\1"..., 10752, 2684416) = 10752 <0.000223>
06:07:14.935588 pwrite64(4, "\200\0\24\220\1\262\0\356\0\0\0^\377\4-\2\1r\360\377\377\377\377\4-\2\1v\360\377\377\377"..., 512, 2688000) = 512 <0.000191>
06:07:14.938442 pwrite64(4, "\200\0\24\220\2\0\0\356\0\0\0^\377\4-\2\1r\360\377\377\377\377\4-\2\1v\360\377\377\377"..., 1536, 2688000) = 1536 <0.000213>
06:07:14.942039 pwrite64(4, "\200\0\24\222\0C\0006\0\0\0^\1r\\\215s\222\0\0\0\0\0\0\0\1Number"..., 512, 2689024) = 512 <0.000406>
06:07:14.944196 write(37, "\222s\215\\!\372\273\23\0=\0\0\0\23\r\0\0\0\0\1\232-\217\206\32\271\21\351\263x\0\f"..., 346) = 346 <0.000176>
06:07:14.955507 recvfrom(59, 0x7f2e48007990, 4, MSG_DONTWAIT, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000233>
06:10:27.191228 recvfrom(59, "\201\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000014>
06:10:27.191364 recvfrom(59, "\3insert into tutorials_tbl(tutor"..., 129, MSG_DONTWAIT, NULL, NULL) = 129 <0.000012>
06:10:27.191504 write(35, "2019-03-17T06:10:27.191478+08:00"..., 174) = 174 <0.000040>
06:10:27.191627 open("./test02/tutorials_tbl.frm", O_RDONLY) = 84 <0.000020>
06:10:27.191738 read(84, "\376\1\n\f\3\0\0\20\1\0\0000\0\0i\1\257\1\0\0\0\0\0\0\0\0\0\2!\0\t\0"..., 64) = 64 <0.000012>
06:10:27.191799 read(84, "//\0\0 \0\0", 7) = 7 <0.000010>
06:10:27.191856 read(84, "\30\2\0\20\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 288) = 288 <0.000010>
06:10:27.191913 read(84, "\1\1\0\0\n\0\0\0\4\0\1\0\0\0\1\200\2\0\0\33\0\4\0\377PRIMARY\377"..., 33) = 33 <0.000022>
06:10:27.191973 pread64(84, "\0\0\6\0InnoDB\0\0\0\0\0\0\r\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 33, 4888) = 33 <0.000010>
06:10:27.192176 pread64(84, "\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 431, 4457) = 431 <0.000012>
06:10:27.192248 read(84, "w\0\5\4\2\24)                         "..., 248) = 248 <0.000010>
06:10:27.192584 access("./test02/tutorials_tbl.TRG", F_OK) = -1 ENOENT (No such file or directory) <0.000025>
06:10:27.192854 pwrite64(4, "\200\0\24\222\2\0\0006\0\0\0`\1r\\\215s\222\0\0\0\0\0\0\0\1Number"..., 1024, 2689024) = 1024 <0.000281>
06:10:27.221914 write(37, "St\215\\!\372\273\23\0=\0\0\0m\16\0\0\0\0\0\232-\217\206\32\271\21\351\263x\0\f"..., 421) = 421 <0.000313>
06:10:27.245928 recvfrom(59, 0x7f2e48007990, 4, MSG_DONTWAIT, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000232>
submission_date 

06:20:22.957977 recvfrom(59, "\34\0\0\0", 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000016>
06:20:22.958107 recvfrom(59, "\3select * from tutorials_tbl", 28, MSG_DONTWAIT, NULL, NULL) = 28 <0.000016>
06:20:22.958258 write(35, "2019-03-17T06:20:22.958225+08:00"..., 73) = 73 <0.000052>
06:20:22.958649 recvfrom(59, 0x7f2e48007990, 4, MSG_DONTWAIT, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000014>
^Cstrace: Process 13071 detached

MySQL 5.7后可以利用sys.processlist:

select thd_id, conn_id,user, pid,program_name,command,current_statement from processlist where conn_id>0 and pid>0;
root@localhost [sys]>use sys;
Database changed
root@localhost [sys]>select thd_id, conn_id,user, pid,program_name,command,current_statement from processlist where conn_id>0 and pid>0;
+--------+---------+----------------+-------+--------------+---------+-------------------------------------------------------------------+
| thd_id | conn_id | user           | pid   | program_name | command | current_statement                                                 |
+--------+---------+----------------+-------+--------------+---------+-------------------------------------------------------------------+
|     29 |       3 | root@localhost | 59903 | mysql        | Query   | select thd_id, conn_id,user, p ... list where conn_id>0 and pid>0 |
+--------+---------+----------------+-------+--------------+---------+-------------------------------------------------------------------+
1 row in set (0.08 sec)

root@localhost [sys]>

strace -T -tt -s 100 -o /tmp/strace.log -p $PID

这里就需要看看mysqld相关的io操作:

mysql: read, write, open
 Innodb: pread64, pwrite64
strace -o /tmp/strace.log -T -tt -f -e trace=read,open,write,pwrite64,pread64 -p `pidof mysqld`
 strace -o /tmp/test.log -T -tt -ff -p `pidof mysqld`
-ff 参数,在跟踪的线程fork出新的thread时,会产生一个新文件存放

利用获取的线程id在MySQL查询,看看都是干什么的?

select thread_id, thread_os_id,name from threads where thread_os_id in (15871,15872,15874,15901);

strace命令常用简单使用方法如下:

strace -T -tt -o /tmp/strace.log CMD
strace -T -tt CMD 2>&1 |tee /tmp/strace.log
strace -T -tt -s 100 -o /tmp/strace.log CMD
strace -T -tt -s 100 -ff -o /tmp/strace.log CMD
strace -T -tt -s 100 -e trace=XXXX -o /tmp/strace.log CMD
strace -T -tt -s 100 -ff -o /root/strace.log /usr/local/mysql/bin/mysqld
strace -T -tt -s 100 -e trace=write -o /root/strace.log /usr/local/mysql/bin/mysqld

本博文是学习腾讯视屏中的课程总结于此。
内容课程地址如下:
https://ke.qq.com/course/384101

strace 故障分析MySQL案例如下:
https://ke.qq.com/course/331538 《MySQL运维之我踩过的那些坑》
https://www.percona.com/blog/2010/07/16/analyzing-the-distribution-of-innodb-log-filewrites/
https://mp.weixin.qq.com/s/wP_ivItVKbodkV1FluTcsg (杨奇?龙的公众号:系统调?用跟踪
分析神器?--strace)
https://www.actionsky.com/index.php?id=2393 (binlog rotate引发的MySQL阻塞事件)

原文地址:https://blog.51cto.com/wujianwei/2368262

时间: 2024-11-17 18:27:37

strace 命令分析mysql的相关的问题的相关文章

利用strace和perf分析MySQL运行

利用strace和perf分析MySQL运行 周万春 perf strace pstack pt-pmp 都是一类工具 [[email protected] ~]# perf top Samples: 330 of event 'cpu-clock', 4000 Hz, Event count (approx.): 42848685 lost: 0/0 drop: 0/0 Overhead Shared Object Symbol 21.09% [kernel] [k] vmw_cmdbuf_h

MySQL中使用SHOW PROFILE命令分析性能的用法整理(配合explain效果更好,可以作为优化周期性检查)

这篇文章主要介绍了MySQL中使用show profile命令分析性能的用法整理,show profiles是数据库性能优化的常用命令,需要的朋友可以参考下 show profile是由Jeremy Cole捐献给MySQL社区版本的.默认的是关闭的,但是会话级别可以开启这个功能.开启它可以让MySQL收集在执行语句的时候所使用的资源.为了统计报表,把profiling设为1 mysql> SET profiling = 1; 之后在运行一个查询 mysql> SELECT COUNT(DIS

strace命令详解

strace命令详解strace 命令是一种强大的工具,它能够显示所有由用户空间程序发出的系统调用. strace 显示这些调用的参数并返回符号形式的值.strace 从内核接收信息,而且不需要以任何特殊的方式来构建内核. 下面记录几个常用 option . 1 -f -F选项告诉strace同时跟踪fork和vfork出来的进程 2 -o xxx.txt 输出到某个文件. 3 -e execve 只记录 execve 这类系统调用 -------------------------------

mysql优化相关

一.优化概述 二.查询与索引优化分析 1性能瓶颈定位 Show命令 慢查询日志 explain分析查询 profiling分析查询 2索引及查询优化 三.配置优化 1)      max_connections 2)      back_log 3)      interactive_timeout 4)      key_buffer_size 5)      query_cache_size 6)      record_buffer_size 7)      read_rnd_buffer

xampp自带mysql命令深入分析MySQL ERROR 1045 (28000)

在命令行输入mysql -u root –p,输入密码,或通过工具连接数据库时,经常出现下面的错误信息,详细该错误信息很多人在使用MySQL时都遇到过. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 1 ** 通常从网上都能找到解决方案: ** 1.停止服务:停止MySQL服务: # windows net stop mysql # linux service mysqld

yum命令安装mysql

linux下使用yum安装mysql,以及启动.登录和远程访问. 1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查看有没有安装包: yum list mysql* 安装mysql客户端: yum install mysql 安装mysql 服务器端: yum install mysql-server yum install mysql-devel 2.启动&&停止 数据库字符集设置 mysql配置文件/etc/

grep 命令详解及相关事例

一.匹配字符 .    匹配任意单个字符 [ ]  匹配指定范围内的任意字符 [^]  匹配飞指定范围内的任意字符 [:alpha:] 字母字符 [:lower:] 小写字母字符 [:upper:] 大写字母字符 [:digit:] 数字 [:alnum:] 字母数字字符 [:space:] 空白字符(禁止打印),如回车符.换行符.竖直制表符和换页符 [:punct:] 标点字符 [:cntrl:] 控制字符(禁止打印) [:print:] 可打印字符 使用时一般使用两个中括号,具体会在下面的例

应该掌握的MySQL命令、MySQL语句

一.MySQL常用的命令: 1. 连接数据库:mysql>mysql -uroot -p回车,再输入密码   mysql -h 192.168.0.200 -P 3306 -u root -p2. mysql>status; 查看运行环境信息3. 显示所有数据库 : show databases;4. 切换连接数据库的语法 : use mydatabase;5. 显示数据库中的所有表 : show tables;6. 显示某个表创建时的全部信息 : show create table tabl

Linux命令:Mysql系列之二cmake编译安装使用mysqladmin管理工具

MySQL相关概念:MySQL是单进程多线程接收应用的请求. SQL/MySQL 1.事务,隔离,并发控制,锁 2.用户和权限 3.监控 STATUS 4.索引类型:查询 VARIABLES 5.备份和恢复 6.复制功能 7.集群 DML:数据操作语言 INSERT:插入 DELETE:删除 SELECT:挑选,选择,查询 UPDATE:更新,修改 DDL:数据定义语言 CREATE:创建 DROP:删除 ALTER:修改 DCL:数据控制语言 GRANT:授权 REVOKE:取消权限 MySQ