本文转载自微信公众号「DBA闲思杂想录」,作者潇湘隐者。转载本文请联系DBA闲思杂想录公众号。
MySQL Sniffer这款抓包工具的安装看似非常简单,官方的安装介绍如下所示:
- git clone https://github.com/Qihoo360/mysql-sniffer
- cd mysql-sniffer
- mkdir proj
- cd proj
- cmake ../
- make
- cd bin/
虽然官方文档的安装介绍非常简单,但是实际上,在CentOS 7下面安装过程中问题多多,如果你没有踩过这些坑的话,建议你看看下面我踩过的一些坑。下面测试环境为CentOS Linux release 7.5.1804,可能不同版本或环境有所区别,请以实际情况为准。
- [root@KerryDB ~]# more /etc/redhat-release
- CentOS Linux release 7.5.1804 (Core)
官方文档[1]中提示,MySQL Sniffer依赖这些包glib2-devel(2.28.8)、libpcap-devel(1.4.0)、libnet-devel(1.1.6),但是实际上它不仅仅只依赖这三个包,而是下面一堆包。
- yum install cmake
- yum install libpcap-devel
- yum install glib2-devel
- yum install libnet-devel
- yum install gcc
- yum install gcc-c++
在安装前,你必须执行下面脚本检查是否缺少相关依赖包,避免踩到一些坑。
- yum list installed | grep -E "cmake|libpcap-devel|glib2-devel|libnet-devel|gcc|gcc-c++"
下面是成功安装后,查询依赖包的信息:
- [root@KerryDB ~]# yum list installed | grep -E "cmake|libpcap-devel|glib2-devel|libnet-devel|gcc|gcc-c++"
- cmake.x86_64 2.8.12.2-2.el7 @base
- gcc.x86_64 4.8.5-44.el7 @base
- gcc-c++.x86_64 4.8.5-44.el7 @base
- glib2-devel.x86_64 2.56.1-9.el7_9 @updates
- libgcc.x86_64 4.8.5-44.el7 @base
- libnet-devel.x86_64 1.1.6-7.el7 @base
- libpcap-devel.x86_64 14:1.5.3-12.el7 @base
下面是我安装过程中踩到的一些坑,如下所示,cmake编译安装过程中报错,具体错误信息如下截图所示
"fatal error: libnet.h: No such file or directory"这个错误是因为缺少libnet-devel包,这里是因为查询验证时遗漏了这个包。安装下面libnet-devel包后,上面错误解决。
- # yum install libnet-devel
然后cmake编译安装依然报错:CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. 这里是因为缺少包gcc-c++包
注意,CentOS下的g++安装包的名字叫做gcc-c++,这是与Centos与Ubuntu不同的地方。安装命令如下:
- #yum install gcc gcc-c++
然后cmake编译成功。
但是make编译安装时,出现下面错误
- [root@KerryDB proj]# make
- Scanning dependencies of target mysql-sniffer
- [ 16%] Building C object bin/CMakeFiles/mysql-sniffer.dir/main.c.o
- [ 33%] Building C object bin/CMakeFiles/mysql-sniffer.dir/mysql-dissector.c.o
- /root/klb/mysql-sniffer/src/mysql-dissector.c: In function ‘decode_mysql_lenenc_int’:
- /root/klb/mysql-sniffer/src/mysql-dissector.c:112:5: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
- return *(int*)value;
- ^
- [ 50%] Building C object bin/CMakeFiles/mysql-sniffer.dir/util.c.o
- [ 66%] Building CXX object bin/CMakeFiles/mysql-sniffer.dir/session.cpp.o
- [ 83%] Building CXX object bin/CMakeFiles/mysql-sniffer.dir/sniff-config.cpp.o
- /root/klb/mysql-sniffer/src/sniff-config.cpp: In function ‘int parse_cmdline_option(int, char**)’:
- /root/klb/mysql-sniffer/src/sniff-config.cpp:183:9: warning: variable ‘opt_len’ set but not used [-Wunused-but-set-variable]
- int opt_len;
- ^
- [100%] Building CXX object bin/CMakeFiles/mysql-sniffer.dir/sniff-log.cpp.o
- Linking CXX executable mysql-sniffer
- /usr/bin/ld: /root/klb/mysql-sniffer/lib/libgthread-2.0.a(gthread-impl.o): undefined reference to symbol 'pthread_setspecific@@GLIBC_2.2.5'
- //usr/lib64/libpthread.so.0: error adding symbols: DSO missing from command line
- collect2: error: ld returned 1 exit status
- make[2]: *** [bin/mysql-sniffer] Error 1
- make[1]: *** [bin/CMakeFiles/mysql-sniffer.dir/all] Error 2
- make: *** [all] Error 2
"make 报错 #30"[2]中,提示通过修改配置文件mysql-sniffer/src/CMakeLists.txt来解决
修改前
修改后
重新编译安装就OK了,MySQL Sniffer顺利编译成功。
参考资料
[1]mysql-sniff: https://github.com/Qihoo360/mysql-sniffer
[2]make 报错 #30: https://github.com/Qihoo360/mysql-sniffer/issues/30