Howto: Connect MySQL server using C program API under Linux or UNIX

From my mailbag:

How do I write a C program to connect MySQL database server?

MySQL database does support C program API just like PHP or perl.

The C API code is distributed with MySQL. It is included in the mysqlclient library and allows C programs to access a database.

Many of the clients in the MySQL source distribution are written in C. If you are looking for examples that demonstrate how to use the C API, take a look at these clients. You can find these in the clients directory in the MySQL source distribution.

Requirements

Make sure you have development environment installed such as gcc, mysql development package etc. Following is the list summarize the list of packages to compile program:

  • mysql: MySQL client programs and shared library
  • mysqlclient: Backlevel MySQL shared libraries (old libs)
  • mysql-devel: Files for development of MySQL applications (a must have)
  • mysql-server: Mysql server itself
  • gcc, make and other development libs: GNU C compiler

Sample C Program

Following instructions should work on any Linux distro or UNIX computer. Here is the small program that connects to mysql server and list tables from mysql database.(download link):

/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server = "localhost";
   char *user = "root";
   char *password = "PASSWORD"; /* set me first */
   char *database = "mysql";
   conn = mysql_init(NULL);
   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
   /* send SQL query */
   if (mysql_query(conn, "show tables")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
   res = mysql_use_result(conn);
   /* output table name */
   printf("MySQL Tables in mysql database:\n");
   while ((row = mysql_fetch_row(res)) != NULL)
      printf("%s \n", row[0]);
   /* close connection */
   mysql_free_result(res);
   mysql_close(conn);
}

How do I compile and link program against MySQL libs?

MySQL comes with a special script called mysql_config. It provides you with useful information for compiling your MySQL client and connecting it to MySQL database server. You need to use following two options.
Pass --libs option - Libraries and options required to link with the MySQL client library.

$ mysql_config --libs
Output:

-L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto

Pass --cflags option - Compiler flags to find include files and critical compiler flags and defines used when compiling the libmysqlclient library.
$ mysql_config --cflags
Output:

-I/usr/include/mysql -g -pipe -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing

You need to pass above option to GNU C compiler i.e. gcc. So to compile above program, enter:
gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)
Now execute program:
$ ./output-file
Output:

MySQL Tables in mysql database:
columns_priv
db
func
help_category
help_keyword
help_relation
help_topic
host
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user 
时间: 2024-11-06 11:10:07

Howto: Connect MySQL server using C program API under Linux or UNIX的相关文章

(解决)ECSHOP info: Can&#39;t Connect MySQL Server(localhost:3306)!转删

今天打开网站,网站出现 ECSHOP info: Can't Connect MySQL Server(localhost:3306)! 网站一直正常,说明数据库的配置文件应该不会错. 经过查询原来是服务器的更新补丁问题. 原因:微软发布了TCP/IP更新补丁(KB967723),如果服务器开启自动更新或者有自动更新软件下载更新了这个补丁,那么就会出现这个问题. 补丁卸载方法:登录服务器,进入控制面板 --- 添加和删除程序 -- (勾选上方的“显示更新”) 在里面可以看到更新的KB967723

Discuz:can not connect MySQL server

问题描述: 将Discuz里面的upload放在/home/nginx/html里面,修改777权限 安装:http://localhost/bbs/install/index.php 安装第三步报错: can not connect MySQL server ...........though socket '/var/lib/mysql/mysql.sock' 查看mysql配置文件/etc/my.cnf,发现sock是放在/tmp/mysql.sock 解决办法: 创建连接文件 mkdir

Mysql is not allowed to connect mysql server

1.     mysql -u root -p 2.    select host from user where user='root';      //可以看到当前主机配置信息为localhost. 3. update user set host = '%' where user ='root'     如果2的列表不唯一,就需要后面添加条件 4.  FLUSH PRIVILEGES;       刷新权限列表 原文地址:https://www.cnblogs.com/chengyangya

mysql链接错误:2003 can&#39;t connect to mysql server on 10038

出现这个错误原因是端口号不是3306. 打开D:\Program Files\MySQL\MySQL Server 5.5 \my.ini文件,当然还有其他的.ini的文件: [client] port=3306 [mysql] default-character-set=utf8 # SERVER SECTION# ----------------------------------------------------------------------## The following opt

MySql数据库:Host &#39;localhost&#39; is not allowed to connect to this MySQL server

修改mysql的root密码后,出现Host 'localhost' is not allowed to connect to this MySQL server 错误. 解决办法: C:\Program Files\MySQL\MySQL Server 5.5\my.ini 在[mysqld]下加下面两行, skip-name-resolveskip-grant-tables 重启mysql的windows服务 MySql数据库:Host 'localhost' is not allowed

Navicat for Mysql2003-can&#39;s connect to MySQL server on&#39;localhost&#39;10061的解决

Navicat for Mysql2003-can's connect to MySQL server on'localhost'10061的解决 环境:Navicat for Mysql 8.2 + MySQL Sever 5.1 问题:通过MySQL命令行能连接,但是通过Navicat for Mysql 8.2却连不上,出现如下错误: 2003 - can's connect to MySQL server on 'localhost' 10061 解决: 删除安装目录下的my.ini,然

1130 - Host &#39;localhost&#39; is not allowed to connect to this MySQL server

打开Win7上的Navicat for MySQL后,连接localhost时,提示错误: 其解决方法是: 打开c:/Program Files/MySQL/MySQL Server 5.5下的my.ini文件,查看是否在在[mysqld]的段中存在: skip-grant-tables . 如果没有,添加即可. 目的是跳过MySQL的访问控制,任何人都可以在控制台以管理员的身份进入MySQL数据库. 注意: 在修改完密码以后要把MySQL服务器停掉重新启动才会生效.其方法是在win7中,打开控

MySQL问题记录--Can&#39;t connect to MySQL server on localhost (10061)解决方法

本文mysql的安装环境为win7 64位,mysql版本为MySQL5.7 问题描述:在命令行输入 mysql -u root -p 登录mysql,返回"Can't connect to MySQL server on localhost (10061)"错误 问题原因:在一番谷歌.百度后,查到问题原因是mysql没有启动. 解决方法:1.将mysql加入到Windows的服务中.切换到mysql安装目录下的bin文件夹,命令行运行"mysqld --install&qu

解决 2003 Can’t connect to MySQL server on ‘localhost’ (10048)

2003 Can’t connect to MySQL server on ‘localhost’ (10048)一般见于使用mysql的windows 2003服务器.错误的出现的原因: 第一种原因:应用程序需要快速释放和创建新连接, 但是由于 TIME_WAIT 中存在的连接超过默认值,导致较低吞吐量.解决方案:和本错误密切相关的两个windows的注册表项:TcpTimedWaitDelay和MaxUserPort的值.TcpTimedWaitDelay 确 定 TCP/IP 可释放已关闭