您的位置:首页 > 运维架构 > Linux

CentOS 6.8 安装MySql+GCC 编译

2017-08-02 23:21 344 查看

安装Mysql

1 . 查看是否安装Mysql

# rpm -qa | grep mysql 


2 . 查看可用版本

# yum list | grep mysql


3 . 安装服务器、客户端、开发库

# yum install -y mysql-server mysql mysql-deve


4 . 查看安装的版本

# rpm -qi mysql-server


配置Mysql

首次启动

# service mysqld start
下面有很多提示


设置数据库root用户密码

# mysqladmin -u root password '我是密码'


登录数据库

# mysql -u root -p
输入刚刚设置的密码


设置Mysql服务开机启动

# chkconfig mysqld on
通过以下命令查看是否设置
# chkconfig --list | grep mysql
mysqld   0:off   1:off   2:on    3:on    4:on    5:on    6:off


查看修改Mysql头文件和库文件

1 . 查找头文件

sudo find / -name 'mysql'
查到mysql文件夹在 /usr/include


2 . 查看链接库文件

sudo find / -name '*mysqlclient*'
结果为:
/usr/lib64/mysql/libmysqlclient_r.so.16
/usr/lib64/mysql/libmysqlclient.so
/usr/lib64/mysql/libmysqlclient_r.so
/usr/lib64/mysql/libmysqlclient.so.16.0.0
/usr/lib64/mysql/libmysqlclient.so.16
/usr/lib64/mysql/libmysqlclient_r.so.16.0.0
而gcc在寻找库文件,默认是没有这个路径的所以把
libmysqlclient.so
libmysqlclient.so.16.0.0
libmysqlclient.so.16
这三个文件复制到  /usr/lib/ 下


3 . 测试

//本程序在
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#define MAX_COLUMN_LEN  32
int main(int argc , char *argv[])
{
MYSQL my_connection;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[MAX_COLUMN_LEN][MAX_COLUMN_LEN];
int res;
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"127.0.0.1","root","root","test",3306,NULL,0))
{
perror("connect");
res=mysql_query(&my_connection,"select * from c_test");//查询
if(!res)
{
result=mysql_store_result(&my_connection);//保存查询到的数据到result
if(result)
{
int i,j;
printf("the result number is %lu\n ",(unsigned long)mysql_num_rows(result));
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
bzero(column[i],sizeof(column[i]));
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i<j;i++)
{
printf("%s\t",sql_row[i]);
}
printf("\n");
}

}
}
else
{
perror("select");
}
}
else
{
perror("connect:error");
}
//mysql_free_result(MYSQL_RES *result);//释放结果资源
mysql_close(&my_connection);//断开连接
}


以上程序源链接

http://www.cnblogs.com/nysanier/archive/2011/03/25/1995890.html

编译命令

gcc mysqltest.c -lmysqlclient

这是数据库中数据

mysql> select * from c_test;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    3 | c    |
|    2 | bbb  |
|    4 | d    |
+------+------+
4 rows in set (0.00 sec)


./a.out后结果

connect: Success
the result number is 4
id name
1   aaa
3   c
2   bbb
4   d
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  centos mysql