debian系列下c++呼叫mysql, linux下面安裝mysql.h檔案
阿新 • • 發佈:2019-01-14
1.介紹需求:
python呼叫資料庫,並做邏輯處理,時間為92.5s,從執行sql到得到資料150w條為22s,邏輯處理(2個for迴圈)為60s。前端處理30s,pending為2min左右,需要處理這個問題
於是思考解決方案:
1. 取資料時資料拆分
先一個count返回條目,然後多次查詢 2. 邏輯部分怎麼辦? 我想用c++處理試試,於是找程式碼,打算用python呼叫c++處理試試 示例:#include <iostream> #includeView Code<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; }
報錯:
/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. 下載解壓安裝:
[[email protected] 下載]#wget https://tangentsoft.com/mysqlpp/releases/mysql++-3.2.4.tar.gz
[[email protected] 下載]# 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
[email protected]:/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