zoukankan      html  css  js  c++  java
  • 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 = 3306;
    
    void print_mysql_error(const char *msg)   // 打印最后一次错误
    {
        if (msg)
            printf("%s: %s
    ", 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字段
    ", iNum_rows, iNum_fields);
    
        puts("id	name
    ");
    
        while ((g_row=mysql_fetch_row(g_res))) // 打印结果集
            printf("%s	%s
    ", g_row[0], g_row[1]); // 第一,第二字段
    
        mysql_free_result(g_res); // 释放结果集
    
        mysql_close(g_conn); // 关闭链接
    
        return EXIT_SUCCESS;
    }
    View Code

    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. 取数据时数据拆分  

           先一个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<<"	"<<(*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 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>
               ^~~~~~~~~
    

      

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

    #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,就可以啦

  • 相关阅读:
    软件测试 Lab1 实验报告
    软件测试 Homework2
    谈谈最近的一个让我印象深刻的错误
    Bill Manager Problem Statement
    C#学习记录(九)Windows Phone开发中的Binding
    C#学习记录(八) XML Serializer尝试
    C#学习记录(七)LINQ语句及LAMDA表达式
    C#学习记录(六)
    软件测试之作业三
    软件测试之实验一
  • 原文地址:https://www.cnblogs.com/renfanzi/p/10266558.html
Copyright © 2011-2022 走看看