can't connect to mysql server on 'localhost'(10061)

ChandlerSQLee 2015-06-16 08:16:24
#include "stdafx.h"

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>

#define STEPBYSTEP


#pragma comment(lib,"libmysql.lib")//连接MysQL需要的库
using namespace std;
int main()
{
const char user[] = "root"; //username
const char pswd[] = "123456"; //password
const char host[] = "localhost"; //or"127.0.0.1"
const char table[] ="electricline"; //database
unsigned int port = 3306; //server port
MYSQL myCont;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[32][32];
int res;
mysql_init(&myCont);

#ifdef STEPBYSTEP
system("pause");
#endif



if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))
{
cout<<"connect succeed!"<<endl;
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
res=mysql_query(&myCont,"select * from electricline");//查询
if(!res)
{
result=mysql_store_result(&myCont);//保存查询到的数据到result
if(result)
{
int i,j;
cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i<j;i++)
{
printf("%s\n",sql_row[i]);
}
printf("\n");
}
}
}
else
{
cout<<"query sql failed!"<<endl;
}
}
else
{
cout<<mysql_error(&myCont)<<endl;
cout<<"connect failed!"<<endl;
}
if(result!=NULL) mysql_free_result(result);//释放结果资源
mysql_close(&myCont);//断开连接
return 0;
}

mysql是开启的,但就出现can't connect to mysql server on 'localhost'(10061), 不知道怎么回事,请各位帮帮忙!
...全文
206 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChandlerSQLee 2015-06-29
  • 打赏
  • 举报
回复
可能是mysq.lib调用错误,在VS2010上改用32位的库就解决问题,但在VC++6.0中仍然未解决该问题,不知道为什么。
ChandlerSQLee 2015-06-16
  • 打赏
  • 举报
回复
还有其它建议吗?我在网上搜索了好多种方法都不行,怎么去查找原因呢?目前我用的操作系统WIN7,mysql是5.6.12
老王爱上猫 2015-06-16
  • 打赏
  • 举报
回复
http://www.jb51.net/article/26505.htm

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧