VC++远程连接MYSQL(连接本地MYSQL正常)
在windows控制台测试通过的代码如下
本地连接正常,且访问数据库正常。
远程连接,数据库的名称和用户名密码这些都没有错。可以运行到"sock handle ok!"却无法得到数据库内的内容。
PS:数据库是本地导入的,没有什么不一样的。
已添加可以访问的主机IP地址。
最下面附WEB服务器关于MYSQL添加主机的说明:
[code=c/c++]
#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;
}
[/code]
注:
以下内容是从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 Hosts
This 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 Hosts
All 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.