作者howdieturtle (^^!!)
看板C_and_CPP
标题[问题] 有关MySQL存取
时间Tue Mar 24 22:36:19 2009
我在linux下写了一个程式想去存取MySQL中的资料
可是当我的Server是利用localhost时就可以连线并取得
而使用本地端的ip时就会有Connection fails
不知道是什麽问题,也试着去mysql的conf档将bind-address改成自己本地端的ip
还是不行,不知各位大大有什麽方法
/*gcc mysql_int.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
-o mysql_int.cgi*/
#include <stdio.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL mysql_conn; /* Connection handle */
MYSQL_RES *mysql_result; /* Result handle */
MYSQL_ROW mysql_row; /* Row data */
char *server="192.168.44.128";
int f1, f2, num_row, num_col;
printf("Content-type:text/html%c%c",10,10);
if (mysql_init(&mysql_conn) != NULL)
{
if (mysql_real_connect(&mysql_conn, server, "root",
"123456", "test", 0, NULL, 0) != NULL)
{
if(mysql_query(&mysql_conn,"select * from test1")== 0)
{
mysql_result = mysql_store_result(&mysql_conn);
num_row = mysql_num_rows(mysql_result);
/* Get the no. of row */
num_col = mysql_num_fields(mysql_result);
/* Get the no. of column */
printf("row=%d, col=%d\n",num_row,num_col);
for (f1 = 0; f1 < num_row; f1++)
{
mysql_row = mysql_fetch_row(mysql_result);
for (f2 = 0; f2 < num_col; f2++)
{
/* Fetch one by one */
printf("[Row %d, Col %d] ==> [%s]<br>", f1, f2,
mysql_row[f2]);
}
}
} else
{
(void) printf("Query fails\n");
}
} else
{
(void) printf("Connection fails\n");
}
} else
{
(void) printf("Initialization fails\n");
}
mysql_free_result(mysql_result);
mysql_close(&mysql_conn);
printf("quit\n");
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.116.177.27
1F:→ vintw:资料库的权限? 03/25 02:12
2F:推 lausai:printf("connect fail: %s\n", mysql_error(&mysql_conn)) 03/25 09:07
3F:→ howdieturtle:可是我是用root登入~有权限的限制吗~localhost就可 03/25 09:20
4F:推 lausai:有 localhost可以进是因为root@localhost这个使用者存在 03/25 09:47
5F:推 lausai:但是root@'your ip'不存在 这是我的想法 你可以先grant一下 03/25 09:49
6F:→ howdieturtle:Connection fails:Access denied for user 'root'@' 03/25 15:59
7F:→ howdieturtle:'turtlertes-desktop.local' (using password: YES) 03/25 15:59
8F:→ howdieturtle:他出现了以上的错误~密码是对的~大大有什麽方法 03/25 16:00
9F:推 lausai:用grant命令开权限给root@"your ip" 03/25 16:09
10F:→ howdieturtle:就是用这个方法出现以上的错误 03/25 16:42
11F:→ lausai:我自己试是可以..@@ 03/25 17:05
12F:→ howdieturtle:那我再试看看好了~~谢谢大大 03/25 17:09