### 安装依赖
[chenjunjun@localhost ~]$ su -
Password:
[root@localhost ~]# yum install wget cmake gcc-c++ boost-devel mysql-devel
[root@localhost ~]# exit
logout
[chenjunjun@localhost ~]$
### 编译安装mysql-connector-c++
[chenjunjun@localhost ~]$ wget http://dev.mysql.com/get/Downloads/Connector-C++/mysql-connector-c++-1.1.4.tar.gz
[chenjunjun@localhost ~]$ tar zxvf mysql-connector-c++-1.1.4.tar.gz
[chenjunjun@localhost ~]$ cd mysql-connector-c++-1.1.4
[chenjunjun@localhost mysql-connector-c++-1.1.4]$ mkdir build
[chenjunjun@localhost mysql-connector-c++-1.1.4]$ cd build
[chenjunjun@localhost build]$ cmake -DCMAKE_INSTALL_PREFIX=/home/chenjunjun/usr/mysql_connect_c++/ -DMYSQL_LIB=/usr/lib64/mysql/libmysqlclient.so ..
[chenjunjun@localhost build]$ make
[chenjunjun@localhost build]$ vim cppconn/cmake_install.cmake
38 "/home/chenjunjun/mysql-connector-c++-1.1.4/cppconn/config.h"
==修改为==>
38 "/home/chenjunjun/mysql-connector-c++-1.1.4/build/cppconn/config.h"
[chenjunjun@localhost build]$ make install
[chenjunjun@localhost build]$ cd
[chenjunjun@localhost ~]$
### 设置
[chenjunjun@localhost ~]$ echo "export LD_LIBRARY_PATH=~/usr/mysql_connect_c++/lib/" >> ~/.bashrc
[chenjunjun@localhost ~]$ . ./.bashrc
### 测试
[chenjunjun@localhost ~]$ vi test.cc
#include
#include
#include
#include
using namespace std;
using namespace sql;
int main(){
Driver* driver;
Connection* conn;
driver = get_driver_instance();
conn = driver->connect("数据库地址", "用户名", "用户密码");
conn->setAutoCommit(1);
cout << "DataBase connection autocommit mode = " << conn->getAutoCommit() << endl;
delete conn;
driver = NULL;
conn = NULL;
return 0;
}
[chenjunjun@localhost ~]$ g++ test.cc -I ~/usr/mysql_connect_c++/include -L ~/usr/mysql_connect_c++/lib -l mysqlcppconn -o test
[chenjunjun@localhost ~]$ ./test
DataBase connection autocommit mode = 1
[chenjunjun@localhost ~]$