我叫林克不是塞尔达

导航

mySQL安装与基础配置

关系型数据库介绍

数据结构模型

数据结构模型主要有:

  • 层次模型
  • 网状结构
  • 关系模型

关系模型:
二维关系:row,column

数据库管理系统:DBMS
关系:Relational,RDBMS

RDBMS专业名词

常见的关系型数据库管理系统:

  • MySQL:MySQL,MariaDB,Percona-Server
  • PostgreSQL:简称为pgsql
  • Oracle
  • MSSQL

事务:多个操作被当作一个整体对待就称为一个事务
要看一个关系型数据库是否支持事务,需要看其是否支持并满足ACID测试
ACID:ACID是事务的一个基本标准

  • A:Automicity,原子性
  • C:Consistency,一致性
  • I:Isolation,隔离性
  • D:Durability,持久性

如果你对ACID感兴趣,可以查看这里了解详细说明,ACID将不作为我们讲解的重点。

SQL:Structure Query Language,结构化查询语言

约束:constraint,向数据表提供的数据要遵守的限制

  • 主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
    一个表只能存在一个
  • 惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
    一个表可以存在多个
  • 外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
    检查性约束

索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储

关系运算:

  • 选择:挑选出符合条件的行(部分行)
  • 投影:挑选出需要的字段
  • 连接

数据抽象方式

  • 物理层:决定数据的存储格式,即RDBMS在磁盘上如何组织文件
  • 逻辑层:描述DB存储什么数据,以及数据间存在什么样的关系
  • 视图层:描述DB中的部分数据

关系型数据库的常见组件

关系型数据库的常见组件有:

  • 数据库:database
  • 表:table,由行(row)和列(column)组成
  • 索引:index
  • 视图:view
  • 用户:user
  • 权限:privilege
  • 存储过程:procedure
  • 存储函数:function
  • 触发器:trigger
  • 事件调度器:event scheduler

SQL语句

SQL语句有三种类型:

  • DDL:Data Defination Language,数据定义语言
    • CREATE:创建
    • DROP:删除
    • ALTER:修改
  • DML:Data Manipulation Language,数据操纵语言
    • INSERT:向表中插入数据
    • DELETE:删除表中数据
    • UPDATE:更新表中数据
    • SELECT:查询表中数据
  • DCL:Data Control Language,数据控制语言
    • GRANT:授权
    • REVOKE:移除授权

mysql安装与配置

安装mysql

#配置yum源
[root@YingMo ~]# cd /usr/src/
[root@YingMo src]# ls
debug  kernels
[root@YingMo src]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@YingMo src]# yum -y install mysql57-community-release-el7-10.noarch.rpm
[root@YingMo src]# ls /etc/yum.repos.d/
CentOS-Base.repo  epel.repo  mysql-community.repo  mysql-community-source.repo
#可以看到已经有了mysql源了

#接下来安装mysql,安装mysql涉及四个包,分别为mysql-community-server、mysql-community-client、mysql-community-common和mysql-community-devel
[root@YingMo src]yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel

配置mysql

#先启动mysql
[root@YingMo ~]# systemctl start mysqld
[root@YingMo ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-02-15 15:00:12 CST; 8s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 26071 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 25997 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 26075 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─26075 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Feb 15 15:00:08 YingMo systemd[1]: Starting MySQL Server...
Feb 15 15:00:12 YingMo systemd[1]: Started MySQL Server.

#确保3306端口处于监听状态
[root@YingMo ~]# ss -antl
State      Recv-Q Send-Q  Local Address:Port                 Peer Address:Port              
LISTEN     0      128                 *:22                              *:*                  
LISTEN     0      80                 :::3306                           :::*                  

#找到临时密码
[root@YingMo ~]# grep "password" /var/log/mysqld.log
2019-02-15T07:00:09.467221Z 1 [Note] A temporary password is generated for root@localhost: g(=mj,K_r1gg

#使用临时密码登陆mysql
[root@YingMo ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
#看到上面“mysql>”代表登录成功

#修改mysql登陆密码
#将密码安全性检查调到LOW
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
#将密码最大长度设置为1(因为是测试环境,所以改成了1,生产环境最好还是长一些)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'lynk';
Query OK, 0 rows affected (0.00 sec)
#如果不想改变validate_password_policy参数,保持默认,那么密码必须包含有数字、小写或大写字母、特殊字符

mysql> quit
Bye

#为了防止更新我们要卸掉最开始安装的yum源。因为更新后可能会存在不稳定的情况,这种问题还是能避免就避免一下的比较好。
[root@YingMo ~]# rpm -qa|grep mysql
mysql-community-common-5.7.25-1.el7.x86_64
mysql-community-libs-5.7.25-1.el7.x86_64
mysql-community-server-5.7.25-1.el7.x86_64
mysql57-community-release-el7-10.noarch
mysql-community-libs-compat-5.7.25-1.el7.x86_64
mysql-community-client-5.7.25-1.el7.x86_64
mysql-community-devel-5.7.25-1.el7.x86_64
[root@YingMo ~]# yum -y remove mysql57-community-release-el7-10.noarch

mysql工具的使用

#语法:mysql [OPTIONS] [database]
#常用OPTIONS:
-uUSERNAME   指定用户名,默认root
-hHOST       指定服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD   指定用户的密码
-P#          指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
-V           查看当前使用的mysql版本
-e           不登录mysql执行sql语句后退出,常用于脚本 
[root@YingMo ~]# mysql -uroot -p -h127.0.0.1
#注意:不建议在命令行中直接使用密码登陆,因为日志系统会将用户命令记录下来,导致密码泄露。建议使用-p选项,用交互式命令输入密码。

服务器监听的两种socket地址

  • ip socket:默认监听在tcp的3306端口,支持远程通讯
  • unix socket:监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)仅支持本地通讯
    • server地址只能是:localhost,127.0.0.1

应用实例

创建一个以lynk为名的数据库,创建一张表student,该表包含三个字段(id,name,age),表结构如下

mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(100) | NO   |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> use lynk;
Database changed
mysql> CREATE TABLE student (id int(11),name varchar(100),age tinyint(4) NULL);
Query OK, 0 rows affected (0.20 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  |     | NULL    |       |
| name  | varchar(100) | YES  |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.15 sec)

查看下该新建的表有无内容(用select语句)

mysql> select * from student;
Empty set (0.00 sec)

往新建的student表中插入数据(用insert语句)

mysql> insert student VALUES (1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',NULL),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20);
Query OK, 11 rows affected (0.04 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql> select * from student;                                            
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    1 | tom         |   20 |
|    2 | jerry       |   23 |
|    3 | wangqing    |   25 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    6 | zhangshan   |   20 |
|    7 | lisi        | NULL |
|    8 | chenshuo    |   10 |
|    9 | wangwu      |    3 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
11 rows in set (0.00 sec)

修改lisi的年龄为50

mysql> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.80 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;                                            
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    1 | tom         |   20 |
|    2 | jerry       |   23 |
|    3 | wangqing    |   25 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    6 | zhangshan   |   20 |
|    7 | lisi        |   50 |
|    8 | chenshuo    |   10 |
|    9 | wangwu      |    3 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
11 rows in set (0.00 sec)

以age字段降序排序

mysql> select * from student order by age desc;
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    7 | lisi        |   50 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    3 | wangqing    |   25 |
|    2 | jerry       |   23 |
|    1 | tom         |   20 |
|    6 | zhangshan   |   20 |
|   11 | qiuxiaotian |   20 |
|   10 | qiuyi       |   15 |
|    8 | chenshuo    |   10 |
|    9 | wangwu      |    3 |
+------+-------------+------+
11 rows in set (0.03 sec)

查询student表中年龄最小的3位同学

mysql> select * from student order by age limit 3;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    9 | wangwu   |    3 |
|    8 | chenshuo |   10 |
|   10 | qiuyi    |   15 |
+------+----------+------+
3 rows in set (0.00 sec)

查询student表中年龄最大的4位同学

mysql> select * from student order by age desc limit 4;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    7 | lisi      |   50 |
|    4 | sean      |   28 |
|    5 | zhangshan |   26 |
|    3 | wangqing  |   25 |
+------+-----------+------+
4 rows in set (0.00 sec)

查询student表中名字叫zhangshan的记录

mysql> select * from student where name = 'zhangshan';
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    5 | zhangshan |   26 |
|    6 | zhangshan |   20 |
+------+-----------+------+
2 rows in set (0.01 sec)

查询student表中名字叫zhangshan且年龄大于20岁的记录

mysql> select * from student where name = 'zhangshan' and age > 20;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    5 | zhangshan |   26 |
+------+-----------+------+
1 row in set (0.02 sec)

查询student表中年龄在23到30之间的记录

mysql> select * from student where age between 23 and 30;
+------+-----------+------+
| id   | name      | age  |
+------+-----------+------+
|    2 | jerry     |   23 |
|    3 | wangqing  |   25 |
|    4 | sean      |   28 |
|    5 | zhangshan |   26 |
+------+-----------+------+
4 rows in set (0.00 sec)

修改wangwu的年龄为100

mysql> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (1.35 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    1 | tom         |   20 |
|    2 | jerry       |   23 |
|    3 | wangqing    |   25 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    6 | zhangshan   |   20 |
|    7 | lisi        |   50 |
|    8 | chenshuo    |   10 |
|    9 | wangwu      |  100 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
11 rows in set (0.00 sec)

删除student中名字叫zhangshan且年龄小于等于20的记录

mysql> delete from student where name = 'zhangshan' and age <= 20;
Query OK, 1 row affected (0.06 sec)

mysql> select * from student;
+------+-------------+------+
| id   | name        | age  |
+------+-------------+------+
|    1 | tom         |   20 |
|    2 | jerry       |   23 |
|    3 | wangqing    |   25 |
|    4 | sean        |   28 |
|    5 | zhangshan   |   26 |
|    7 | lisi        |   50 |
|    8 | chenshuo    |   10 |
|    9 | wangwu      |  100 |
|   10 | qiuyi       |   15 |
|   11 | qiuxiaotian |   20 |
+------+-------------+------+
10 rows in set (0.00 sec)

posted on 2019-02-16 15:03  林克克克  阅读(739)  评论(0编辑  收藏  举报