未加星标

mysql 协议的错误包及解析

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
git

https://github.com/sea-boat/mysql-protocol

概况

mysql客户端与mysql服务端交互过程中,如果服务端遇到错误需要告知客户端则返回错误包。

mysql通信报文结构 类型 名字 描述 int<3> payload长度 按照the least significant byte first存储,3个字节的payload和1个字节的序列号组合成报文头 int<1> 序列号 string payload 报文体,长度即为前面指定的payload长度 错误包 Payload Type Name Description int<1> header [ff] header of the ERR packet int<2> error_code error-code if capabilities & CLIENT_PROTOCOL_41 { string[1] sql_state_marker marker of the SQL State string[5] sql_state SQL State } string error_message human readable error message

更多详情 : http://dev.mysql.com/doc/internals/en/packet-ERR_Packet.html

错误包操作 错误包类 /** * * @author seaboat * @date 2016-09-25 * @version 1.0 * <pre><b>email: </b>849586227@qq.com</pre> * <pre><b>blog: </b>http://blog.csdn.net/wangyangzhizhou</pre> * <p>mysql error packet.</p> */ public class ErrorPacket extends MySQLPacket { public static final byte header = (byte) 0xff; private static final byte SQLSTATE_MARKER = (byte) '#'; private static final byte[] DEFAULT_SQLSTATE = "HY000".getBytes(); public int errno; public byte mark = SQLSTATE_MARKER; public byte[] sqlState = DEFAULT_SQLSTATE; public byte[] message; public void read(byte[] data) { MySQLMessage mm = new MySQLMessage(data); packetLength = mm.readUB3(); packetId = mm.read(); mm.read(); errno = mm.readUB2(); if (mm.hasRemaining() && (mm.read(mm.position()) == SQLSTATE_MARKER)) { mm.read(); sqlState = mm.readBytes(5); } message = mm.readBytes(); } public void write(ByteBuffer buffer) { int size = calcPacketSize(); BufferUtil.writeUB3(buffer, size); buffer.put(packetId); buffer.put(header); BufferUtil.writeUB2(buffer, errno); buffer.put(mark); buffer.put(sqlState); buffer.put(message); } @Override public int calcPacketSize() { int size = 9;// 1 + 2 + 1 + 5 if (message != null) { size += message.length; } return size; } @Override protected String getPacketInfo() { return "MySQL Error Packet"; } } 十六进制转换工具 /** * * @author seaboat * @date 2016-09-25 * @version 1.0 * <pre><b>email: </b>849586227@qq.com</pre> * <pre><b>blog: </b>http://blog.csdn.net/wangyangzhizhou</pre> * <p>hex transform util.</p> */ public class HexUtil { private final static byte[] hex = "0123456789ABCDEF".getBytes(); public static String Bytes2HexString(byte[] b) { byte[] buff = new byte[2 * b.length]; for (int i = 0; i < b.length; i++) { buff[2 * i] = hex[(b[i] >> 4) & 0x0f]; buff[2 * i + 1] = hex[b[i] & 0x0f]; } return new String(buff); } public static String str2HexStr(String str) { char[] chars = "0123456789ABCDEF".toCharArray(); StringBuilder sb = new StringBuilder(""); byte[] bs = str.getBytes(); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append(chars[bit]); bit = bs[i] & 0x0f; sb.append(chars[bit]); } return sb.toString(); } } 错误包生成测试 /** * * @author seaboat * @date 2016-09-25 * @version 1.0 * <pre><b>email: </b>849586227@qq.com</pre> * <pre><b>blog: </b>http://blog.csdn.net/wangyangzhizhou</pre> * <p>test auth packet.</p> */ public class ErrorPacketTest { @Test public void produce() { ErrorPacket err = new ErrorPacket(); err.packetId = 1; err.errno = 32322; err.message = "sorry".getBytes(); ByteBuffer buffer = ByteBuffer.allocate(256); err.write(buffer); buffer.flip(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes, 0, bytes.length); String result = HexUtil.Bytes2HexString(bytes); System.out.println(result); assertTrue(Integer.valueOf(result.substring(0, 2), 16) == result .length() / 2 - 4); ErrorPacket err2 = new ErrorPacket(); err2.read(bytes); assertTrue(err2.errno == 32322); assertTrue(err2.message.length == "sorry".getBytes().length); } }

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQLUBAU
分页:12
转载请注明
本文标题:mysql 协议的错误包及解析
本站链接:http://www.codesec.net/view/522340.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(99)