centos下安装 mysql_connector_c++
依赖
- cmake 编译安装时需要
- Connector/C:Connector/C++ 依赖于 Connector/C 所以两者都需要安装,而且要采用与之匹配的版本,我采用最新的;
- boost 1.34及以上版本,CentOS 6.5 下的版本为 1.41.0 满足需求
安装
下载
- Connector/C:http://dev.mysql.com/downloads/connector/c/ 得到文件 mysql-connector-c-6.1.5-linux-glibc2.5-x86_64.tar.gz
- Connector/C++:http://dev.mysql.com/downloads/connector/cpp/ 得到文件 mysql-connector-c++-1.1.3-linux-el6-x86-64bit.tar.gz
两者平台都是选用"Linux-Generic",避免编译麻烦,如果安装rpm包,可能会与系统中已经安装的mysql文件冲突
安装
安装boost,需要root权限:
yum install boost-devel
选择一个目录,将其解压,并将其中文件放入对应的头文件目录已经库目录,这里,我放在自己用户定义的头文件和库目录:
tar zxvf mysql-connector-c-6.1.5-linux-glibc2.5-x86_64.tar.gz -C /tmp
tar zxvf mysql-connector-c++-1.1.3-linux-el6-x86-64bit.tar.gz -C /tmp
cd /tmp/mysql-connector-c-6.1.5-linux-glibc2.5-x86_64
mv include ~/include/mysql_connector_c
cd lib/
mv * ~/lib64/
cd /tmp/mysql-connector-c++-1.1.3-linux-el6-x86-64bit/
mv include ~/include/mysql_connector_c++
cd lib/
mv * ~/lib64/
设置运行时连接库目录:
export LD_LIBRARY_PATH=~/lib64/
测试
编写如下代码:
#include <iostream>
#include <string>
#include <mysql_connection.h>
#include <mysql_driver.h>
using namepsce std;
using namespace sql;
int main(){
Driver* driver;
Connection* conn;
driver = get_driver_instance();
conn = driver->connect(DBHOST, USER, PASSWORD);
conn->setAutoCommit(1);
cout << "DataBase connection autocommit mode = " << conn->getAutoCommit() << endl;
delete conn;
driver = NULL;
conn = NULL;
return 0;
}
编译命令:
$ g++ test.cc -I ~/include/mysql_connector_c -I ~/include/mysql_connector_c++ -L ~/lib64 -l mysqlcppconn -l mysqlclient
$ ./a.out
DataBase connection autocommit mode = 0