debian系列下c++调用mysql, linux下面安装mysql.h文件
mysql.h的报错还没有解决,你们不用看了,等我解决了吧还不知道什么时候
先用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 = ; 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 -; // 表示失败 return ; // 成功执行
} 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, )) // 如果失败
return -; // 是否连接已经可用
executesql("set names utf8"); // 如果失败
// return -1;
return ; // 返回成功
} 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[], g_row[]); // 第一,第二字段 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 -lpthread
1.介绍需求:
python调用数据库,并做逻辑处理,时间为92.5s,从执行sql到得到数据150w条为22s,逻辑处理(2个for循环)为60s。前端处理30s,pending为2min左右,需要处理这个问题
于是思考解决方案:
1. 取数据时数据拆分
#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 -;
}
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 -;
} for(auto iter=result.begin();iter!=result.end();++iter){
cout<<"\t"<<(*iter)[]<<endl;
} return ;
}
报错:
/opt/code/testC++/tcp_server/test_server_1/main.cpp::: fatal error: mysql++/mysql++.h: 没有那个文件或目录
#include <mysql++/mysql++.h>
2. 怎么安装<mysql++/mysql++.h>头文件
系统环境:
debian系统kali
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..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>
^~~~~~~~~
3.执行c++代码执行测试
#include <mysql++/mysql++.h> int main() { cout << "hello world" << endl;return ; }
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,就可以啦
debian系列下c++调用mysql, linux下面安装mysql.h文件的更多相关文章
- Linux中安装MySQL
因为使用yum安装.安装过程需保证网络通畅 一.安装mysql 1.yum安装mysqlCentOS7默认数据库是mariadb,配置等用着不习惯,因此决定改成mysql,但是CentOS7的yum源 ...
- 怎样在 Ubuntu Linux 上安装 MySQL
本教程教你如何在基于 Ubuntu 的 Linux 发行版上安装 MySQL.对于首次使用的用户,你将会学习到如何验证你的安装和第一次怎样去连接 MySQL. -- Sergiu MySQL 是一个典 ...
- linux上安装 mysql
一.linux 上安装 mysql 1.查看mysql是否安装 rpm -qa|grep mysql 2.卸载 mysql yum remove mysql mysql-server mysql-li ...
- Linux 上安装 mysql
1.通过 yum 命令安装 mysql 可以先通过 yum list |grep mysql 方式查看有哪些版本的 mysql 2.安装 mysql yum install mysql-server ...
- MySQL介绍及安装&MySQL软件基本管理
mysql介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好 ...
- 记录下在阿里云linux上安装与配置Mysql
环境:阿里云ECS服务器,系统为centos7.2 用户:root 参考博客:https://blog.csdn.net/kunzai6/article/details/81938613 师兄的哈哈哈 ...
- linux(Centos7)安装mysql
查看系统环境 [root@localhost html]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) CentOS 7 ...
- linux上安装mysql
linux下mysql 最新版安装图解教程 1.查看当前安装的linux版本 命令:lsb_release -a 如下图所示 通过上图中的数据可以看出安装的版本为RedHat5.4,所以我们需要下载R ...
- 亲测linux上安装mysql
1.rpm -ivh MySQL-server-5.6.19-linux_glibc2.5.x86_64.rpm(这是复制过来的,用Tab键自动补齐吧)2.rpm -ivh MySQL-client- ...
随机推荐
- hdu4035 Maze
题目链接 hdu4035 Maze 题解 f[u]表示在节点u通关的所需的边数期望 转移方程分叶子节点和非叶子点讨论 发现都可以化成f[x]=af[1]+bf[dad]+c的形式 然后推一下系数 还是 ...
- git配置SSH Key,上传本地代码至github
git配置全局的name和email git config --global user.name "name" git config --global user.email &qu ...
- 喵哈哈村的魔法考试 Round #13 (Div.2) 题解
喵哈哈村的木星传说(一) 旋转90°,找找规律就知道(x,y)->(n-1-y,x) 然后输出就好了. #include<bits/stdc++.h> using namespace ...
- epoll惊群原因分析
考虑如下情况(实际一般不会做,这里只是举个例子): 在主线程中创建一个socket.绑定到本地端口并监听 在主线程中创建一个epoll实例(epoll_create(2)) 将监听socket添加到e ...
- autorelease' is unavailable
ARC forbids explicit message send of'release' 'release' is unavailable: not available inautomatic re ...
- IDEA使用笔记(六)——设置项目的JDK配置
1:由于dev分支和master分支的代码差异比较多,所以,就从master上分出一个新的分支dev_,于是我就克隆新的代码,打开对应的项目文件,然后启动试试,发现报出如下的错误,很明显是因为没有制定 ...
- Orace 12.2 ORA-12012: error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_21"
一个测试环境的12.2.0.1数据库后台alert不断报出以下错误信息: Errors in file /d12/app/oracle/diag/rdbms/test/test/trace/test_ ...
- django --- DetailView源码分析
[背景] 最近在看django官方文档的class-based-views这一节的时候一直不得要领,感觉自己清楚,但是回想起来又没有脉络:于是没有办法只 能是“暗中观察”django的源码了. 刚打开 ...
- Xilinx 常用模块汇总(verilog)【02】
作者:桂. 时间:2018-05-08 18:35:56 链接:http://www.cnblogs.com/xingshansi/p/9010282.html [本文遗留几处细节问题,待闲下来解决 ...
- css min-width和max-width
min-width: 浏览器缩小设置min-width,元素最小也是min-width设置的值.设置min-width元素不会压扁. max-width:元素最大宽度