欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux下C++连接操作MySQL数据库环境搭建 博客分类: c/c++LinuxMySQL c/c++LinuxMySQLgccboost 

程序员文章站 2024-03-05 21:56:37
...
Linux下C++连接操作MySQL数据库环境搭建


  日期     版本   作者   修改内容   备注

  2017年4月8日 V1.0   刘胜军  文件初建   无


一、引言
1.1 编写目的
  Linux下C++连接操作MySQL数据库环境搭建
1.2 术语
gcc/gcc-c++:
  The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages (libstdc++,...). GCC was originally written as the compiler for the GNU operating system. The GNU system was developed to be 100% free software, free in the sense that it respects the user's freedom.
boost/boost-devel:
  Boost provides free peer-reviewed portable C++ source libraries.
mysql-connector-c++:
  All distributions of Connector/C++ contain a README file, which contains platform-specific notes. At the end of the README file contained in the binary distribution you will find the settings used to build the binaries. If you experience build-related issues on a platform, it may help to check the settings used on the platform to build the binary.
1.3 准备工作
  一台安装有CentOS 7.X OS的机器,可连接互联网,或备有离线安装包。
1.4 注意事项
  使用yum源安装时,需正确配置yum源。

二、依赖环境搭建
2.1 安装gcc-c++
方法一:使用yum源
[root@user ~]# yum install -y gcc-c++

方法二:使用本地rpm
rpm -ivh libgcc-4.8.5-11.el7.x86_64.rpm --force --nodeps
rpm -ivh glibc-common-2.17-157.el7_3.1.x86_64.rpm --force --nodeps
rpm -ivh glibc-2.17-157.el7_3.1.x86_64.rpm --force --nodeps
rpm -ivh mpfr-3.1.1-4.el7.x86_64.rpm --force --nodeps
rpm -ivh libmpc-1.0.1-3.el7.x86_64.rpm --force --nodeps
rpm -ivh libstdc++-4.8.5-11.el7.x86_64.rpm --force --nodeps
rpm -ivh libstdc++-devel-4.8.5-11.el7.x86_64.rpm --force --nodeps
rpm -ivh cpp-4.8.5-11.el7.x86_64.rpm --force --nodeps
rpm -ivh libgomp-4.8.5-11.el7.x86_64.rpm --force --nodeps
rpm -ivh kernel-headers-3.10.0-514.10.2.el7.x86_64.rpm --force --nodeps
rpm -ivh glibc-headers-2.17-157.el7_3.1.x86_64.rpm --force --nodeps
rpm -ivh glibc-devel-2.17-157.el7_3.1.x86_64.rpm --force --nodeps
rpm -ivh gcc-4.8.5-11.el7.x86_64.rpm --force --nodeps
rpm -ivh gcc-c++-4.8.5-11.el7.x86_64.rpm --force --nodeps

2.2 安装boost-devel
方法一:使用yum源
[root@user ~]# yum install -y boost-devel

方法二:使用本地rpm
rpm -ivh boost-system-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-chrono-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-filesystem-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-thread-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh libicu-50.1.2-15.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-regex-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-date-time-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-wave-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-graph-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-locale-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-timer-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-random-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-program-options-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-context-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-atomic-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-python-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-iostreams-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-serialization-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-math-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-test-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-signals-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-1.53.0-26.el7.x86_64.rpm --force --nodeps
rpm -ivh boost-devel-1.53.0-26.el7.x86_64.rpm --force --nodeps

2.3 安装mysql-connector-c++
下载地址:
https://dev.mysql.com/downloads/connector/cpp/
Connector/C++ 1.1.8
Looking for previous GA versions?
Select Operating System:
Linux - Generic
Select OS Version:
Linux - Generic (glibc 2.5) (x86, 64-bit)
安装:
rpm -ivh mysql-connector-c++-1.1.8-linux-glibc2.5-x86-64bit.rpm


三、测试
3.1 编写测试文件
[root@user ~]# cat Test.cpp
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main() {
    try {
        std::cout << "入口开始" << std::endl;
        sql::mysql::MySQL_Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        driver = sql::mysql::get_mysql_driver_instance();
        std::cout << "连接数据库" << std::endl;
        con = driver->connect("tcp://127.0.0.1:3306", "test", "test");
        stmt = con->createStatement();
        stmt->execute("USE test"); //选择使用的数据库
        std::cout << "获取查询结果" << std::endl;
        sql::ResultSet* result = stmt->executeQuery("select now() as time");
        std::cout << "输出查询结果" << std::endl;
        if (result) {
            while (result->next()) {
                cout << result->getString(1) << std::endl;
            }
        }
    } catch (sql::SQLException &e) {
        return -1;
    }
}

3.2 编译测试文件
[root@user ~]# g++ -o test Test.cpp

3.3 测试
[root@user ~]# ./test 
入口开始
连接数据库
获取查询结果
输出查询结果
2017-04-08 12:38:20


四、问题解析
4.1 问题一:undefined reference to `sql::mysql::get_driver_instance()'
[root@user ~]# g++ -o test Test.cpp
/tmp/cc2gvcAt.o: In function `sql::mysql::get_mysql_driver_instance()':
Test.cpp:(.text+0x5): undefined reference to `sql::mysql::get_driver_instance()'
collect2: error: ld returned 1 exit status

解决方法:
[root@user ~]# g++ -o test Test.cpp -lmysqlcppconn
[root@user ~]# ./test 
入口开始
连接数据库
获取查询结果
输出查询结果
2017-04-08 12:45:14
[root@user ~]#