建立web服务器一般是由LAMP(linux:操作系统、Apache:http服务器软件、MySQL:数据库软件和PHP:(有时也是指Perl 或 Python) 的第一个字母)组合起来的。

组件分析:

 Linux   

Linux 是免费开源软件,这意味着源代码可用的操作系统。

  Apache   

Apache 是使用中最受欢迎的一个开放源码的WEB服务器软件。   

  MySQL   

MySQL 是多线程、多用户的SQL数据库管理系统。   

  PHP,(Perl 或 Python )  

PHP 是一种编程语言最初设计生产动态网站。

PHP 是主要用于服务器端的应用程序软件。Perl 和 Python 类似。

联系:

建立一台WEB服务器,需要有Apache软件,而php是挂在apache底下执行的一个模块,而我们要用网页的php程序程控MySQL时,php就得要支持MySQL的模块。

所以我们配置一台web服务器需要的软件主要有:

httpd(提供Apache主程序)

mysql(MySQL客户端程序)

mysql-server(MySQL服务器端程序)

php(php主程序含给apache使用的模块)

php-mysql(提供给php程序读取MySQL数据库的模块)

这里我们主要介绍下web服务器中mysql的用法。

MySQL:

 数据库其实是一种特殊格式的档案,这种档案必需要通过一个特殊的接口(数据库软件)来进行读写。

 开源的数据库中,MySQL在性能、稳定性和功能上是首选,可以达到百万级别的数据存储,网站初期可以将MySQL和Web服务器放在一起,

 但是当访问 量达到一定规模后,应该将MySQL数据库从Web Server上独立出来,在单独的服务器上运行,同时保持Web Server和MySQL服务器的稳定连接。

 

数据库模型分类:

层次模型

网状模型

关系模型(实体-关系模型)

对象-关系模型

非关系模型

关系模型是目前最常用的

 

MySQL的数据类型:

数值型:精确数值型,整型,近似数值型(浮点型)

其中整型包括(tinyint  smallint mediumint  int(integer)  )

近似数值型也包括(单精度浮点型 fioat 双精度浮点型 double)

字符型:

字符( char(length):不区分大小写   varchar(length) 可以变化  character set :字符集 collate:排序规则   区分字母大小写:binary(length)  varbinary(length))

 

多字符:(text:不区分大小写 tinytext text  midiumtext longtext  blob等)

只要是字符,都有字符集和排序规则

内部类型(enum :枚举 定义了什么,只能存什么  set:定义了的字符,可以组合用)

日期时间型:(日期:date 时间:time  日期时间:datetime  年:year)

类型通常还有修饰符:unsigned (无符号的)

注:这里只列出一部分,想要知道更多的类型,可以网上搜索下哦

 

MySQL中对数据库操作的一些命令:

1、数据库对象操作:

创建   create

修改 alter

删除 drop

我们称其为DDL:database defining language 数据库定义语言

 

2、对表的操作:

添加 insert into

删除 delete

修改 update

查询 select

这些语言我们称其为DML: database manipulate language  数据库操作语言

 

3、对用户,角色的控制:

授予权限 grant

取消权限 revoke

这些操作我们称其为DCL:database conctrolling language  数据库控制语言

 

注:(以上这写命令是不区分大小写的)

 

下面以一个实例来解析下数据库这些命令的用法:

要想使用MySQL中这些命令,首先在我们系统上要有和MySQL相关的一些rpmbao

这里我们使用yum安装mysql.i386  、mysql-server.i386(基于Red Hat Enterprise Linux Server release 5.8 )这里yum安装就不多做介绍了

使用service mysqld start 启动数据库服务,

使用mysql命令进入数据库系统,便可进行进一步的操作了:

mysql>drop database if exists mydata; #如果存在mydata则删除

mysql> create database mydata; #建立库mydata

mysql> use mydata;   #打开库mydata

Database changed

 
  
  1. mysql> use mydata;  
  2. Database changed 
  3. mysql> create table data (ID int unsigned,Name char(20),Age int unsigned,Gender enum ('Female','Male'),Course char(30));     //建表结束 
  4. Query OK, 0 rows affected (0.01 sec) 
  5.  
  6. 注:在建表中(1)data将ID设为长度为整型的数字字段:并且是无符号的 
  7. (2)将NAME设为长度为20的字符字段 
  8. (3)将Age设为整型的数字字段而且是无符号的 
  9. (4)将Gender设为和Male或Female匹配的字符 
  10. (5)课程设置长度为30的字符字段。 

mysql> desc data;  #查看定义的表

 
  
  1. +--------+-----------------------+------+-----+---------+-------+ 
  2. | Field  | Type                  | Null | Key | Default | Extra | 
  3. +--------+-----------------------+------+-----+---------+-------+ 
  4. | ID     | int(10) unsigned      | YES  |     | NULL    |       |  
  5. | Name   | char(20)              | YES  |     | NULL    |       |  
  6. | Age    | int(10) unsigned      | YES  |     | NULL    |       |  
  7. | Gender | enum('Female','Male') | YES  |     | NULL    |       |  
  8. | Course | char(30)              | YES  |     | NULL    |       |  
  9. +--------+-----------------------+------+-----+---------+-------+ 
  10. 5 rows in set (0.00 sec) 

#以下为在表中插入字段

mysql> insert into data values (1, 'aa', 24, 'Male', 'math');

Query OK, 1 row affected (0.00 sec)

mysql> insert into data values (2, 'bb', 20, 'Female', 'language');

Query OK, 1 row affected (0.00 sec)

按照以上方式插入5条数据信息

mysql> select * from data;  #完成后,查看表里的内容:

 
  
  1. mysql> select * from data; 
  2. +------+------+------+--------+----------+ 
  3. | ID   | Name | Age  | Gender | Course   | 
  4. +------+------+------+--------+----------+ 
  5. |    1 | aa   |   24 | Male   | math     |  
  6. |    2 | bb   |   20 | Female | language |  
  7. |    3 | cc   |   21 | Male   | English  |  
  8. |    4 | dd   |   22 | Male   | history  |  
  9. |    5 | ee   |   25 | Female | Chinese  |  
  10. +------+------+------+--------+----------+ 
  11. 5 rows in set (0.00 sec) 
  12. 以上就是自己定义的数据的信息了哦。。 

对表中数据的操作(定义表):

update tb_name set colum_name=value where condition;    #更改某一字段的属性(约束条件)

一般来讲,where 中的condition指的是对表中某字段或某些字段做判定(等值比较,或正则表达式匹配,通配符匹配)

 
  
  1. eg: 
  2. mysql>update data set Course='math' where ID=2;#将ID号为2的人选的课程更改为math 
  3. Query OK, 1 row affected (0.00 sec) 
  4. Rows matched: 1  Changed: 1  Warnings: 0 
  5.  
  6. mysql> select * from data; 
  7. +------+------+------+--------+---------+ 
  8. | ID   | Name | Age  | Gender | Course  | 
  9. +------+------+------+--------+---------+ 
  10. |    1 | aa   |   24 | Male   | math    |  
  11. |    2 | bb   |   20 | Female | math    |  
  12. |    3 | cc   |   21 | Male   | English |  
  13. |    4 | dd   |   22 | Male   | history |  
  14. |    5 | ee   |   25 | Female | Chinese |  
  15. +------+------+------+--------+---------+ 
  16. 5 rows in set (0.00 sec) 

delete from tb_name where condition;  #从表中删除符合某种条件的数据

 
  
  1. eg: 
  2. mysql> delete from data where Age < 22;  #将年龄小于22的所有人删除 
  3. Query OK, 2 rows affected (0.00 sec) 
  4.  
  5. mysql> select * from data;    
  6. +------+------+------+--------+---------+ 
  7. | ID   | Name | Age  | Gender | Course  | 
  8. +------+------+------+--------+---------+ 
  9. |    1 | aa   |   24 | Male   | math    |  
  10. |    4 | dd   |   22 | Male   | history |  
  11. |    5 | ee   |   25 | Female | Chinese |  
  12. +------+------+------+--------+---------+ 
  13. 3 rows in set (0.00 sec) 

查询:

select 字段 from 表 [where condition] 

 
  
  1. mysql> select Name ,Age ,ID from data where Gender='Male'
  2.  #查找性别为男的姓名、年龄和ID号。 
  3. +------+------+------+ 
  4. | Name | Age  | ID   | 
  5. +------+------+------+ 
  6. | aa   |   24 |    1 |  
  7. | dd   |   22 |    4 |  
  8. +------+------+------+ 
  9. 2 rows in set (0.00 sec) 

对字段进行操作:

alter table tb_name drop 字段名称;  #删除字段

 
  
  1. mysql> alter table data drop Age;    #删除年龄这个字段 
  2. Query OK, 3 rows affected (0.02 sec) 
  3. Records: 3  Duplicates: 0  Warnings: 0 
  4.  
  5. mysql> select * from data; 
  6. +------+------+--------+---------+ 
  7. | ID   | Name | Gender | Course  | 
  8. +------+------+--------+---------+ 
  9. |    1 | aa   | Male   | math    |  
  10. |    4 | dd   | Male   | history |  
  11. |    5 | ee   | Female | Chinese |  
  12. +------+------+--------+---------+ 
  13. 3 rows in set (0.01 sec) 

alter table tb_name add 字段名 类型 [{first|after column_name}]; #添加字段

 
  
  1. mysql> alter table data add class int unsigned after ID;  
  2. #在ID 号之后添加一个CLASS 字段 
  3. Query OK, 3 rows affected (0.05 sec) 
  4. Records: 3  Duplicates: 0  Warnings: 0 
  5.  
  6. mysql> select * from data; 
  7. +------+-------+------+--------+---------+ 
  8. | ID   | class | Name | Gender | Course  | 
  9. +------+-------+------+--------+---------+ 
  10. |    1 |  NULL | aa   | Male   | math    |  
  11. |    4 |  NULL | dd   | Male   | history |  
  12. |    5 |  NULL | ee   | Female | Chinese |  
  13. +------+-------+------+--------+---------+ 
  14. 3 rows in set (0.00 sec) 

alter table tb_name change old_name new_name datatype;  #更改字段名

alter table tb_name modify column_name datatype;    #更改一个字段的属性

(可以按照上面的例子自己尝试一下哦)

数据控制语言DCL:

grant  privileges on db_name to 'username'@host'identified by 'passwd';  #赋予某用户什么权限

 
  
  1. mysql> grant all on mydata.data to 'abc'@'172.16.%.%' identified by 'redhat';
  2.   
  3. Query OK, 0 rows affected (0.00 sec) 
  4. mysql> flush privileges; 
  5. #将mydata库中表data的所有权限授予172.16/24这个网段内的abc用户,
  6. 并给用户指定密码为redhat 
  7. #让新建用户和授权立即生效:flush privileges; 

revoke privileges on db_name.tb_name to 'username'@'host';  #从用户收回在数据库对象上的权限

 
  
  1. mysql> revoke delete on mydata.data from 'abc'@'172.16.%.%';  
  2.  #从abc用户上收回对数据库delete的权限 
  3. Query OK, 0 rows affected (0.00 sec) 
  4.  

 

 
  
  1. mysql> flush privileges; 

验证效果,再打开一台主机(172.16.10.1),连接该数据库,进行操作。

远程连接有些命令选项:

-h:指定服务器地址

-u:指定用户名

-p:让用户输入密码

 
  
  1. [root@www ~]# mysql -h 172.16.9.1 -u abc -p; #以用户abc的身份连接服务器上的数据库    
  2. Enter password:  
  3. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  4. Your MySQL connection id is 12 
  5. Server version: 5.0.77 Source distribution 
  6.  
  7. Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
  8.  
  9. mysql> mysql> select * from data;  #可以查询的哦!! 
  10. +------+-------+------+--------+---------+ 
  11. | ID   | class | Name | Gender | Course  | 
  12. +------+-------+------+--------+---------+ 
  13. |    1 |  NULL | aa   | Male   | math    |  
  14. |    4 |  NULL | dd   | Male   | history |  
  15. |    5 |  NULL | ee   | Female | Chinese |  
  16. +------+-------+------+--------+---------+ 
  17. 3 rows in set (0.00 sec) 
  18. mysql> delete from data where ID=1
  19. ERROR 1142 (42000): DELETE command denied to user 'abc'@'172.16.10.1' for table 'data' 
  20. #在这里就会显示不允许delete操作的提示了!! 

 

以上就是数据库的操作了^_^