1.先安装mysql 代码:sudo apt-get install mysql-server mysql-client 2.再装开发包 代码:sudo apt-get install libmysqlclient-dev
启动mysql服务:
1.net start mysql 这是在DOS环境下采用的命令,在linux中没有这个命令.
2. /etc/init.d/mysql restart 这是linux下的服务启动命令,我的机子上已经启动本服务,运行命令的结果显示如下
上面显示已经开启服务,还给出了相应进程号.
3.测试连接,编辑conn_mysql.c文件:
代码:
#include
#include
#include
int main(int argc, char *argv[])
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "xtaywgqt","c_test",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
res = mysql_query(&my_connection, "insert into user_info values(3,'Li','f')");
if (!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
/*里头的函数返回受表中影响的行数*/
}
else
{
//分别打印出错误代码及详细信息
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
}
else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
4.编译方法:
gcc $(mysql_config --cflags) conn_mysql.c -o conn_mysql $(mysql_config --libs)