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)) {
fprintf(stderr, "%s\n", mysql_error(conn));
);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
]);
/* 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
Howto: Connect MySQL server using C program API under Linux or UNIX的更多相关文章
- (二)阿里云ECS Linux服务器外网无法连接MySQL解决方法(报错2003- Can't connect MySQL Server on 'x.x.x.x'(10038))(自己亲身遇到的问题是防火墙的问题已经解决)
我的服务器买的是阿里云ECS linux系统.为了更好的操作数据库,我希望可以用navicat for mysql管理我的数据库. 当我按照正常的模式去链接mysql的时候, 报错提示: - Can' ...
- ECSHOP info: Can't Connect MySQL Server(localhost:3306)!
今天对服务器压力测试后,某个公司电脑的页面显示标题所示,别人电脑正常.故让她清理cookie.无效.重启MySQL后问题解决.
- 最近使用Navicat for MySQl访问远程mysql数据库,出现报错,显示“2003- Can't connect MySQL Server on 'localhost'(10038)“。
优先考虑mysql数据库是否开启 1.先看报错窗口. 通过百度,最终找到的原因是:远程3306端口未对外开放. 于是下面进行远程3306端口开放操作. 首先远程连接服务器,点击“开始”-“管理 ...
- Mysql is not allowed to connect mysql server
1. mysql -u root -p 2. select host from user where user='root'; //可以看到当前主机配置信息为localhost ...
- MySQL记录-Lost Connect MySQL Server during query解决方案
vim /etc/my.cnf ,在[mysqld]下面加上: skip-name-resolve max_allowed_packet = 800M default-character-set=ut ...
- mysql server advanced 5.6基于oracle linux 6.6的安装
mysql 安装有两种,rpm安装和源码包安装,两种包都可以从www.mysql.com官网下载,这次我测试下rpm安装方式. 1.安装环境以及mysql版本: 1.1vcenter 虚拟机环境 1. ...
- ubuntu 16.04安装mysql server入门
1.安装mysql-server -> sudo apt-get install mysql-server 输入root密码即可 2.修改服务器配置 默认mysql-server只对本机访问,新 ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
有时候,当我们使用"mysql"."mysqladmin"."mysqldump"等命令管理数据库时,服务器抛出类似如下错误: 一.错误现场 ...
- MySql数据库:Host 'localhost' is not allowed to connect to this MySQL server
修改mysql的root密码后,出现Host 'localhost' is not allowed to connect to this MySQL server 错误. 解决办法: C:\Progr ...
随机推荐
- VC中基于 Windows 的精确定时[转]
在工业生产控制系统中,有许多需要定时完成的操作,如定时显示当前时间,定时刷新屏幕上的进度条,上位 机定时向下位机发送命令和传送数据等.特别是在对控制性能要求较高的实时控制系统和数据采集系统中,就更需要 ...
- 如何优雅的使用 phpStorm 开发工具
按照惯例依然是从百科上复制一条简介: PhpStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具.PhpStorm可随时帮助用户对其编码进行调整,运行单元测试或者提供可视化d ...
- 【GoLang】GoLang for 中有多个循环变量怎么处理?
代码示例: sum := , ; i <= && j <= ; i, j = i+, j- { t.Log("i: ", i) t.Log(" ...
- 【云计算】docker三剑客如何支持分布式部署?
This blog will explain how to create multi-container application deployed on multiple hosts using Do ...
- 解决IDEA自动重置LanguageLevel和JavaCompiler版本的问题
使用IDEA时,导入的Maven项目默认的LanguageLevel和JavaCompiler都是1.5,1.5的情况下连最简单的@Override注解都不支持,所以项目可能出现一堆错. 虽然在项目上 ...
- vs2013 控制台程序exe图标
工程右击选择添加resource->Icon. 在工程目录就会生成 工程名.rc 和 XX.ico文件. 重新编译程序,就会生成有图标的exe. 对应的配置在 工程名.rc 文件里,用记事 ...
- 理解和解决MySQL乱码问题【转】
本文来自:http://www.cnblogs.com/cenalulu/p/4325693.html 要了解为什么会出现乱码,我们就先要了解从客户端发起请求,到MySQL存储数据,再到下次从表取回客 ...
- 数据结构——二叉查找树、AVL树
二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...
- Centos6.5 Openvpn的安装与配置
一.安装准备 ? 1 2 yum -y install openssl-devel openssl yum -y install gcc gcc-c++ 二.OpenVPN服务端安装过程1.lzo下载 ...
- 【leetcode】 Scramble String (hard)★
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...