[問題] 有關MySQL存取

看板C_and_CPP (C/C++)作者 (^^!!)時間16年前 (2009/03/24 22:36), 編輯推噓4(408)
留言12則, 3人參與, 最新討論串1/1
我在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

03/25 02:12, , 1F
資料庫的權限?
03/25 02:12, 1F

03/25 09:07, , 2F
printf("connect fail: %s\n", mysql_error(&mysql_conn))
03/25 09:07, 2F

03/25 09:20, , 3F
可是我是用root登入~有權限的限制嗎~localhost就可
03/25 09:20, 3F

03/25 09:47, , 4F
有 localhost可以進是因為root@localhost這個使用者存在
03/25 09:47, 4F

03/25 09:49, , 5F
但是root@'your ip'不存在 這是我的想法 你可以先grant一下
03/25 09:49, 5F

03/25 15:59, , 6F
Connection fails:Access denied for user 'root'@'
03/25 15:59, 6F

03/25 15:59, , 7F
'turtlertes-desktop.local' (using password: YES)
03/25 15:59, 7F

03/25 16:00, , 8F
他出現了以上的錯誤~密碼是對的~大大有什麼方法
03/25 16:00, 8F

03/25 16:09, , 9F
用grant命令開權限給root@"your ip"
03/25 16:09, 9F

03/25 16:42, , 10F
就是用這個方法出現以上的錯誤
03/25 16:42, 10F

03/25 17:05, , 11F
我自己試是可以..@@
03/25 17:05, 11F

03/25 17:09, , 12F
那我再試看看好了~~謝謝大大
03/25 17:09, 12F
文章代碼(AID): #19oE_a18 (C_and_CPP)
文章代碼(AID): #19oE_a18 (C_and_CPP)