好的,所以我的问题非常简单,我对C ++比较陌生,所以这对某些人来说可能是一个简单的答案,但我使用libmysql连接到数据库,到目前为止它的工作方式也正如我所需要的那样。但我希望能够连接到本地主机上找不到的数据库,数据库仍然在我的局域网上但不在本地机器上。我很确定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"
答案 0 :(得分:1)
通常,如果您在通过IP连接时遇到问题,那么您的用户帐户将无法通过“远程”连接进行连接。地址。
通常这可以通过以下方式解决:
GRANT ALL PRIVILEGES ON *.* TO `user`@`%` IDENTIFIED BY '...'
user
是您的用户名。显然,您可能希望更多地锁定访问权限。
localhost
用于&#34;本地&#34;通过UNIX套接字连接。所有其他人都被视为远程。