Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4116973
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: Mysql/postgreSQL

2016-10-11 19:36:27

代码如下


#include
#include


int main(int argc, char **argv)

  MYSQL *con = mysql_init(NULL);


  if (con == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      exit(1);
  }


  if (mysql_real_connect(con, "localhost", "root", "root_pswd", 
          NULL, 0, NULL, 0) == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  } 


  if (mysql_query(con, "CREATE DATABASE testdb")) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }


  mysql_close(con);
  exit(0);
}


gcc -o test test.c  -I/usr/include/mysql/ -lmysqlclient -Wall -g
mysql_real_connect中
If unix_socket is not NULL, the string specifies the socket or named pipe to use. Note that the host parameter determines the type of the connection.




bool MySQLDBThread::connect()
{
  mysql_init(&handle);
  if (!mysql_real_connect(&handle,
                          options->host.c_str(),
                          options->user.c_str(),
                          options->password.c_str(),
                          options->schema.c_str(),
                          options->port,
                          "",
                          CLIENT_MULTI_STATEMENTS))
  {
    fprintf(stderr, "Can't connect to server: %s\n",
            mysql_error(&handle));
    return false;
  }
  return true;
}


If options->host is "localhost" mysql_real_connect() tries to connect to server via unix socket (or other ways to optimize communication on localhost, see documentation). In this case the name of unix socket must me specified in the pre-last argument of mysql_real_connect() which is empty string in the code above.


That is why if --mysql-host command line option is "localhost" percona-playback can not connect to server. The workaround is to use 127.0.0.1 instead of "localhost".




阅读(1686) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~