linux + 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 <mysql/mysql.h>

编译选项:

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<stdio.h> 
  #include<mysql.h> 
  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 today@localhost 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

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值