免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 959 | 回复: 0
打印 上一主题 下一主题

linux + mysql笔记 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-25 15:24 |只看该作者 |倒序浏览
   最近在看linux和mysql,发现在使用API连接到mysql时候有些参数容易引起错误,在网上搜了下,发现了一片比较详细的文章,就转载了过来,偷懒。。博文如下。

一 安装
rpm安装后启动不成功,改用压缩包编译
1)建立相应目录和组:
# mkdir /usr/local/mysql
# groupadd mysql
# useradd -g mysql mysql                  //useradd -g mysql -d /usr/local/mysql name
2)开始安装mysql
# tar xzvf mysql-5.0.22.tar.gz                            //解压缩
# cd mysql-5.0.22                         //进入解压后的文件目录
# ./configure --prefix=/usr/local/mysql \                     //设定安装目录
--enable-thread-safe-client \                                     //编译线程安全版的客户端库
--without-debug \                                                      //关闭debug功能
--with-extra-charsets=gb2312 \                         //添加gb2312中文字符支持
--enable-assembler \                                                       //使用一些字符函数的汇编版本
# make                            //编译
# make install                            //安装
3)copy配置文件
有large,medium,small三个环境下的,根据机器性能选择,如果负荷比较大,可修改里面的一些变量的内存使用值
# cp support-files/my-medium.cnf /etc/my.cnf                  //复制配置文件
4)更改目录权限和组
# cd /usr/local/mysql
# chown -R mysql .
# chgrp -R mysql .
5)建立数据库和表
# bin/mysql_install_db --user=mysql                            //初始化授权
6)再次更改目录权限和组
# chown -R root .
# chown -R mysql var
7)启动MySQL服务
# bin/mysqld_safe --user=mysql &                                
//启动MySQL(The & character tells the operating system to run MySQL in the background;
//it is ignored by MySQL itself.
//如果报错,注意及时查看/usr/local/mysql/var/下的日志文件)
8)设置MySQL启动服务
# cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld                                                 //在自动启动列表里添加mysqld
# chkconfig --level 345 mysqld on
9)修改MySQL密码
# /usr/local/mysql/bin/mysqladmin -u root password 'new-password'                //修改密码
# /usr/local/mysql/bin/mysqladmin -u root -h localhost password 'new-password'
// 将localhost替换成你的主机域名,比如:zhaorg.csu.edu.cn
10)登录mysql数据库:
#  cd /usr/local/mysql/bin
#  ./mysql -u root -p
# /usr/local/mysql/bin/mysqladmin -u root -p new-password shutdown          //关闭MySQL
二 开发
头文件:#include
编译选项:
gcc        -o server server.c –I /usr/include/mysql –L /usr/lib/mysql –l mysqlclient –lz –lm
连接过程:
Step 1:连接数据库
建立MYSQL mysql;
mysql_init(&mysql);初始化
mysql_real_connect(&mysql,”hostname”,”username”,”password”,”database”,0,NULL,0);
Step 2:执行SQL语句
mysql_real_query(&mysql,SQL);
Step 3:获取查询结果
建立MYSQL_RES结构 *res;
res = mysql_store_result(&mysql);
Step 4:获取查询结果每一行,并进行相应处理
mysql_fetch_row(res);
mysql_num_fields(res); 获取结果的字段数
printf(“%s\n”,row[index]);
Step 5:释放资源
mysql_free_result(res);
mysql_close(&mysql);
三 例子
下面有一个用GCC编译的例子希望对你有所帮助.  
  #include  
  #include  
  int   main()  
  {  
          /*declare   structures   and   variables   */  
        MYSQL   mysql;  
        MYSQL_RES   *result;  
        MYSQL_ROW   row;  
   
   
        //initialize   MYSQL   structure  
      mysql_init(&mysql);  
   
   
      //connect   to   database  
      mysql_real_connect(&mysql,"localhost","john","doe","db1",0,null,0);  
   
      //execute   query  
        mysql_query(&mysql,"select   title,author   from   books");  
   
        //get   result   set  
        result=mysql_store_result(&mysql);  
   
      //process   result   set  
        while((row=mysql_fetch_row(result))  
  {  
        fprintf(stdout,"%s   -   %s\n",row[0],row[1]);  
  }  
   
        //clean   up  
        mysql_free_result(result);  
        mysql_close(&mysql);  
  }  
  (假设mysql安装在/usr/local/mysql目录下)  
  完成之后,保存该文件(名为:sample.c),并且进行编译:  
  [user@host]$   /usr/bin/gcc   sample.c   -o   sample.bin   -I/usr/local/mysql/include   -L/usr/local/mysql/lib     -lmysclient   -lz  
   
   
  -lmysclient   选项告诉连接程序连接libmysqlclient库,而-I和-L选项告诉编译程序在什么位置分别查找MySQL头文件和库,-o选项告诉编译程序分配给最终可执行二进制文件的名称.如果没有使用这些变量,GCC则使用默认值a.out  
   
   
  说明:我不是用linux的,恰好我这里有这样的一个例子,所以拿出来希望对你有所帮助,更加详细到mysql的官方网站上去看吧.  
  
http://dev.mysql.com/doc/refman/4.1/en/apis.html
四 参考命令
mysqladmin -u root password 123456 设置root的密码为123456
grant all privileges on *.* to
[email=today@localhost]today@localhost[/email]
identified by "todaylxp";
mysql>create database sampdb;
mysql>create table new (name char(20),phone char(20));
mysql>insert into new (’abc,’0532555555’);


mysql>show database;
mysql
sampdb
test
mysql>use sampdb;
mysql>show tables;
new


五 错误记录

[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I/usr/local/mysql/include   -L/usr/local/mysql/lib
sample.c:2:21: 错误:mysql.h:没有那个文件或目录
sample.c: 在函数 ‘main’ 中:
sample.c:6: 错误:‘MYSQL’ 未声明 (在此函数内第一次使用)
sample.c:6: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
sample.c:6: 错误:所在的函数内只报告一次。)
sample.c:6: 错误:expected ‘;’ before ‘mysql’
sample.c:7: 错误:‘MYSQL_RES’ 未声明 (在此函数内第一次使用)
sample.c:7: 错误:‘result’ 未声明 (在此函数内第一次使用)
sample.c:8: 错误:‘MYSQL_ROW’ 未声明 (在此函数内第一次使用)
sample.c:8: 错误:expected ‘;’ before ‘row’
sample.c:12: 错误:‘mysql’ 未声明 (在此函数内第一次使用)
sample.c:16: 错误:‘null’ 未声明 (在此函数内第一次使用)
sample.c:25: 错误:‘row’ 未声明 (在此函数内第一次使用)
sample.c:26: 错误:expected ‘)’ before ‘{’ token
sample.c:33: 错误:expected expression before ‘}’ token
[root@localhost cplusproject]# vi sample.c
[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I/usr/local/mysql/include   -L/usr/local/mysql/lib
sample.c:2:22: 错误:mysql.h:没有那个文件或目录
sample.c: 在函数 ‘main’ 中:
sample.c:6: 错误:‘MYSQL’ 未声明 (在此函数内第一次使用)
sample.c:6: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
sample.c:6: 错误:所在的函数内只报告一次。)
sample.c:6: 错误:expected ‘;’ before ‘mysql’
sample.c:7: 错误:‘MYSQL_RES’ 未声明 (在此函数内第一次使用)
sample.c:7: 错误:‘result’ 未声明 (在此函数内第一次使用)
sample.c:8: 错误:‘MYSQL_ROW’ 未声明 (在此函数内第一次使用)
sample.c:8: 错误:expected ‘;’ before ‘row’
sample.c:12: 错误:‘mysql’ 未声明 (在此函数内第一次使用)
sample.c:16: 错误:‘null’ 未声明 (在此函数内第一次使用)
sample.c:25: 错误:‘row’ 未声明 (在此函数内第一次使用)
sample.c:26: 错误:expected ‘)’ before ‘{’ token
sample.c:33: 错误:expected expression before ‘}’ token
[root@localhost cplusproject]# vi sample.c
[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I/usr/local/mysql/include   -L/usr/local/mysql/lib
sample.c: 在函数 ‘main’ 中:
sample.c:16: 错误:‘null’ 未声明 (在此函数内第一次使用)
sample.c:16: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
sample.c:16: 错误:所在的函数内只报告一次。)
sample.c:26: 错误:expected ‘)’ before ‘{’ token
sample.c:33: 错误:expected expression before ‘}’ token
[root@localhost cplusproject]# vi sample.c
[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I/usr/local/mysql/include   -L/usr/local/mysql/lib
sample.c: 在函数 ‘main’ 中:
sample.c:26: 错误:expected ‘)’ before ‘{’ token
sample.c:33: 错误:expected expression before ‘}’ token
[root@localhost cplusproject]# vi sample.c
[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I/usr/local/mysql/include   -L/usr/local/mysql/lib
/tmp/ccauglxT.o: In function `main':
sample.c:(.text+0x1e): undefined reference to `mysql_init'
sample.c:(.text+0x64): undefined reference to `mysql_real_connect'
sample.c:(.text+0x7a): undefined reference to `mysql_query'
sample.c:(.text+0x88): undefined reference to `mysql_store_result'
sample.c:(.text+0xc3): undefined reference to `mysql_fetch_row'
sample.c:(.text+0xd7): undefined reference to `mysql_free_result'
sample.c:(.text+0xe5): undefined reference to `mysql_close'
collect2: ld 返回 1
[root@localhost cplusproject]# vi sample.c
[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I/usr/local/mysql/include/mysql   -L/usr/local/mysql/lib/mysql
/tmp/ccUaDjfQ.o: In function `main':
sample.c:(.text+0x1e): undefined reference to `mysql_init'
sample.c:(.text+0x64): undefined reference to `mysql_real_connect'
sample.c:(.text+0x7a): undefined reference to `mysql_query'
sample.c:(.text+0x88): undefined reference to `mysql_store_result'
sample.c:(.text+0xc3): undefined reference to `mysql_fetch_row'
sample.c:(.text+0xd7): undefined reference to `mysql_free_result'
sample.c:(.text+0xe5): undefined reference to `mysql_close'
collect2: ld 返回 1
[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I /usr/local/mysql/include/mysql   -L /usr/local/mysql/lib/mysql
/tmp/ccUDtNGD.o: In function `main':
sample.c:(.text+0x1e): undefined reference to `mysql_init'
sample.c:(.text+0x64): undefined reference to `mysql_real_connect'
sample.c:(.text+0x7a): undefined reference to `mysql_query'
sample.c:(.text+0x88): undefined reference to `mysql_store_result'
sample.c:(.text+0xc3): undefined reference to `mysql_fetch_row'
sample.c:(.text+0xd7): undefined reference to `mysql_free_result'
sample.c:(.text+0xe5): undefined reference to `mysql_close'
collect2: ld 返回 1
[root@localhost cplusproject]# gcc   sample.c   -o   sample.bin   -I /usr/local/mysql/include/mysql   -L /usr/local/mysql/lib/mysql -l mysqlclient -lz
[root@localhost cplusproject]# ./sample.bin
./sample.bin: error while loading shared libraries: libmysqlclient.so.15: cannot open shared object file: No such file or directory
库文件链接错误1.库的路径要设置正确
2.要加上"-l mysqlclient -lz"
3.[root@localhost cplusproject]# cp /usr/local/mysql/lib/mysql/libmysqlclient.so.15 /usr/lib

本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/todaylxp/archive/2009/07/06/4326455.aspx


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/101537/showart_2007084.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP