c++ - libmysql mysql_real_connect 因 IP 地址而失败,但适用于 localhost

标签 c++ mysql

好吧,我的问题很简单,我是 C++ 的新手,所以这对某些人来说可能是一个简单的答案,但我正在使用 libmysql 连接到数据库,到目前为止它也能正常工作,我也需要它。但是我希望能够连接到在本地主机上找不到的数据库,该数据库仍将位于我的 LAN 上,但不在本地计算机上。我很确定 unix_socket 参数是我需要更改的所有参数,但我不知道如何或将其更改为什么或如何制作“unix_socket”。下面我将发布我的代码并提前感谢您的帮助谢谢!

void readFromDB(){
skip:
     MYSQL *connect; // Create a pointer to the MySQL instance
connect=mysql_init(NULL); // Initialise the instance
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
if(!connect)    /* If instance didn't initialize say so and exit with fault.*/
{
    fprintf(stderr,"MySQL Initialization Failed");


}
/* Now we will actually connect to the specific database.*/

connect=mysql_real_connect(connect,"10.1.3.253",USER,PASSWORD,DATABASE,3306, NULL,0);
/* Following if statements are unneeded too, but it's worth it to show on your
first app, so that if your database is empty or the query didn't return anything it
will at least let you know that the connection to the mysql server was established. */

if(connect){
    printf("First Connection Succeeded\n");
            system("PAUSE");
}
else{
    printf("Connection Failed!\n");
            Sleep(800);
            goto skip;
}
MYSQL_RES *result; /* Create a pointer to recieve the return value.*/
MYSQL_ROW row;  /* Assign variable for rows. */

//mysql_query(connect,"SELECT * FROM locationTime");
/* Send a query to the database. */
    if(!(mysql_query(connect,"SELECT * FROM locationtime") == 0)){
            mysql_close(connect);
            Sleep(300);
            goto skip;
    }
result = mysql_store_result(connect); /* Receive the result and store it in res_set */


unsigned int numrows = mysql_num_rows(result); /* Create the count to print all rows */

/* This while is to print all rows and not just the first row found, */
    if(numrows != 0){
            while ((row = mysql_fetch_row(result)) != NULL){
                    ids.push_back(row[0]);
                    dateTime.push_back(row[1]);
                    locs.push_back(setLocation(row[2]));
                    strLocs.push_back(row[2]);
                    imgPaths.push_back(row[3]);

                    //for(int i = 0; i<(sizeof(row)  ); i++){
                    //        //printf("%s\n",row[i] != NULL ? row[i] : "NULL"); /* Print the     row data */
                    //}
                    //cout<<endl<<endl;
            }
    }else if(numrows <= 0){
            mysql_close(connect);   /* Close and shutdown */
            mysql_free_result(result);
            Sleep(200);
            goto skip;
    }
mysql_close(connect);   /* Close and shutdown */
    mysql_free_result(result);
return;
}

这是我的头文件:

#include "my_global.h" // Include this file first to avoid problems
#include "mysql.h" // MySQL Include File
#define SERVER "10.1.3.253"
#define USER "user"
#define PASSWORD "pass"
#define DATABASE "DBName"

最佳答案

通常,如果您在通过 IP 连接时遇到问题,那是因为您的用户帐户不允许从“远程”地址连接。

通常这是通过以下方式解决的:

GRANT ALL PRIVILEGES ON *.* TO `user`@`%` IDENTIFIED BY '...'

user 是您的用户名。显然,您可能希望更多地锁定访问权限。

localhost 用于通过 UNIX 套接字进行“本地”连接。所有其他的都被视为远程。

关于c++ - libmysql mysql_real_connect 因 IP 地址而失败,但适用于 localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615075/

相关文章:

php - WordPress 查询拥有 2 个相等元的用户

php - mySQL SELECT 在日期和条件之间

mysql - 根据时间间隔查询获取计数值

c++ - 在openGL(版本> 3.3)中显示在屏幕上时如何改变纹理的大小?

php - 将 PHP 的不同变量输入接收到 C++ 中

c++ - 如何从另一个线程唤醒没有超时时间的 Select 调用

c# - 什么决定了最大纹理尺寸?

c++ - libstdc++ 在 ubuntu 64 位中崩溃

mysql - 许多在mysql进程上运行的 sleep 查询

mysql - 我得到 Cannot add or update a child row : a foreign key constraint fails error