您的位置:首页 > 数据库 > MySQL

mysql out of memory needed 8164 bytes 等问题

2013-11-22 13:27 369 查看
c++ 连接mysql。

#include “mysql.h”

int main ()

{

MYSQL conn;

mysql_init(&conn);

if (mysql_real_connect(&conn,"172.16.66.1","xaf","111111","bai",0,NULL,0)

{

  cout<<"success!"<<endl;

}

else

{

  cerr<<"failed"<<endl;

  if (mysql_errno(&conn))

  {    cout<<mysql_errno(&conn)<<" "<<mysql_error(&conn);}

}

std::string sql="select * from ss ";

int sq=mysql_query(&conn,sql);

if (!sq){

  res_ptr=mysql_store_result(&conn);

  if(res_ptr)

  { while(sqlrow=mysql_fetch_row(res_ptr)){..} }

mysql_free_result(res_ptr); 这句话特别重要,

mysql_close(&conn);

}

g++ -g -c test.cpp

g++ -o test test.o -lmysqlclient

1.查询结果要释放。

以前我编程序只要能跑出来就好了,不需要考虑程序的持久性,但是自从遇到比较大的项目,数据量很大,要循环很多次查询数据库,程序甚至需要运行几天。这样我就遇到了这里所说的错误,mysql out of memory needed 8164 bytes. 而且我都不知到错在哪。

以后编程要知道收尾,每创建一个对象,每使用一次内存,想着是不是要在该释放的时候释放。

p2. errno:2003,error:can't connect to mysql on server "172..." (111) :

最终解决:因为我有多个线程,每个线程里创建MYSQL connect,建立连接,一开始没有对建立连接的语句加锁,后来加锁后,就可以了:

pthread_mutex_t m_mutex();

pthread_mutex_lock(&m_mutex);

mysql_init(&conn);

if (mysql_real_connect(&conn,"172.16.66.1","xaf","111111","bai",0,NULL,0)

{

  cout<<"success!"<<endl;

}

else

{

  cerr<<"failed"<<endl;

  if (mysql_errno(&conn))

  {    cout<<mysql_errno(&conn)<<" "<<mysql_error(&conn);}

}

pthread_mutex_unlock(&m_mutex);

p3. 多线程运行程序,如果不在每个线程函数结束加mysql_close(&conn) ;之前调试运行时,每次都没有释放连接,会出现error:too many connections.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: