首页 > 未分类 > 在CentOS中搭建MySQL开发环境
2019
11-12

在CentOS中搭建MySQL开发环境

一.部署mysql-connector-c

  1. 下载mysql-connector-c-6.1.5-linux-glibc2.5-x86_64.tar.gzhttp://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-6.1.5-linux-glibc2.5-x86_64.tar.gz
  2. 解压缩mysql-connector-c-6.1.5-linux-glibc2.5-x86_64.tar.gz,输入命令:tar xvzf mysql-connector-c-6.1.5-linux-glibc2.5-x86_64.tar.gz将解压缩获得目录重命名为mysqlclient
  3. 进入mysqlclient目录
  4. 将include目录拷贝到/usr/include/mysqlclient目录下,输入命令:cp -r include/ /usr/include/mysqlclient
  5. 将lib目录拷贝到/usr/lib64/mysqlclient目录下,输入命令:cp -r lib/ /usr/lib64/mysqlclient

二.部署mysql-connector c++

  1. 下载mysql-connector-c++-1.1.1-linux-glibc2.3-x86-64bit.tar.gzhttp://cdn.mysql.com/Downloads/Connector-C++/mysql-connector-c++-1.1.1-linux-glibc2.3-x86-64bit.tar.gz
  2. 解压缩mysql-connector-c++-1.1.1-linux-glibc2.3-x86-64bit.tar.gz,输入命令:tar xvzf mysql-connector-c++-1.1.1-linux-glibc2.3-x86-64bit.tar.gz将解压缩得到的目录重命名为mysqlcppconn
  3. 进入mysqlcppconn目录
  4. 将include目录拷贝到/usr/include/mysqlcppconn目录下,输入命令:cp -r include/ /usr/include/mysqlcppconn
  5. 将lib目录拷贝到/usr/lib64/mysqlcppconn目录下,输入命令:cp -r lib/ /usr/lib64/mysqlcppconn

三.设置GCC-G++相关的环境变量

  1. 修改环境变量CPLUS_INCLUDE_PATH,LIBRARY_PATH,LD_LIBRARY_PATH
  2. 进入/home/$your_username目录,输入命令:vi .bash_profile(使用VI编辑器修改.bash_profile文件)修改内容如下:
    CPLUS_INCLUDE_PATH=/usr/include/mysqlcppconn:/usr/include/mysqlclient
    export CPLUS_INCLUDE_PATH
    LIBRARY_PATH=/usr/lib64:/usr/lib64/mysqlclient:/usr/lib64/mysqlcppconn
    export LIBRARY_PATH
    LD_LIBRARY_PATH=/usr/lib64:/usr/lib64/mysql:/usr/lib64/mysqlcppconn
    export LD_LIBRARY_PATH
  3. 保存后,在Terminal输入命令,source .bash_profile,以使修改的内容立即生效。
  4. 输入命令:echo $CPLUS_INCLUDE_PATH,查看环境变量的值

四.编写测试程序

  1. 创建testmysql目录,新建文件main.cpp代码如下:
    #include <iostream>
    #include <sstream>
    #include <memory>
    #include <string>
    #include <stdexcept>
    
    using namespace std;
    
    #include <mysql_connection.h>
    #include <mysql_driver.h>
    #include <cppconn/driver.h>
    using namespace sql;
    
    #define DBHOST "tcp://localhost:3306"
    #define USER "root"
    #define PASSWORD "123456"
    
    int main()
    {
        Driver* driver = NULL;
        Connection* conn = NULL;
        driver = mysql::get_driver_instance();
        conn = driver->connect(DBHOST, USER, PASSWORD);
        conn->setAutoCommit(0);
        cout<< "DataBase connection autocommit mode ="<<conn->getAutoCommit() <<endl;
        delete conn;
        driver = NULL;
        conn = NULL;
        return 0;
    }
    
  2. 编译程序,进入testmysql目录,输入命令:g++ main.cpp -lmysqlcppconn -lmysqlclient -o testmysql
  3. 运行测试程序,输入命令:./testmysql
最后编辑:
作者:搬运工
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。