匿名通过本文主要向大家介绍了mysql ,服务端,握手包等相关知识,希望本文的分享对您有所帮助
概况
mysql客户端登陆到mysql服务端需要一个交互的过程,这里先看服务端给客户端发送的初始握手包。如下,client通过socket连接到server指定的端口后,server将往client发送初始握手包。服务端会根据不同的服务版本和不同的配置返回不同的初始化握手包。
client server |------connect---- >| | | |<----handshake-----| | | | | | |
mysql通信报文结构
| 类型 | 名字 | 描述 |
|---|---|---|
| int<3> | payload长度 | 按照the least significant byte first存储,3个字节的payload和1个字节的序列号组合成报文头 |
| int<1> | 序列号 | |
| string | payload | 报文体,长度即为前面指定的payload长度 |
初始握手包
HandshakeV10协议如下
1 [0a] protocol version
string[NUL] server version
4 connection id
string[8] auth-plugin-data-part-1
1 [00] filler
2 capability flags (lower 2 bytes)
if more data in the packet:
1 character set
2 status flags
2 capability flags (upper 2 bytes)
if capabilities & CLIENT_PLUGIN_AUTH {
1 length of auth-plugin-data
} else {
1 [00]
}
string[10] reserved (all [00])
if capabilities & CLIENT_SECURE_CONNECTION {
string[$len] auth-plugin-data-part-2 ($len=MAX(13, length of auth-plugin-data - 8))
if capabilities & CLIENT_PLUGIN_AUTH {
if version >= (5.5.7 and < 5.5.10) or (>= 5.6.0 and < 5.6.2) {
string[EOF] auth-plugin name
} elseif version >= 5.5.10 or >= 5.6.2 {
string[NUL] auth-plugin name
}
}生成初始握手包
定义版
/**
*
* @author seaboat
* @date 2016-09-25
* @version 1.0
* <pre><b>email: </b>849586227@qq.com</pre>
* <pre><b>blog: </b>http:///;/pre>
* <p>proxy's version.</p>
*/public interface Versions {
byte PROTOCOL_VERSION = 10; byte[] SERVER_VERSION = "5.6.0-snapshot".getBytes();
}随机数工具
/**
*
* @author seaboat
* @date 2016-09-25
* @version 1.0
* <pre><b>email: </b>849586227@qq.com</pre>
* <pre><b>blog: </b>http:///;/pre>
* <p>a random util .</p>
*/public class RandomUtil {
private static final byte[] bytes = { '1', '2', '3', '4', '5', '6', '7',
'8', '9', '0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v',
'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V',
'B', 'N', 'M' };
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
private static final long integerMask = (1L << 33) - 1;
private static final long seedUniquifier = 8682522807148012L;
private static long seed;
static {
long s = seedUniquifier + System.nanoTime();
s = (s ^ multiplier) & mask;
seed = s;
} public static final byte[] randomBytes(int size) {
byte[] bb = bytes;
byte[] ab = new byte[size];
for (int i = 0; i < size; i++) {
ab[i] = randomByte(bb);
} return ab;
} private static byte randomByte(byte[] b) {
int ran = (int) ((next() & integerMask) >>> 16);
return b[ran % b.length];
} private static long next() {
long oldSeed = seed;
long nextSeed = 0L;
do {
nextSeed = (oldSeed * multiplier + addend) & mask;
}
while (oldSeed == nextSeed);
seed = nextSeed;
return nextSeed;
}
}mysql包基类
/**
*
* @author seaboat
* @date 2016-09-25
* @version 1.0
* <pre><b>email: </b>849586227@qq.com</pre>
* <pre><b>blog: </b>http:///;/pre>
* <p>MySQLPacket is mysql's basic packet.</p>
*/public abstract class MySQLPacket {
/**
* none, this is an internal thread state
*/
public static final byte COM_SLEEP = 0; /**
* mysql_close
*/
public static final byte COM_QUIT = 1; /**
* mysql_select_db
*/
public static final byte COM_INIT_DB = 2; /**
* mysql_real_query
*/
public static final byte COM_QUERY = 3; /**
* mysql_list_fields
*/
public static final byte COM_FIELD_LIST = 4; /**
* mysql_create_db (deprecated)
*/
public static final byte COM_CREATE_DB = 5; /**
* mysql_drop_db (deprecated)
*/
public static final byte COM_DROP_DB = 6; /**
* mysql_refresh
*/
public static final byte COM_REFRESH = 7; /**
* mysql_shutdown
*/
public static final byte COM_SHUTDOWN = 8; /**
* mysql_stat
*/
public static final byte COM_STATISTICS = 9; /**
* mysql_list_processes
*/
public static final byte COM_PROCESS_INFO = 10; /**
* none, this is an internal thread state
*/
public static final byte COM_CONNECT = 11; /**
* mysql_kill
*/
public static final byte COM_PROCESS_KILL = 12; /**
* mysql_dump_debug_info
*/
public static final byte COM_DEBUG = 13; /**
* mysql_ping
*/
public static final byte COM_PING = 14; /**
* none, this is an internal thread state
*/
public static final byte COM_TIME = 15; /**
* none, this is an internal thread state
*/
public static final byte COM_DELAYED_INSERT = 16; /**
* mysql_change_user
*/
public static final byte COM_CHANGE_USER = 17; /**
* used by slave server mysqlbinlog
*/
public static final byte COM_BINLOG_DUMP = 18; /**
* used by slave server to get master table
*/
public static final byte COM_TABLE_DUMP = 19; /**
* used by slave to log connection to master
*/
public static final byte COM_CONNECT_OUT = 20; /**
* used by slave to register to master
*/
public static final byte COM_REGISTER_SLAVE = 21; /**
* mysql_stmt_prepare
*/
public static final byte COM_STMT_PREPARE = 22; /**
* mysql_stmt_execute
*/
public static final byte COM_STMT_EXECUTE = 23; /**
* mysql_stmt_send_long_data
*/
public static final byte COM_STMT_SEND_LONG_DATA = 24; /**
* mysql_stmt_close
*/
public static final byte COM_STMT_CLOSE = 25; /**
* mysql_st

