最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

详细介绍mysql协议的错误包及解析

来源:懂视网 责编:小采 时间:2020-11-09 09:09:17
文档

详细介绍mysql协议的错误包及解析

详细介绍mysql协议的错误包及解析:githttps://github.com/sea-boat/mysql-protocol概况mysql客户端与mysql服务端交互过程中,如果服务端遇到错误需要告知客户端则返回错误包。mysql通信报文结构类型名字描述int<3>payload长度按照the least significant
推荐度:
导读详细介绍mysql协议的错误包及解析:githttps://github.com/sea-boat/mysql-protocol概况mysql客户端与mysql服务端交互过程中,如果服务端遇到错误需要告知客户端则返回错误包。mysql通信报文结构类型名字描述int<3>payload长度按照the least significant

git

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

概况

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

mysql通信报文结构

类型名字描述
int<3>payload长度按照the least significant byte first存储,3个字节的payload和1个字节的序列号组合成报文头
int<1>序列号
stringpayload报文体,长度即为前面指定的payload长度

错误包

Payload

TypeNameDescription
int<1>header[ff] header of the ERR packet
int<2>error_codeerror-code

if capabilities & CLIENT_PROTOCOL_41 {
string[1]sql_state_markermarker of the SQL State
string[5]sql_stateSQL State

}
stringerror_messagehuman readable error message

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

错误包操作

  1. 错误包类

/**
 * 
 * @author seaboat
 * @date 2016-09-25
 * @version 1.0
 * <pre><b>email: </b>849586227@qq.com</pre>
 * <pre><b>blog: </b>http://www.gxlcms.com/;/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";
 }

}
  1. 十六进制转换工具

/**
 * 
 * @author seaboat
 * @date 2016-09-25
 * @version 1.0
 * <pre><b>email: </b>849586227@qq.com</pre>
 * <pre><b>blog: </b>http://www.gxlcms.com/;/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();
 }
}
  1. 错误包生成测试

/**
 * 
 * @author seaboat
 * @date 2016-09-25
 * @version 1.0
 * <pre><b>email: </b>849586227@qq.com</pre>
 * <pre><b>blog: </b>http://www.gxlcms.com/;/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);
 }

}

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

详细介绍mysql协议的错误包及解析

详细介绍mysql协议的错误包及解析:githttps://github.com/sea-boat/mysql-protocol概况mysql客户端与mysql服务端交互过程中,如果服务端遇到错误需要告知客户端则返回错误包。mysql通信报文结构类型名字描述int<3>payload长度按照the least significant
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top