数据库01

第一章:mysql的简介及安装升级

1. 课程体系介绍

![]Z8K{39J~RVI_L2(SZOBZV.png

2. it人的职业素养

1.人品
责任-----权利
2.严谨
磁带库----备份数据----17楼
3.请收起你的好奇心
注意规范
脱裤?  某住网(ip   root   密码) ,“脱敏”
学会保护自己
4. 细心
把最简单的事做的最漂亮,最专业
不要怕成功的门很拥挤,因为很多人在半路就已经放弃了
5.心态
别惹事,出事别怕事
6.熟悉操作系统
7.熟悉公司业务
业务?
产品的功能
用户的行为(热功能,热数据)
8.熟悉行业
熟悉行业发展趋势
版本
数据库产品类型
https://db-engines.com/en/ranking
9.喜欢数据库
喜欢很重要

3. mysql介绍也安装

3.1什么是数据
3.2数据库管理系统(DBMS)

RDBMS:关系型
NoSQL:非关系型
NewSQL:分布式型

3.3笔试:请你列举熟悉的数据库产品

RDBNS:oracle,mysql,mssql,pg
NoSQL:redis,mongodb,es
NewSQL:tidb(pingcap),spanner(googel),OceanBase(Alibaba),PolarDB(aliyun)

3.4面试你们公司用了哪些数据库产品?

MySQL,Redis,MongoDB,ES
引出 版本 MySQL 5.7.20 ###
引出 公司 数据库架构
引出 公司 备份策略
引出 公司 遇到的故障和性能问题
总结: ....

3.5 MySQL 软件分支

Oracle MySQL
MariaDB
PerconaDB
云数据(RDS MySQL 腾讯DB等)

3.6 MySQL的企业版本选择

3.6.1 企业主流版本
5.6: 5.6.34 5.6.36 5.6.38 5.6.40
5.7: 5.7.18 5.7.19 5.7.20 5.7.24
MySQL RC apl beta 不要选择,一定选择的是GA
关于版本GA时间

尽量记录两个版本的具体GA时间
5.6.38, 5.7.20 , 20170913

3.7 企业版本新环境建议

5.7 版本 GA 6-12 月版本 偶数版

4.mysql 5.7-26 二进制规划和部署


4.1 规划:
4.1.1 硬件环境
DELL R720 , CPU*2*8,MEM*128G, RAID10*8*600G SAS
4.1.2 OS
Centos 7.6.1810 , kernel 3.10.0-957.el7.x86_64
4.1.3 网络规划
eth0 : 10.0.0.51/24 
Hostname: db01
4.1.4 存储规划
/dev/sdb    20G
4.2安装
第一个历程:创建相关的目录
[root@db01 ~]# mkdir -p /application    ###软件的存放目录
[root@db01 ~]# mkdir -p /data/3306/data   ###数据的存放位置
第二个历程:创建虚拟用户
[root@db01 ~]# useradd -s /sbin/nologin -M mysql
[root@db01 ~]# id mysql
uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)
[root@db01 ~]# 
第三个历程:上传软件并解压处理
[root@db01 /application]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz 
[root@db01 /application]# ll
total 629756
drwxr-xr-x 9 root root       129 Aug  5 20:22 mysql-5.7.26-linux-glibc2.12-x86_64
-rw-r--r-- 1 root root 644869837 Aug  5 09:31 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@db01 /application]# mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
[root@db01 /application]# ll
total 629756
drwxr-xr-x 9 root root       129 Aug  5 20:22 mysql
-rw-r--r-- 1 root root 644869837 Aug  5 09:31 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
第四个历程:查看环境是否有别的数据库
[root@db01 ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@db01 ~]# yum remove mariadb-libs    -y
第五个历程:创建环境变量
[root@db01 ~]# vim /etc/profile
[root@db01 ~]# source /etc/profile
[root@db01 ~]# tail -n1 /etc/profile
export PATH=/application/mysql/bin:$PATH
[root@db01 ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using  EditLine wrapper
[root@db01 ~]# 
第六个历程:书写配置文件
[root@db01 ~]# cat /etc/my.cnf 
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data/3306/data
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
prompt=oldguo[\d]>
第七个历程:初始化数据
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/3306/datamysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

[root@db01 ~]# yum search libaio
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                    | 3.6 kB  00:00:00     
epel                                                                                    | 5.3 kB  00:00:00     
extras                                                                                  | 3.4 kB  00:00:00     
updates                                                                                 | 3.4 kB  00:00:00     
(1/7): epel/x86_64/group_gz                                                             |  88 kB  00:00:00     
(2/7): base/7/x86_64/group_gz                                                           | 166 kB  00:00:00     
(3/7): extras/7/x86_64/primary_db                                                       | 205 kB  00:00:00     
(4/7): epel/x86_64/updateinfo                                                           | 997 kB  00:00:00     
(5/7): epel/x86_64/primary_db                                                           | 6.8 MB  00:00:01     
(6/7): updates/7/x86_64/primary_db                                                      | 7.4 MB  00:00:01     
base/7/x86_64/primary_db       FAILED                                          
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/6614b3605d961a4aaec45d74ac4e5e713e517debb3ee454a1c91097955780697-primary.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; Connection refused"
Trying other mirror.
(7/7): base/7/x86_64/primary_db                                                         | 6.0 MB  00:00:00     
============================================= N/S matched: libaio =============================================
libaio.i686 : Linux-native asynchronous I/O access library
libaio.x86_64 : Linux-native asynchronous I/O access library
libaio-devel.i686 : Development files for Linux-native asynchronous I/O access
libaio-devel.x86_64 : Development files for Linux-native asynchronous I/O access

  Name and summary matches only, use "search all" for everything.
[root@db01 ~]# yum install -y libaio-devel.x86_64
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/3306/data2019-08-05T12:41:20.904619Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-08-05T12:41:22.103305Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-08-05T12:41:22.254100Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-08-05T12:41:22.358574Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5489b96b-b77e-11e9-a909-000c29cbbb28.
2019-08-05T12:41:22.369421Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-08-05T12:41:22.369951Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
第八个里程:授权

[root@db01 ~]# chown -R mysql.mysql /application
[root@db01 ~]# chown -R mysql.mysql /data
第九个历程:准备启动脚本,启动服务

[root@db01 ~]# cp /application/mysql/support-files/mysql.server /etc/init
init.d/  inittab  
[root@db01 ~]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@db01 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/3306/data/db01.err'.
 SUCCESS! 
[root@db01 ~]# netstat -lntup |grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      8437/mysqld         
[root@db01 ~]#
第十个历程:使用systemctl管理mysql
[root@db01 ~]# cat /etc/systemd/system/mysqld.service 
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

注意:将原来模式启动mysqld先关闭,然后再用systemd管理。
[root@db01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@db01 ~]# systemctl start mysql
Failed to start mysql.service: Unit not found.
[root@db01 ~]# systemctl start mysqld
[root@db01 ~]# netstat -lntup |grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      8522/mysqld         
[root@db01 ~]# 

安装过程中的细节说明

5.1 初始化命令
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/3306/data

作用: 生成系统数据到/data/3306/data中
-rw-r----- 1 mysql mysql       56 Aug  5 12:33 auto.cnf
-rw-r----- 1 mysql mysql     3159 Aug  5 12:38 db01.err
-rw-r----- 1 mysql mysql        6 Aug  5 12:38 db01.pid
-rw-r----- 1 mysql mysql      419 Aug  5 12:33 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Aug  5 12:38 ibdata1
-rw-r----- 1 mysql mysql 50331648 Aug  5 12:38 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Aug  5 12:33 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Aug  5 12:38 ibtmp1
drwxr-x--- 2 mysql mysql     4096 Aug  5 12:33 mysql
drwxr-x--- 2 mysql mysql     8192 Aug  5 12:33 performance_schema
drwxr-x--- 2 mysql mysql     8192 Aug  5 12:33 sys

参数说明:
--initialize-insecure   # 启用非安全模式,进行数据初始化
--initialize            # 启用安全模式,进行数据初始化
5.7 版本以后,加强了用户密码安全管理的功能
(1) 密码长度 
(2) 密码复杂度
(3) 初始化时自动生成密码

扩展: 
5.6 在初始化的方式
/application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3306/data

5.2  启动方式说明

systemd (centos7)

cat > /etc/systemd/system/mysqld.service <

4.3mysql5.7升级8.0

第一个历程:先进行数据备份
第二个历程:上传软件,解压软件,并改名
[root@db01 /application]# ll
total 1079692
drwxr-xr-x 9 mysql mysql       129 Aug  5 20:22 mysql
-rw-r--r-- 1 mysql mysql 644869837 Aug  5 09:31 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root  root  460733332 Aug  5 09:31 mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz
[root@db01 /application]# tar xf mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz 
[root@db01 /application]# mv mysql-8.0.16-linux-glibc2.12-x86_64 mysql8
[root@db01 /application]# ll
total 1079692
drwxr-xr-x  9 mysql mysql       129 Aug  5 20:22 mysql
-rw-r--r--  1 mysql mysql 644869837 Aug  5 09:31 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 11 root  root        194 Aug  5 21:00 mysql8
-rw-r--r--  1 root  root  460733332 Aug  5 09:31 mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz
[root@db01 /application]# 
第三个历程:停止原有的数据库
[root@db01 ~]# systemctl stop mysqld.service
第四个历程:修改配置文件,修改环境变量,修改启动脚本
[root@db01 ~]# vim /etc/profile
[root@db01 ~]# source /etc/profile
[root@db01 ~]# tail -n1 /etc/profile
export PATH=/application/mysql8/bin:$PATH
[root@db01 ~]# vim /etc/my.cnf 
[root@db01 ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/application/mysql8
datadir=/data/3306/data
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
prompt=oldguo[\d]>
[root@db01 ~]# vim /etc/systemd/system/mysqld.service 
[root@db01 ~]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql8/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@db01 ~]# 
################
[root@db01 ~]# mysql -V
mysql  Ver 8.0.16 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
[root@db01 ~]# systemctl start mysqld
[root@db01 ~]# mysql
'ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@db01 ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@db01 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.16 MySQL Community Server - GPL

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.

oldguo[(none)]>
###启动完需要等一会

备注:8.0以前版本,需要进行数据升级(此步骤在8.0版本可省略)
[root@db01 ~]# mysqladmin -uroot -p password 123
[root@db01 ~]# mysql -uroot -p

5.7 以前:
oldguo[mysql]>select user,host ,password from mysql.user;
5.7 以后:
oldguo[mysql]>select user,host ,authentication_string from mysql.user;

mysql_upgrade  
第五个历程:恢复原数据

第二章:mysql体系结构和管理

1.MSYQL C/S模型

Server : mysqld
Client :
socket : 仅本地连接使用
tcp/ip : 应用连接使用
TCP/IP方式(远程、本地):
mysql -uroot -poldboy123 -h 10.0.0.51 -P3306
Socket方式(仅本地):
mysql -uroot -poldboy123 -S /tmp/mysql.sock

2.实例

实例= mysqld ----> master thread ----> 干活的线程 + 预分配的内存结构
公司= boss 经理 员工 办公室

3. mysqld的程序结构moxing

3.1 MySQL的专用管理和操作命令SQL
DDL 数据定义语言
DCL 数据控制语言
DML 数据操作语言
DQL 数据查询语言
3.2 SQL语句的执行过程
select user,host from mysql.user;
(1) mysql -uroot -pxxxx

3.2.1 连接层
(1) 提供连接协议
sokcet
TCP/IP
(2) 验证 user password host port
(3) 提供连接线程
show [full] processlist;
功能: 接收SQL语句,返回结果.]

3.2.2 SQL层 *****
(1) 语法检查和SQL_MODE检查
(2) 语义检查和权限检查
(3) 解析预处理,生成解析树(执行计划树)
(4) 优化会根据自带算法,选择最优的方案(代价模型算法)
代价? cpu io mem
(5) 选择最优的方案进行执行SQL
(6) 提供查询缓存(默认是没开启的),会使用redis tair替代查询缓存功能
(7) 提供日志记录(日志管理章节):binlog,默认是没开启的。

3.2.3 存储引擎层(简单介绍)
相当于文件系统,将数据取出,再交给SQL层结构化成表,返回给用户.

你可能感兴趣的:(数据库01)