mysql.h的報錯尚未解決,大家不用看了,等我解決了吧還不知道何時html
先用c吧前端
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h> MYSQL *g_conn; // mysql 鏈接 MYSQL_RES *g_res; // mysql 記錄集 MYSQL_ROW g_row; // 字符串數組,mysql 記錄行 #define MAX_BUF_SIZE 1024 // 緩衝區最大字節數 const char *g_host_name = "localhost"; const char *g_user_name = "root"; const char *g_password = "python123"; const char *g_db_name = "c_test"; const unsigned int g_db_port = 3306; void print_mysql_error(const char *msg) // 打印最後一次錯誤 { if (msg) printf("%s: %s\n", msg, mysql_error(g_conn)); else puts(mysql_error(g_conn)); } int executesql(const char * sql) { /*query the database according the sql*/ if (mysql_real_query(g_conn, sql, strlen(sql))) // 若是失敗 return -1; // 表示失敗 return 0; // 成功執行 } int init_mysql() // 初始化鏈接 { // init the database connection g_conn = mysql_init(NULL); /* connect the database */ if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 若是失敗 return -1; // 是否鏈接已經可用 executesql("set names utf8"); // 若是失敗 // return -1; return 0; // 返回成功 } int main(void) { if (init_mysql()); print_mysql_error(NULL); char sql[MAX_BUF_SIZE]; if (executesql("select * from user")) // 句末沒有分號 print_mysql_error(NULL); g_res = mysql_store_result(g_conn); // 從服務器傳送結果集至本地,mysql_use_result直接使用服務器上的記錄集 int iNum_rows = mysql_num_rows(g_res); // 獲得記錄的行數 int iNum_fields = mysql_num_fields(g_res); // 獲得記錄的列數 printf("共%d個記錄,每一個記錄%d字段\n", iNum_rows, iNum_fields); puts("id\tname\n"); while ((g_row=mysql_fetch_row(g_res))) // 打印結果集 printf("%s\t%s\n", g_row[0], g_row[1]); // 第一,第二字段 mysql_free_result(g_res); // 釋放結果集 mysql_close(g_conn); // 關閉連接 return EXIT_SUCCESS; }
g++ -g -o mysql -I /usr/include/mysql main.cpp -L /usr/lib/x86_64-linux-gnu/ -lmysqlclient -lz -lpthreadnode
python調用數據庫,並作邏輯處理,時間爲92.5s,從執行sql到獲得數據150w條爲22s,邏輯處理(2個for循環)爲60s。前端處理30s,pending爲2min左右,須要處理這個問題python
因而思考解決方案: mysql
1. 取數據時數據拆分 linux
#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; }
報錯:ios
/opt/code/testC++/tcp_server/test_server_1/main.cpp:5:10: fatal error: mysql++/mysql++.h: 沒有那個文件或目錄 #include <mysql++/mysql++.h>
系統環境:c++
debian系統kalisql
1. 配置數據庫
debian:
apt-get install libmysqlclient-dev libmysql++-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
4. 添加軟鏈接
此段來自blog:https://www.cnblogs.com/zhxilin/p/5897211.html
install成功後會將.so文件拷貝到/usr/local/lib下,並把.h頭文件拷貝到/usr/local/include下。
到這裏MySQL++已經安裝到本機了,然而若是直接在C++代碼裏引用以下頭文件是沒法編譯經過的!
#include <mysql++.h>
緣由是C++在編譯時須要加載這個動態庫,默認狀況下,g++編譯器只會使用/lib和/usr/lib這兩個目錄下的庫文件。
回頭看一下make以前的./configure步驟,咱們並無指定--prefix=/some/path,因此庫會默認安裝到/usr/local目錄下。
既然libmysqlpp.so是在/usr/local/lib下,編譯器固然就沒法找到它的定義了。
那麼編譯器如何正確找到/usr/local/lib目錄呢?
/etc/ld.so.conf文件記錄了編譯器編譯時使用的動態庫路徑!那咱們把/usr/local/lib路徑加入到文件末尾就能夠了!
次配置文件修改保存後,經過ldconfig程序(在usr/sbin/下),將/etc/ld.so.conf文件列舉的路徑下的庫文件緩存到/etc/ld.so.cache以供開發使用:
[root@localhost mysql++]# ldconfig
而後
[root@localhost mysql++]# ln -s /usr/local/lib/libmysqlpp.so /usr/lib/libmysqlpp.so
然並卵;;;
/usr/local/include/mysql++/common.h:219:11: fatal error: mysql.h: 沒有那個文件或目錄 # include <mysql.h> ^~~~~~~~~
#include <mysql++/mysql++.h> int main() { cout << "hello world" << endl;return 0; }
g++ test.cpp -o test.so
./test.so
這裏還有可能報錯,爲何,有多是你用的編譯器的問題,舉個例子,你用的是clion軟件,而這個軟件默認是cmake的因此咱們須要手動編譯
指定一些東西 :https://www.cnblogs.com/lywy510/p/3615710.html
g++ -g -o mysql.so -I /usr/include/mysql test.cpp -L /usr/lib/x86_64-linux-gnu/ -lmysqlclient -lz
若是是c的話,就是gcc嘍
編譯的時候要注意用到2個路徑,mysql.h和libmysqlclient.so的路徑
查找mysql.h路徑
[root@liu mysql]# find / -name 'mysql.h' /usr/include/mysql/mysql.h
[root@liu mysql]# find / -name '*mysqlclient*'
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0
/usr/lib/x86_64-linux-gnu/libmysqlclient_r.so.18
/usr/lib/x86_64-linux-gnu/libmysqlclient.a
/usr/lib/x86_64-linux-gnu/libmysqlclient_r.a
/usr/lib/x86_64-linux-gnu/libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient_r.so
/usr/lib/x86_64-linux-gnu/libmysqlclient_r.so.18.0.0
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
而後./mysql.so,就能夠啦