Ubuntu 12.04下安装mysql并用C程序测试连接
2014-03-28 12:57
549 查看
1.先安装mysql
代码:sudo apt-get install
mysql-server mysql-client
2.再装开发包
代码:sudo apt-get install
libmysqlclient-dev
启动mysql服务:
3.测试连接,编辑conn_mysql.c文件:
代码:#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
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)
代码: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 <stdio.h>
#include <stdlib.h>
#include <mysql.h>
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)
相关文章推荐
- ubuntu安装mysql,window连接该mysql
- ubuntu 12.04 下apache2.2.22 php5 mysql5.5 的安装与使用
- windows下用java程序连接Ubuntu16.04下的MySQL
- ubuntu下mysql安装与测试
- Ubuntu 12.04 Server LTS安装部署Nginx+MySQL+Ruby1.9.3+Rails3.2
- Ubuntu 12.04上安装Nginx, PHP,Mysql及配置
- Ubuntu下安装MySql并在外网连接
- Ubuntu 12.04安装Go语言运行环境,第一个程序Hello world
- Ubuntu 12.04 下安装MySQL 5.5.25
- 64位Ubuntu 12.04 安装mysql-workbench 分类: ubuntu学习 2013-12-23 15:16 835人阅读 评论(0) 收藏
- ubuntu 12.04 安装 nginx+php+mysql web服务器
- ubuntu安装mysql后无法远程连接处理
- linux 下安装mysql (ubuntu 12.04)
- Ubuntu 12.04卸载与安装Mysql
- Linux下安装mysql并用c语言连接
- my SQL下载安装,环境配置,以及密码忘记的解决,以及navicat for mysql下载,安装,测试连接
- 教为学:Python学习之路(二):MySQLdb的几种安装方式,以及用Python测试连接MySql
- 1、MySql的安装和连接测试并给root用户赋密码
- ubuntu 12.04 安装 nginx+php+mysql web服务器
- 64位Ubuntu 12.04 安装mysql-workbench