debian系列下c++调用mysql, linux下面安装mysql.h文件

1.介绍需求:

  python调用数据库,并做逻辑处理,时间为92.5s,从执行sql到得到数据150w条为22s,逻辑处理(2个for循环)为60s。前端处理30s,pending为2min左右,需要处理这个问题

  于是思考解决方案:  

    1. 取数据时数据拆分  

       先一个count返回条目,然后多次查询
    2. 逻辑部分怎么办?
      我想用c++处理试试,于是找代码,打算用python调用c++处理试试
      示例:
      
#include <iostream>
#include <string>
#include <cstdlib>

#include <mysql++/mysql++.h>

using namespace std;

#define MYSQL_USER "root"
#define MYSQL_PASSWD "passwd"
#define MYSQL_PORT 3306

int main(){

    mysqlpp::Connection con(false);
    con.set_option(new mysqlpp::SetCharsetNameOption("utf8"));
    if(!con.connect("test","localhost",MYSQL_USER,MYSQL_PASSWD,MYSQL_PORT)){
        cout<<"can't connect,check the user and passwd"<<endl;
        return -1;
    }
    cout<<"mysql connect successfully!"<<endl;

    mysqlpp::Query query=con.query("select * from City");
    mysqlpp::StoreQueryResult result=query.store();
    if(nullptr==result){
        cout<<"query failed!"<<endl;
        return -1;
    }

    for(auto iter=result.begin();iter!=result.end();++iter){
        cout<<"\t"<<(*iter)[0]<<endl;
    }

    return 0;
}
View Code

报错:

/opt/code/testC++/tcp_server/test_server_1/main.cpp:5:10: fatal error: mysql++/mysql++.h: 没有那个文件或目录
 #include <mysql++/mysql++.h>

2. 怎么安装<mysql++/mysql++.h>头文件

系统环境:

  debian系统kali

1. 配置

debian: 
  apt-get install libmysqlclient-dev
redhat:
  yum install mysql-devel

发现没有,就这么干,根据不同系统因地制宜

apt-cache search libmysql
发现没有
apt-get update更新源
apt-cache search libmysql 有了类似的了
apt-get install default-libmysqlclient-dev default-libmysqld-dev

2. 下载解压安装:

[root@localhost 下载]#wget https://tangentsoft.com/mysqlpp/releases/mysql++-3.2.4.tar.gz
[root@localhost 下载]# tar zxvf mysql++-3.2.2.tar.gz 

进入mysql++目录下,开始编译,先执行./configure生成makefile文件,之后再make,编译出libmysqlpp.so库文件:

./configure

报错:

 下面这段来自blog:https://blog.csdn.net/daodaozhu05/article/details/12970657

checking for MySQL include directory... configure: error: Didn't find the MySQL include dir in '
/usr/include/mysql
/usr/local/include/mysql
/usr/local/mysql/include
/usr/local/mysql/include/mysql
/usr/mysql/include/mysql
/opt/mysql/include/mysql
/sw/include/mysql'

这个有两种情况,这个是第一种

首先查找本地libmysqlclient的目录在哪里,在终端敲下面的命令:

locate libmysqlclient
sudo ./configure --with-mysql-lib=/usr/lib/x86_64-linux-gnu

如果还出现刚才的问题

Didn't find the mysql in ......

这是用--with-mysql-include选项进行安装,比如我的mysqlclient lib在/opt/local/lib/mysql5/mysql, 而mysql 在/opt/local/include/mysql5/mysql,则用下列命令安装一遍即可。

sudo ./configure --with-mysql-lib=/opt/local/lib/mysql5/mysql/ --with-mysql-include=/opt/local/include/mysql5/mysql/

3.编译并安装

sudo make

sudo make install

第二种方法:

apt-cache search libmysql
发现没有
apt-get update更新源
apt-cache search libmysql 有了类似的了

apt-get install
default-libmysqlclient-dev default-libmysqld-dev
 
root@corleone:/usr/local/lib# apt-cache search libmysql
default-libmysqlclient-dev - MySQL database development files (metapackage)
default-libmysqld-dev - MySQL embedded database development files (metapackage)
libcrypt-mysql-perl - Perl module to emulate the MySQL PASSWORD() function
libglpk40 - linear programming kit with integer (MIP) support
libmariadbclient-dev-compat - MariaDB database development files (libmysqlclient compatibility)
libmysql-diff-perl - module for comparing the table structure of two MySQL databases
libmysql-ocaml - OCaml bindings for MySql (runtime package)
libmysql-ocaml-dev - OCaml bindings for MySql (development package)
libmysqlcppconn-dev - MySQL Connector for C++ (development files)
libmysqlcppconn7v5 - MySQL Connector for C++ (library)
libreoffice-base-drivers - Database connectivity drivers for LibreOffice
node-mysql - MySQL client implementation for Node.js
solr-common - Enterprise search server based on Lucene3 - common files

然后直接也OK

./configure
 make 

make install

 3.执行c++代码执行测试

#include <mysql++.h>

int main()

{

    mysqlpp::String greeting("Hello, world!");

    std::cout << greeting << std::endl;

    return 0;

}

g++ test.cpp -o test.so

./test.so 

猜你喜欢

转载自www.cnblogs.com/renfanzi/p/10266558.html
今日推荐