mysql超时-c/c++

mysql超时-c/c++,c++,mysql,c,C++,Mysql,C,伙计们,我在这段代码中遇到了一个问题,问题是超时,我指的是 程序告诉我服务器是否已连接所需的时间。如果我用我的 localhost我很快就能得到答案,但当我连接到localhost外部时,需要50sc- 响应时间为1.5分钟,程序开始运行,直到完成。我怎样才能修好泡沫,或使它变硬 我自己的超时,比如在50sc后仍在等待,告诉我连接失败并停止 请使用代码作为帮助,因为我会更好地理解它,谢谢我得到的任何帮助 PS:使用MAC #include "mysql.h" #include <stdio

伙计们,我在这段代码中遇到了一个问题,问题是超时,我指的是

程序告诉我服务器是否已连接所需的时间。如果我用我的

localhost我很快就能得到答案,但当我连接到localhost外部时,需要50sc-

响应时间为1.5分钟,程序开始运行,直到完成。我怎样才能修好泡沫,或使它变硬

我自己的超时,比如在50sc后仍在等待,告诉我连接失败并停止

请使用代码作为帮助,因为我会更好地理解它,谢谢我得到的任何帮助

PS:使用MAC

#include "mysql.h"
#include <stdio.h>
#include <stdlib.h>


// Other Linker Flags: -lmysqlclient -lm -lz 

// just going to input the general details and not the port numbers
struct connection_details
{
    char *server;
    char *user;
    char *password;
    char *database;
};

MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
    // first of all create a mysql instance and initialize the variables within
    MYSQL *connection = mysql_init(NULL);

    // connect to the database with the details attached.
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
        printf("Conection error : %s\n", mysql_error(connection));
        exit(1);
    }
    return connection;
}



MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
    // send the query to the database
    if (mysql_query(connection, sql_query))
    {
        printf("MySQL query error : %s\n", mysql_error(connection));
        exit(1);
    }

    return mysql_use_result(connection);
}

int main()
{

        MYSQL *conn;        // the connection
    MYSQL_RES *res; // the results
    MYSQL_ROW row;  // the results row (line by line)

    struct connection_details mysqlD;
    mysqlD.server = (char*)"Localhost";  // where the mysql database is
    mysqlD.user =  (char*)"root";      // the root user of mysql   
    mysqlD.password =  (char*)"123456"; // the password of the root user in mysql
    mysqlD.database =  (char*)"test";  // the databse to pick

    // connect to the mysql database
    conn = mysql_connection_setup(mysqlD);

    // assign the results return to the MYSQL_RES pointer
    res = mysql_perform_query(conn, (char*) "SELECT * FROM me");


    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) !=NULL)
        printf("%s - %s\n", row[0], row[1], row[2]); // <-- Rows

    /* clean up the database result set */
    mysql_free_result(res);
    /* clean up the database link */
    mysql_close(conn);

    return 0;
}

如果您主要关心的是冻结,我认为您的意思是,在进行此连接尝试时,UI没有响应,那么您应该尝试创建一个线程,在后台运行连接代码,同时显示一个对话框,其中显示连接。。。或者是带有取消按钮的东西,这样用户可以在他/她想放弃时放弃。尝试搜索此站点或其他站点-关于如何完成此操作,有很多示例和讨论。

超时在TCP堆栈中-您可能可以做一些调整,但除非你告诉你的平台。我使用Mac作为平台。你确定在本地主机之外实现代码时会更改代码的基础吗?mysqlD.server=char*Localhost;在非本地主机服务器中没有意义此外,当您在另一台主机中实现它时,它是否连接?我的意思是,你说它会冻结一段时间,但它会连接吗?它连接到我的本地主机,但不是外部,它说失败或其他什么,我唯一的问题是冻结如果它只是在远程连接到主机时变慢,那么慢度可能是由服务器正在执行的反向DNS查找造成的。看一看。我很喜欢你们的例子,展示如何去做,这不一定很难,因为我知道你们可以把它做得很好。对不起。我从来没有在Mac上写过一行代码,我不知道你在用什么GUI库,等等。试试谷歌;是你的朋友!StackOverflow也不错!