0
回答
线程中链接MySQL数据库出错#Error 2003:Can't connect to MySQL server on 'localhost' (111)

在线程中链接数据库出现错误

mysql_real_connect error
#Error 2003:Can't connect to MySQL server on 'localhost' (111)

不再线程中则可以成功链接,这是什么问题呢?难道

mysql_real_connect
是非线程安全的吗?

void *connect_db(void *arg);
int main(int argc, const char *argv[])
{
    pthread_t   tid;
    int     i;

    for (i = 0; i < 10; i++)
        pthread_create(&tid, NULL, connect_db, NULL);
        
    pause();
    return 0;
}

void *connect_db(void *arg){
    MYSQL   mysql;

    if (mysql_init(&mysql) == NULL)
        err_exit(&mysql, "mysql_init() error");

    if (mysql_real_connect(&mysql, "localhost", "user", "password", "testdb", 0, NULL,0) == NULL)
        err_exit(&mysql, "mysql_real_connect error");

    printf("connect to mysql success\n");
    fflush(stdout);
}





举报
锋了
发帖于4年前 0回/737阅
顶部