繁体   English   中英

无法连接到“本地主机”上的MySQL服务器(111)

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

我正在开发一个线程程序,该程序在10个线程中读取mysql数据库,并通过每个线程将结果打印为行数。 因此,最终结果是命令行上的100行计数。 问题是当我运行它时,有时会出现错误,提示Can't connect to MySQL server on 'localhost' (111)有时会返回segmentation fault(core dumped)我检查了线程,并且它们也都创建良好,没有错误。

当我只创建一个线程时,它可以很好地提供预期的输出。 No of Rows of 00 - 6

另一件事是我在红帽服务器上运行此程序,它运行正常,没有错误,但计数错误。我创建了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]);
     }

我不得不经过一个非常艰难的处境才能最终解决问题。 非常简单,只有一个数组声明即可完成。 在我的代码中,在主要功能中。 创建我使用过的线程时,

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]);
}

这是一个有关此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]);
 }

这做得很好。 而且也有一些修改。 我在这里未提及的原因是我认为它们与我的问题无关。 它们是在主线程的末端连接线程时,我用pthread_attr使它们可连接,否则不太正确。 因此,这里仅是最终代码(由于仅在main中进行了更改,因此此处提供了main函数)

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);
}

感谢任何试图帮助自己付出时间的人。...

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以获得参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM