在windows控制台测试通过的代码如下
本地连接正常,且访问数据库正常。远程连接,数据库的名称和用户名密码这些都没有错。可以运行到"sock handle ok!"却无法得到数据库内的内容。
PS:数据库是本地导入的,没有什么不一样的。
已添加可以访问的主机IP地址。
最下面附WEB服务器关于MYSQL添加主机的说明:
#include  <windows.h> 
#include  <iostream> 
#include  <mysql.h> 
#include  <stdio.h> 
using namespace std;
int main() 

//connection params 
 FILE *file; 
 char *host = "localhost"; 
 char *user = "root"; 
 char *pass = "123456"; 
 char *db = "mysql";  //sock 
 MYSQL *sock; 
 MYSQL_RES *results; 
 MYSQL_ROW record; 
  
 sock = mysql_init(0); 
 if (sock) cout  << "sock handle ok!"  << endl; 
 else { 
 cout  << "sock handle failed!" << mysql_error(sock)<< endl; 
 } 
 //connection 
 if (mysql_real_connect(sock, host, user, pass, db, 3306, NULL, 0)) 
 cout  << "connection ok!"  << endl;
 else { 
 cout << "connection fail: " << mysql_error(sock)<< endl; 
 } 
 mysql_set_character_set(sock,"gb2312"); 
 //connection character set 
 cout  << "connection character set: " << mysql_character_set_name(sock) << endl; 
 //wait for posibility to check system/mysql sockets  if(mysql_query(sock,"select * from user")) 
 { 
 cout <<"very good!" <<endl; 
 }  //links=mysql_fetch_row(sock); results=mysql_store_result(sock);
 printf("user\tlevel\n"); 
 while(record=mysql_fetch_row(results)) 
 { 
 printf("%s\t %s\n",record[1],record[3]); 
  
 } 
 mysql_free_result(results); 
 system("PAUSE"); 
  
 //closing connection 
 mysql_close(sock);
 return EXIT_SUCCESS;
}注:以下内容是从WEB服务器复制来的:
远程数据库 访问主机你可以将外部网络服务器的域名添加到能够访问你网站上数据库的主机列表,从而让外部网络服务器访问你的 MySQL 数据库。 本地服务器是指你的网站所在的服务器。
添加访问主机主机(可使用%通配符):
访问主机  移除
218.107.x.x

删除
NOTE - To remotely access your databases, you must allow the connection through our firewall as well. You can add your current IP (or Class C) in the Customer Preferences section of the Customer Profile
Help!What Is Remote MySQL?Remote MySQL allows external web servers to access your MySQL databases by adding their domain name to the list of hosts that are able to access databases on your web site.Add Access HostsThis functions allows you to add external hosts that are allowed to connect to a MySQL Databases. Host should be ab IP Address or a domain name.Access HostsAll hosts that have been added as allowed to connect to a database hosted on your server will be listed here. You can delete hosts from this list here.

解决方案 »

  1.   

    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
    GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
      

  2.   

    连接局域网内的同一段的IP的MYSQL正常,可以正常访问数据
      

  3.   

    想请教下GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
    这句语句加在哪里呢
      

  4.   

    知道了,在MYSQL命令行中
    试下
      

  5.   


    在远程WEB服务器上的phpmyadmin执行出错:SQL 查询:GRANT ALL PRIVILEGES ON links . * TO youroot@ "%"IDENTIFIED BY "123456"MySQL 返回:文档
    #1044 - Access denied for user 'youroot'@'localhost' to database 'links' 
      

  6.   

    搞定了原来
    NOTE - To remotely access your databases, you must allow the connection through our firewall as well. You can add your current IP (or Class C) in the Customer Preferences section of the Customer Profile
    不但要在这上面设置IP地址还要点
     Customer Profile(是个链接)进去设置--->Customer Preferences在这里添加上
    和主机的设置有关,谢谢了.PS:他们的PHPMYADMIN是不允许远行上面刚才的命令的