无法连接到 'localhost' 上的 MySQL 服务器 (111)

Can't connect to MySQL server on 'localhost' (111)

我正在开发一个线程程序,它在 10 个线程中读取 mysql 数据库,并将结果打印为每个线程的行数。所以最终结果是命令行上有 100 行计数。问题是当我 运行 它有时会给出一个错误说 Can't connect to MySQL server on 'localhost' (111) 有时 returns a segmentation fault(core dumped) 我检查了线程,它们也创建得很好,没有错误。

当我只创建一个线程时,它工作得很好,给出了预期的输出。 No of Rows of 00 - 6

另一件事是我 运行 这是在红帽服务器上运行的,没有错误,但计数是 wrong.I 创建了 5 个线程,第一个线程 52380 是正确但对于其他人它给出了不同的结果结果应该是这样的

No of Rows  of 00 - 52380
No of Rows  of 01 - 53434
No of Rows  of 02 - 53333
No of Rows  of 03 - 50005
No of Rows  of 04 - 48393

但实际出来的结果是这样的

No of Rows  of 00 - 52380
No of Rows  of 00 - 52380
No of Rows  of 01 - 52380
No of Rows  of 01 - 52380
No of Rows  of 01 - 52380

我用

编译这个
gcc main.c -lpthread `mysql_config --cflags --libs`

这个问题的原因是什么。谁能帮我解决这个问题。下面是我使用的代码。这里不给出头文件。它包含数据库详细信息。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main1.h"
#include <pthread.h>
#include <mysql/mysql.h>
#include <mysql/my_global.h>

int main(int argc, char** argv) {

   int threadRet[10], i = 0;
   pthread_t * threadT = (pthread_t *) malloc(10 * sizeof (pthread_t));

   for (i = 0; i < 10; i++) {
        threadRet[i] = pthread_create(&threadT[i], NULL, threadedConnection, &i);
        printf("thread %d - thread ret %d\n",i,threadRet[i]);
   }
   i = 0;
   for (i = 0; i < 10; i++) {
        pthread_join(threadT[i], NULL);
   }
   return (EXIT_SUCCESS);
 }



void * threadedConnection(void * parr) {

    int * j = (int *) parr;
    int tableNo=*j;
    long int rowCount = 0;
    long int totalInserts = 0;

    MYSQL *con1 = mysql_init(NULL);
    MYSQL_RES *result;
    MYSQL_ROW row;
    con1[tableNo] = mysql_init(NULL);
    if (mysql_real_connect(con1, dataBase1[0], dataBase1[1], dataBase1[2], dataBase1[3], 0, NULL, 0) == NULL) {
        fprintf(stderr, "%s\n", mysql_error(con1));
        mysql_close(con1);
        exit(1);
     }

     char countQuery[70];
     sprintf(countQuery, "SELECT COUNT(*) FROM numberTable where number like '%s%.2d'", "%", *tableNo);

     if (mysql_query(con1[tableNo], countQuery)) {
        fprintf(stderr, "%s\n", mysql_error(con1));
        mysql_close(con1);
        exit(1);
     }

     result = mysql_store_result(con1);       //Counting
     row = mysql_fetch_row(result[tableNo]);  //line
     rowCount = strtol((row)[0], NULL, 10);   //numbers
     totalInserts = rowCount;                 //numberTable
     mysql_free_result(result[tableNo]);

     printf("No of Rows  of %.2d - %ld\n", tableNo, totalInserts);



     mysql_close(con1[tableNo]);
     }

if (mysql_real_connect(con1, dataBase1[0], dataBase1[1], dataBase1[2], dataBase1[3], 0, NULL, 0) == NULL) {
          ^ port

您需要通过端口,默认为 3306。检查主机名和其他参数。

参考http://dev.mysql.com/doc/refman/5.7/en/mysql-real-connect.html

我不得不经历一个非常艰难的情况才能最终达到目的。它非常简单,只需一个数组声明即可完成。在我的代码中,在 main 函数中。创建我使用过的线程时,

for (i = 0; i < 10; i++) {
    threadRet[i] = pthread_create(&threadT[i], NULL, threadedConnection, &i);
    printf("thread %d - thread ret %d\n",i,threadRet[i]);
}

这是作者 link 说的 Pthreads

问题是,当数组执行它们的工作时,它们使用传递的整数 i 并且由于整数的地址被传递并且在 for 循环中它不断增加它的值。因此,当线程尝试使用它时,它已经增加并不断增加。所以它被卡住了。

所以为了克服它,我所做的是我声明了一个数组

 for (i = 0; i < threadCount; i++) {
    tbl[i] = i;
 }
 i == 0;

并且这个数组元素是在创建线程时传递的

 for (i = 0; i < 10; i++) {
    threadRet[i] = pthread_create(&threadT[i], NULL, threadedConnection, &tbl[i]);
    printf("thread %d - thread ret %d\n",i,threadRet[i]);
 }

这很好。并且也有一些较少的修改。我没有在这里提到,因为我相信它们与我的问题无关。他们在 main 末尾加入线程时,我使用 pthread_attr 使它们可以加入,否则不太正确。所以这只是最终代码(这里给出了主要功能,因为仅在主要部分进行了更改)

int main(int argc, char** argv) {

    int threadRet, i = 0;
    int tbl[threadCount];
    void *status;
    pthread_attr_t attr;

    for (i = 0; i < threadCount; i++) {
    tbl[i] = i;
    }
    i == 0;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    pthread_t * threadT = (pthread_t *) malloc(threadCount * sizeof (pthread_t));

    for (i = 0; i < threadCount; i++) {
    threadRet = pthread_create(&threadT[i], &attr, threadedConnection, &tbl[i]);
        if (threadRet) {
            printf("Error creating thread return value of pthread_create() is %d", threadRet);
        }
        printf("thread %d - thread ret %d\n", i, threadRet);
    }
    i = 0;

    pthread_attr_destroy(&attr);

    for (i = 0; i < threadCount; i++) {
        threadRet = pthread_join(threadT[i], &status);
        if (threadRet) {
            printf("Error joining thread return value of pthread_join() is %d", threadRet);
        }

    }

    free(threadT);
    pthread_exit(NULL);
    return (EXIT_SUCCESS);
}

感谢任何试图帮助贡献时间的人....