安装完mysql是还不可以的,需要额外安装linux下操作mysql依赖的库,安装命令如下:
sudo apt-get install libmysqlclient-dev
安装完后即可对mysql进行操作。
因为最后编译的时候需要用到mysql的路径,如果不知道mysql的路径在哪,可以用一下命令:
whereis mysql
我这里的是/usr/include/mysql和/usr/lib/mysql
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mysql.h>
#define SERV_PORT 8000
int main()
{
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if(!conn_ptr){
printf("mysql init failed\n");
}
conn_ptr = mysql_real_connect(conn_ptr,"localhost","User","passwd","database",0,NULL,0);
if(!conn_ptr) printf("Connection failed..\n");
mysql_close(conn_ptr);
}
编译命令:gcc -I/usr/include/mysql my_mysql.c -L/usr/lib/mysql -lmysqlclient -o my_mysql