MySQL5.7之Debug模式


使用MySQL的调试模式跟踪一些常见报错信息的过程

编译MySQL5.7

[root@mysql3 percona-server-5.7.23-24]# mkdir -p /opt/mysql57
[root@mysql3 percona-server-5.7.23-24]# mkdir -p /data/mysql57/data
#编译的时候加上with_debug
cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql57 -DMYSQL_DATADIR=/data/mysql57/data -DSYSCONFDIR=/opt/mysql57 -DWITH_BOOST=/usr/local/boost -DWITH_DEBUG=1
#初始化db
[root@mysql3 mysql57]# /opt/mysql57/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql57 --datadir=/data/mysql57/data
[root@mysql3 mysql57]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld3308
启动db
[root@mysql3 mysql57]# /etc/rc.d/init.d/mysqld3308 start
Starting MySQL (Percona Server).... SUCCESS!
[root@mysql3 mysql57]# ps -ef|grep mysqld
root 1354 1 0 Oct28 ? 00:00:00 /bin/sh /opt/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql3.pid
mysql 2524 1354 0 Oct28 ? 01:31:38 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/data/mysql/data --plugin-dir=/opt/mysql/lib/mysql/plugin --user=mysql --log-error=/data/mysql/log/alert.log --open-files-limit=135582 --pid-file=/data/mysql/data/mysql3.pid --socket=/data/mysql/run/mysql.sock --port=3306
root 3776 1 0 09:23 pts/3 00:00:00 /bin/sh /opt/mysql57/bin/mysqld_safe --datadir=/data/mysql57/data --pid-file=/data/mysql57/data/mysql3.pid
mysql 4934 3776 22 09:23 pts/3 00:00:03 /opt/mysql57/bin/mysqld --basedir=/opt/mysql57 --datadir=/data/mysql57/data --plugin-dir=/opt/mysql57/lib/mysql/plugin --user=mysql --log-error=/data/mysql57/log/alert.log --open-files-limit=135582 --pid-file=/data/mysql57/data/mysql3.pid --socket=/data/mysql57/run/mysql.sock --port=3308
root 4983 27157 0 09:23 pts/3 00:00:00 grep mysqld
[root@mysql3 mysql57]#
登录db
[root@mysql3 mysql57]# /opt/mysql57/bin/mysql --socket=/data/mysql57/run/mysql.sock
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
忘记了初始化db时的密码,my.cnf中加入skip-grant-tables重启mysqld
[root@mysql3 mysql57]#
[root@mysql3 mysql57]# /etc/rc.d/init.d/mysqld3308 restart
Shutting down MySQL (Percona Server).. SUCCESS!
Starting MySQL (Percona Server).... SUCCESS!
[root@mysql3 mysql57]#
[root@mysql3 mysql57]# /opt/mysql57/bin/mysql --socket=/data/mysql57/run/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-24-debug-log Source distribution
Copyright (c) 2009-2018 Percona LLC and/or its affiliates
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@(none) 09:27:15>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
root@(none) 09:27:18>
root@mysql 09:28:28>select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *89C6AA92FB4A025F12E4016BA0A7EDDB685AC1A9 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
root@mysql 09:28:41>alter user root@localhost identified by '';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
root@mysql 09:29:36>update mysql.user set authentication_string=password('xueledba') where user='root' ;
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
root@mysql 09:29:49>flush privileges;
Query OK, 0 rows affected (0.00 sec)
注释掉my.cnf中的skip-grant-tables重启mysqld,用设置的新密码登录:
[root@mysql3 mysql57]# /opt/mysql57/bin/mysql --socket=/data/mysql57/run/mysql.sock -pxueledba
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23-24-debug-log
Copyright (c) 2009-2018 Percona LLC and/or its affiliates
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(unknown)@(none) 09:31:15>use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
(unknown)@(none) 09:31:30>alter user root@localhost identified by '';
Query OK, 0 rows affected (0.00 sec)
root@(none) 09:31:38>flush privileges;
Query OK, 0 rows affected (0.00 sec)
root@(none) 09:31:43>use mysql;
Database changed
root@mysql 09:31:46>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
退出再登录,就不需要密码了
[root@mysql3 mysql57]# /opt/mysql57/bin/mysql --socket=/data/mysql57/run/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23-24-debug-log Source distribution
Copyright (c) 2009-2018 Percona LLC and/or its affiliates
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@(none) 09:32:48>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
root@(none) 09:32:53>exit
Bye
设置别名:
[root@mysql3 mysql57]# vi ~/.bash_profile
alias mysql2='/opt/mysql57/bin/mysql'
[root@mysql3 mysql57]# source ~/.bash_profile
[root@mysql3 mysql57]# mysql2
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.23-24-debug-log Source distribution
Copyright (c) 2009-2018 Percona LLC and/or its affiliates
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@(none) 09:34:12>exit
Bye

以debug模式启动

[root@mysql3 mysql57]# /etc/rc.d/init.d/mysqld3308 start --debug --console
Starting MySQL (Percona Server).... SUCCESS!
[root@mysql3 mysql57]# ps -ef|grep mysqld|grep mysql57
root 8912 1 0 09:46 pts/3 00:00:00 /bin/sh /opt/mysql57/bin/mysqld_safe --datadir=/data/mysql57/data --pid-file=/data/mysql57/data/mysql3.pid --debug --console
mysql 10099 8912 6 09:46 pts/3 00:00:03 /opt/mysql57/bin/mysqld --basedir=/opt/mysql57 --datadir=/data/mysql57/data --plugin-dir=/opt/mysql57/lib/mysql/plugin --user=mysql --debug --console --log-error=/data/mysql57/log/alert.log --open-files-limit=135582 --pid-file=/data/mysql57/data/mysql3.pid --socket=/data/mysql57/run/mysql.sock --port=3308
[root@mysql3 mysql57]#

创建dba库和测试表

root@(none) 09:38:15>create database dba;
Query OK, 1 row affected (0.00 sec)
root@(none) 09:38:36>use dba;
Database changed
root@dba 09:38:41>create table t1(a int,b varchar(10));
Query OK, 0 rows affected (0.02 sec)
root@dba 09:41:02>insert into t1 values(1,'aaaaa'),(2,'bbbbb');
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
root@dba 09:41:36>select * from t1;
+------+-------+
| a | b |
+------+-------+
| 1 | aaaaa |
| 2 | bbbbb |
+------+-------+
2 rows in set (0.00 sec)

验证一个报错

root@dba 09:41:41>select * from t1 where c=1;
ERROR 1054 (42S22): Unknown column 'c' in 'where clause'
root@dba 09:41:56>
请问:这个c字段不存在的报错是在Server层的哪个阶段报出来的?
跟踪trace文件可以看到这个过程:分析器阶段
113632 T@2: | | >mysql_parse
113633 T@2: | | | mysql_parse: query: 'select * from t1 where c=1'
113634 T@2: | | | >mysql_reset_thd_for_next_command
113635 T@2: | | | | debug: reset_unsafe_rollback_flags
113636 T@2: | | | | >clear_error
113637 T@2: | | | | <clear_error 3809
113638 T@2: | | | | >reset_diagnostics_area
113639 T@2: | | | | <reset_diagnostics_area 371
113640 T@2: | | | | >THD::clear_slow_extended
113641 T@2: | | | | <THD::clear_slow_extended 4415
113642 T@2: | | | | >reset_current_stmt_binlog_format_row
113643 T@2: | | | | | debug: in_sub_stmt: no, system_thread: NON_SYSTEM_THREAD
113644 T@2: | | | | | >set_current_stmt_binlog_format_row
113645 T@2: | | | | | <set_current_stmt_binlog_format_row 4099
113646 T@2: | | | | <reset_current_stmt_binlog_format_row 4121
113647 T@2: | | | | >THD::set_trans_pos
113648 T@2: | | | | | return: m_trans_log_file: (null), m_trans_fixed_log_file: (null), m_trans_end_pos: 0
113649 T@2: | | | | <THD::set_trans_pos 3145
113650 T@2: | | | | debug: is_current_stmt_binlog_format_row(): 1
113651 T@2: | | | <mysql_reset_thd_for_next_command 5623
查询缓存:如果缓存中有结果则直接返回给客户端
113677 T@2: | | | >Query_cache::send_result_to_client
113678 T@2: | | | <Query_cache::send_result_to_client 2255
解析SQL:
113679 T@2: | | | >parse_sql
113680 T@2: | | | | >alloc_root
113681 T@2: | | | | | enter: root: 0x7f0e98003ca8
113682 T@2: | | | | | exit: ptr: 0x7f0e98006600
113683 T@2: | | | | <alloc_root 304
113684 T@2: | | | | >alloc_root
113685 T@2: | | | | | enter: root: 0x7f0e98003ca8
113686 T@2: | | | | | exit: ptr: 0x7f0e98006720
113687 T@2: | | | | <alloc_root 304
检查权限:
113832 T@2: | | | | >check_access
113833 T@2: | | | | | enter: db: dba want_access: 1 master_access: 536870911
113834 T@2: | | | | | THD::enter_stage: 'checking permissions' /data/src/percona-server-5.7.23-24/sql/auth/sql_authorization.cc:818
113835 T@2: | | | | | >PROFILING::status_change
113836 T@2: | | | | | <PROFILING::status_change 384
113837 T@2: | | | | <check_access 889
113838 T@2: | | | | >check_grant
113839 T@2: | | | | <check_grant 2218
打开表:
113855 T@2: | | | | | | >open_and_process_table
113856 T@2: | | | | | | | tcache: opening table: 'dba'.'t1' item: 0x7f0e98006bc0
113857 T@2: | | | | | | | >open_table
113858 T@2: | | | | | | | | >my_hash_first_from_hash_value
113859 T@2: | | | | | | | | | exit: found key at 0
判断字段是否存在:
113949 T@2: | | | | | | >SELECT_LEX::setup_conds
113950 T@2: | | | | | | | info: thd->mark_used_columns: 1
113951 T@2: | | | | | | | >find_field_in_table_ref
113952 T@2: | | | | | | | | enter: table: 't1' field name: 'c' item name: 'c' ref 0x7f0e98007328
113953 T@2: | | | | | | | | >find_field_in_table
113954 T@2: | | | | | | | | | enter: table: 't1', field name: 'c'
113955 T@2: | | | | | | | | <find_field_in_table 7532
113956 T@2: | | | | | | | <find_field_in_table_ref 7731
113957 T@2: | | | | | | | >find_field_in_table_ref
113958 T@2: | | | | | | | | enter: table: 't1' field name: 'c' item name: 'c' ref 0x7f0e98007328
113959 T@2: | | | | | | | | >find_field_in_table
113960 T@2: | | | | | | | | | enter: table: 't1', field name: 'c'
113961 T@2: | | | | | | | | <find_field_in_table 7532
113962 T@2: | | | | | | | <find_field_in_table_ref 7731
113963 T@2: | | | | | | | >check_grant_column
113964 T@2: | | | | | | | | enter: table: t1 want_privilege: 1
113965 T@2: | | | | | | | <check_grant_column 2274
113966 T@2: | | | | | | | >my_error
113967 T@2: | | | | | | | | my: nr: 1054 MyFlags: 0 errno: 11
113968 T@2: | | | | | | | | >my_message_sql
113969 T@2: | | | | | | | | | error: error: 1054 message: 'Unknown column 'c' in 'where clause''
解析器关闭表
114032 T@2: | | | | THD::enter_stage: 'closing tables' /data/src/percona-server-5.7.23-24/sql/sql_parse.cc:5278
文章目录
  1. 1. 编译MySQL5.7
  2. 2. 以debug模式启动
  3. 3. 创建dba库和测试表
  4. 4. 验证一个报错
|