
点击了解更多技术资料和免费学习视频,请加入讨论群:662736393
欢迎关注百哥IT技术专栏,联系QQ:3473067134
什么是数据库
在各种互联网应用中,存在大量的数据,比如用户信息,用户行为,系统日志等。
这些数据必须能高效存取,于是需要用到数据库。有很多数据库软件(Myslq,Oracle,Sql server等),其中最常用的开源数据库为mysql,这里以mysql为例,来看最常用的一些操作。
实验环境介绍
这里以通过lnmp部署的discuz论坛为环境,来看mysql在web架构中的角色和工作原理,以及增删改查等相关操作。Discuz的部署,可以参考之前的文章。
系统平台:最新版centos8
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
Web平台:nginx 1.14
[root@centos8 ~]# nginx -V
nginx version: nginx/1.14.1
built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC)
php版本:7.2.11
[root@centos8 ~]# php-fpm -version
PHP 7.2.11 (fpm-fcgi) (built: Oct 9 2018 15:09:36)
Mysql版本:15.1(等同mariadb10.3.17)
[root@centos8 ~]# mysql -V
mysql Ver 15.1 Distrib 10.3.17-MariaDB
Discuz:最新版3.4

动态网站和数据库的关系
我们在web页面查看用户信息,其实读取的就是mysql数据库里的信息

图中的个人资料,对应在mysql中的数据如下
MariaDB [discuz]> select realname,birthyear,birthprovince from pre_common_member_profile where
+-----------+-----------+---------------+
| realname | birthyear | birthprovince |
+-----------+-----------+---------------+
| 令狐冲 | 1985 | 江西省 |
+-----------+-----------+---------------+
1 row in set (0.001 sec)
接下来我们来看数据库中的曾删改查相关操作。
Mysql常用命令之“查”
1、登录数据库
[root@centos8 ~]# mysql -uroot –p123456
说明,这里的root是用户名,123456是密码
2、查有哪些数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| discuz |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
说明,当前有4个数据库,名字分别如上
3、使用discuz库,这个是discuz论坛的库
MariaDB [(none)]> use discuz;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [discuz]>
4、查discuz库中有哪些表
MariaDB [discuz]> show tables;
+-----------------------------------+
| Tables_in_discuz |
+-----------------------------------+
| pre_common_admincp_cmenu |
| pre_common_admincp_group |
| pre_common_admincp_member |
| pre_common_admincp_perm |
| pre_common_admincp_session |
| pre_common_admingroup |
| pre_common_adminnote |
。。。省略
说明:会看到很多表,每个动态web站点,都会有很多表,用来存放不同数据。在discuz中,用来存放用户详细资料的表为pre_common_member_profile,我们在这个表中,来看相关的查询命令如何使用。
5、查看pre_common_member_profile表中的所有数据
MariaDB [discuz]> select * from pre_common_member_profile ;
会发现有大量的数据。包含用户的各种详细资料,如姓名出生年月工作所在地职位年薪等等。因为数据量太大,不方便一下找到自己想要的关键信息。所以就需要更精确的来查找关键信息。
6、查看用户张三丰的公司、岗位和年薪
MariaDB [discuz]> select company,occupation,revenue from pre_common_member_profile where realname = "张三丰" ;
+-----------+------------+---------+
| company | occupation | revenue |
+-----------+------------+---------+
| 武当派 | 掌门人 | 1000000 |
+-----------+------------+---------+
1 row in set (0.001 sec)
说明,company,occupation,revenue为表中的3个列,where realname = "张三丰"为只看用户张三丰的信息
7、查出所有单身的用户
MariaDB [discuz]> select realname from pre_common_member_profile where affectivestatus="单身" ;
+-----------+
| realname |
+-----------+
| 令狐冲 |
| 张三丰 |
+-----------+
2 rows in set (0.002 sec)
8、查出所有单身并且年薪大于50w的用户
MariaDB [discuz]> select realname from pre_common_member_profile where affectivestatus="单身" and revenue>500000 ;
+--------------+
| realname |
+--------------+
| 张三丰 |
| 灭绝师太 |
+--------------+
2 rows in set, 1 warning (0.001 sec)
9、查出所有单身的,并且年薪大于50w的,并且性别不是女性的
这个有点饶,主要用来看mysql查询命令中的not
MariaDB [discuz]> select realname from pre_common_member_profile where affectivestatus="单身" and revenue>500000 and not gender=2;
+-----------+
| realname |
+-----------+
| 张三丰 |
+-----------+
1 row in set, 1 warning (0.001 sec)
说明:在这张表中,gender(性别)的选项只有1和2两个,其中1代表男性2代表女性。所以这里的not gender=2意思是“性别不是女性”作为查询条件。
Mysql常用命令之“删”
删除命令非常危险,一定要小心操作
1、删除用户令狐冲的所有信息
MariaDB [discuz]> delete from pre_common_member_profile where realname="令狐冲";
Query OK, 1 row affected (0.001 sec)
在web页面上查看,发现令狐冲的所有资料都空了

2、删除张三丰的公司信息
MariaDB [discuz]> update pre_common_member_profile set company="" where realname="张三丰";
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
说明:这里的update是用来修改company数据的,将其修改为空也就是删除company数据

在web界面发现张三丰的公司为空
3、删除整张表
MariaDB [discuz]> delete from pre_common_member_profile;
Query OK, 3 rows affected (0.001 sec)
在表后面不跟where,则是删除整个表,非常危险的操作。所以在删除某行信息时,一定不要忘记where,否则后果很严重。
进入论坛,发现所有用户的资料都空了

4、删除数据库
这个就更危险了,所以这里在操作前拍了快照。
MariaDB [discuz]> drop database discuz ;
Query OK, 292 rows affected (0.156 sec)
然后返回论坛,发现网站已经崩溃,无法打开

所以一定不要误删重要的数据库,另外平常要做好备份,容灾等相关操作。
5、删除数据库的数据文件
Linux系统一切皆文件,那么数据库里的数据也在文件里,删除相对应的文件,也会删除数据。
[root@centos8 my.cnf.d]# cat /etc/my.cnf.d/mariadb-server.cnf | grep datadir
datadir=/var/lib/mysql
在mysql配置文件中可以看到数据存储目录。恢复快照后,也就是删库之前,在这里可以看到discuz数据库对应的文件
[root@centos8 mysql]# pwd
/var/lib/mysql
[root@centos8 mysql]# ls -alh discuz -d
drwx------. 2 mysql mysql 36K Apr 9 12:51 discuz
所以备份好这个目录,如果数据被删除了,再还原这个目录,数据库也是额可以恢复的。
Mysql常用命令之“改”
1、修改令狐冲的情感状态为“恋爱中”
MariaDB [discuz]> update pre_common_member_profile set affectivestatus = "恋爱中" where realname= "令狐冲";
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0

2、批量修改所有用户的所在地为北京
MariaDB [discuz]> select resideprovince,realname from pre_common_member_profile ;
+----------------+--------------+
| resideprovince | realname |
+----------------+--------------+
| | |
| 北京市 | 令狐冲 |
| 安徽省 | 张三丰 |
| | 灭绝师太 |
+----------------+--------------+
4 rows in set (0.001 sec)
MariaDB [discuz]> update pre_common_member_profile set resideprovince="北京";
Query OK, 4 rows affected (0.001 sec)
Rows matched: 4 Changed: 4 Warnings: 0
MariaDB [discuz]> select resideprovince,realname from pre_common_member_profile ;
+----------------+--------------+
| resideprovince | realname |
+----------------+--------------+
| 北京 | |
| 北京 | 令狐冲 |
| 北京 | 张三丰 |
| 北京 | 灭绝师太 |
+----------------+--------------+
4 rows in set (0.002 sec)
Mysql常用命令之“增”
1、创建新数据库
MariaDB [(none)]> create database `ceshi` character set utf8;
Query OK, 1 row affected (0.002 sec)
说明:创建一个名为ceshi的库,并且字符编码指定为utf8,这样才可以支持中文。
2、创建可管理这个数据库的用户
grant all on ceshi.* to 'ceshiadmin'@'%' identified by '123456';
说明:
grant all on ceshi.* 针对ceshi库,赋予所有权限
'ceshiadmin' 用户名
@'%' 允许ceshiadmin这个用户从所有源ip来登录数据库
identified by '123456'; 设置密码
3、创建表
MariaDB [ceshi]> create table employee (`id` mediumint(11),`name` varchar(30),`mobile` bigint(11));
Query OK, 0 rows affected (0.073 sec)
说明:创建一个名为employee的员工信息表,其中包含三个内容
Id:员工工号,数据类型为数字,medium是用3个字节来存储,范围可以2的24次方
Name:员工姓名,数据类型为字符串。
Mobile:手机号码,数据类型为更大范围的数字(bigint是8个字节),因为mediumint放不下13位的手机号,加国际区号就更放不下了。
4、查看刚才创建的表结构
MariaDB [ceshi]> describe employee;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id | mediumint(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| mobile | bigint(11) | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
3 rows in set (0.004 sec)
5、在表中新增一条记录并查看
MariaDB [ceshi]> insert into employee (id,name,mobile) values (123,"丘处机",13333333333);
说明:
insert into 插入新的数据
(id,name,mobile) values (123,"丘处机",13333333333),数据的内容
MariaDB [ceshi]> select * from employee;
+------+-----------+-------------+
| id | name | mobile |
+------+-----------+-------------+
| 123 | 丘处机 | 13333333333 |
+------+-----------+-------------+
1 row in set (0.001 sec)
6、在表中增加两列,分别是出生日期和地点
MariaDB [ceshi]> alter table employee add (`birthday` date,`location` char(30));
Query OK, 0 rows affected (0.314 sec)
Records: 0 Duplicates: 0 Warnings: 0
说明:这里的出生日期用了date类型,默认格式是xxxx-xx-xx
MariaDB [ceshi]> describe employee;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| id | mediumint(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| mobile | bigint(11) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| location | char(30) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+
5 rows in set (0.002 sec)
插入一行新数据并查看
MariaDB [ceshi]> insert into employee (id,name,mobile,birthday,location) values (007,"张无忌",13999999999,"1887-07-07","北京");
Query OK, 1 row affected (0.277 sec)
MariaDB [ceshi]> select * from employee where name="张无忌";
+------+-----------+-------------+------------+----------+
| id | name | mobile | birthday | location |
+------+-----------+-------------+------------+----------+
| 7 | 张无忌 | 13999999999 | 1887-07-07 | 北京 |
+------+-----------+-------------+------------+----------+
1 row in set (0.001 sec)