简体   繁体   English

Linux 上的 Netbeans 连接到 C 中的 MySQL 数据库

[英]Netbeans on Linux to Connect to a MySQL Database in C

Basically, I am trying to connect to a MySQL Database using Connector C drivers for MySQL.基本上,我正在尝试使用 MySQL 的连接器 C 驱动程序连接到 MySQL 数据库。 I am writing the code in Netbeans on a Linux system.我正在 Linux 系统上的 Netbeans 中编写代码。 But here is the mystery: I can connect to my database in Debug Mode, but not Release Mode???但这里有一个谜:我可以在调试模式下连接到我的数据库,但不能在发布模式下连接??? I am using Netbeans as my IDE on Linux, and I set up all the libraries and include folders using the dropdown under project properties so it applies the properties to both the Debug Configuration and Release Configuration, and yet it will connect to the database when I execute in Debug, but not when I execute in Run - ???我在 Linux 上使用 Netbeans 作为我的 IDE,并且我使用项目属性下的下拉菜单设置了所有库并包含文件夹,因此它将属性应用于调试配置和发布配置,但它会在我连接到数据库时在调试中执行,但在运行中执行时不执行 - ??? I simplified the code to show you the actual connection code:我简化了代码以向您展示实际的连接代码:

int readDB(void) {
    MYSQL *mysql = NULL;
    char *server = "localhost";
    char *user = "root";
    char *password = "";
    char *database = "myDB";
    int port = 3306;

    mysql = mysql_init(mysql);

    if (!mysql) {
        puts("Init failed, out of memory?");
        return EXIT_FAILURE;
    } else {
        puts("SUCCESS!\n");
    }

    if (!mysql_real_connect(mysql,server,user,password,database,port,NULL,0)) {
        puts("Connect failed\n");
    }

    return 1;
}

So when I run it in Debug, it prints "SUCCESS" and nothing else.因此,当我在 Debug 中运行它时,它会打印“SUCCESS”而没有其他内容。 When I run it, it prints "SUCCESS" followed by "Connect failed".当我运行它时,它会打印“成功”,然后是“连接失败”。 I am at a loss.我很茫然。 Could this be a problem with the IDE?这可能是IDE的问题吗? Is there another IDE for C that works well on Linux that I should consider?是否有另一个我应该考虑的在 Linux 上运行良好的 C IDE? Or am I missing something quite obvious?或者我错过了一些很明显的东西?

Update: calling mysql_error() revealed更新:调用 mysql_error() 显示

"Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)" 《无法通过socket'/tmp/mysql.sock'连接本地MySQL服务器(2)"

When using a mysql client library in most languages including C, if the hostname is specified as localhost the client library will attempt to connect through a unix domain socket.在包括 C 在内的大多数语言中使用 mysql 客户端库时,如果将主机名指定为localhost则客户端库将尝试通过 unix 域套接字进行连接。 You can force connection using TCP/IP by specifying the hostname as 127.0.0.1您可以通过将主机名指定为127.0.0.1来强制使用 TCP/IP 连接

Yes they are one and the samebut是的,它们是一回事,但是

If unix_socket is not NULL, the string specifies the socket or named pipe to use.如果 unix_socket 不为 NULL,则字符串指定要使用的套接字或命名管道。 Note that the host parameter determines the type of the connection.请注意,host 参数决定了连接的类型。

Alternatively you can examine your mysql configuration to find out where the socket has been created when the server starts and use that as the unix_socket parameter.或者,您可以检查您的 mysql 配置以找出服务器启动时创建套接字的位置,并将其用作 unix_socket 参数。

Third alternative is to change your server configuration to create the socket on /tmp/第三种选择是更改您的服务器配置以在 /tmp/ 上创建套接字

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

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