一、MySQL介绍
1、什么是数据
数据:文字、图片、视频。。。人类认知的数据表现方式
计算机:二进制、16进制的机器语言
基于数据的重要性和复杂性的不同,我们可能有不同的管理方式。
什么数据适合存储到数据库?
重要性比较高的
关系较复杂的数据
2、数据库管理系统(DBMS)
RDBMS | NoSQL | NewSQL |
---|---|---|
关系型数据库管理系统 | 非关系型数据库管理系统 | 分布式 |
Oracle、MySQL、PG、MSSQL | MongoDB、Redis、ES | TiDB,Spanner ,AliSQL ,OB ,PolarDB |
比较适用于安全级别要求高的数据以及关系较复杂的数据 | 适合于高性能存储数据,一般是配合RDBMS进行使用的 | 针对大数量处理分析,分布式架构更加擅长 |
3、MySQL企业版本GA选择
版本 | 具体 |
---|---|
5.6 | 5.6.34、 5.6.36、 5.6.40、 5.6.38(2017913) |
5.7 | 5.7.18、 5.7.24、 5.7.26、 5.7.20(2017913) |
8.0 | 8014、 8015、 8016 |
二、MySQL 5.7.26 二进制版本安装
1、下载并上传软件至/server/tools
[root@db01 ~]# mkdir -p /server/tools #创建目录
[root@db01 ~]# cd /server/tools/ #移动到目录中
[root@db01 /server/tools]# yum install -y lrzsz
[root@san/server/tools]# ll #上传之后查看
总用量 632452
-rw-r--r-- 1 root root 644869837 6月 13 11:30 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
2、解压软件
[root@san/server/tools]# mkdir -p /application #创建目录
[root@san/server/tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz #解压
[root@san/server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql #移动并改名
3、用户的创建、处理原始环境
[root@san/server/tools]# id mysql #查看用户是否存在,不存在则创建
id: mysql: no such user
[root@san/server/tools]# useradd -s /sbin/nilogin mysql
[root@san/server/tools]# rpm -qa|grep mariadb #查看mariadb是否存在,存在则卸载
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@san/server/tools]# yum remove mariadb-libs-5.5.60-1.el7_5.x86_64 -y #卸载mariadb
[root@san/server/tools]# rpm -qa |grep mariadb #再次查看
[root@san/server/tools]#
4、设置环境变量
[root@san/server/tools]# vim /etc/profile
export PATH=/application/mysql/bin:$PATH #配置的内容
[root@san/server/tools]# source /etc/profile #使环境变量生效
[root@san/server/tools]# mysql -V #检查一下是否生效,出现如下内容则生效
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
5、添加磁盘并授权
1.添加一块磁盘
2.格式化并挂载
[root@san~]# fdisk -l
磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000d9bef
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 1845247 614400 82 Linux swap / Solaris
/dev/sda3 1845248 41943039 20048896 83 Linux
磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@san~]# mkfs.xfs /dev/sdb #格式化
meta-data=/dev/sdb isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@san~]# mkfs.xfs /dev/sdb
[root@san~]# mkdir /data
[root@san~]# blkid
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="eb072bde-e0f7-4791-8de6-77f9f397f095" TYPE="xfs"
/dev/sda2: UUID="4c2ec514-31b2-4914-855c-52a3f9034233" TYPE="swap"
/dev/sda3: UUID="b04c77ed-b8a9-4f5e-8a83-d43f2a1ca828" TYPE="xfs"
/dev/sdb: UUID="10f25271-9977-455b-8fc5-a03808e026d7" TYPE="xfs"
[root@san~]# tail -1 /etc/fstab
UUID="10f25271-9977-455b-8fc5-a03808e026d7" /data xfs defaults 0 0 #添加到内容到/etc/fstab
[root@san~]# mount -a
[root@san~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 20G 5.2G 15G 27% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 297M 138M 159M 47% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb 5.0G 33M 5.0G 1% /data
3.授权
[root@san~]# mkdir /data/mysql/data -p
[root@san~]# chown -R mysql.mysql /application/*
[root@san~]# chown -R mysql.mysql /data
6、初始化数据(创建系统数据)
[root@san~]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2019-06-13T11:42:36.754439Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-13T11:43:02.699915Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-13T11:43:05.550272Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-13T11:43:05.717652Z 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: 687be90d-8dd0-11e9-92bc-000c296526fd.
2019-06-13T11:43:05.750416Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-13T11:43:05.770806Z 1 [Note] A temporary password is generated for root@localhost: OgiC0EHbxf?n
如果有如下报错:
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决方法:
[root@db01 ~]# yum install -y libaio-devel
--initialize 参数:
1.对于密码复杂度进行定制:12位,4种
2.密码过期时间:180
3.给root@localhost用户设置临时密码
--initialize-insecure 参数:
无限制,无临时密码
重新初始化:
[root@san~]# \rm -rf /data/mysql/data/*
[root@san~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2019-06-13T11:49:52.266154Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-13T11:49:55.237230Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-13T11:49:55.353834Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-13T11:49:55.411138Z 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: 5cae2e9e-8dd1-11e9-9e8c-000c296526fd.
2019-06-13T11:49:55.413023Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-13T11:49:55.414734Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@san~]#
7、配置文件
[root@san~]# cat>/etc/my.cnf<<EOF
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
8、启动数据库
方法一:service
[root@san/application/mysql/support-files]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@san/application/mysql/support-files]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@san/application/mysql/support-files]# ps -ef|grep mysql
root 7920 7293 0 19:59 pts/0 00:00:00 grep --color=auto mysql
[root@san/application/mysql/support-files]# service mysqld start
Starting MySQL. SUCCESS!
[root@san/application/mysql/support-files]# ps -ef|grep mysql
root 7953 1 0 20:00 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/san.pid
mysql 8121 7953 10 20:00 pts/0 00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/data/mysql/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=san.err --pid-file=/data/mysql/data/san.pid --socket=/tmp/mysql.sock --port=3306
root 8151 7293 0 20:00 pts/0 00:00:00 grep --color=auto mysql
[root@san/application/mysql/support-files]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
方法二:systemd
[root@san/application/mysql/support-files]# systemctl start mysqld
[root@san/application/mysql/support-files]# ps -ef|grep mysql
mysql 8508 1 2 20:05 ? 00:00:00 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
root 8537 7293 0 20:05 pts/0 00:00:00 grep --color=auto mysql
[root@san/application/mysql/support-files]# systemctl stop mysqld
[root@san/application/mysql/support-files]# ps -ef|grep mysql
root 8545 7293 0 20:05 pts/0 00:00:00 grep --color=auto mysql
[root@san/application/mysql/support-files]# systemctl restart mysqld
三、如何分析处理MySQL数据库无法启动
1、without updating PID 类似错误
查看日志:
日志位置:
/data/mysql/data/主机名.err
[ERROR] 上下文
可能情况:
/etc/my.cnf 路径位置错误
/tmp/mysql.sock文件修改过
数据目录权限不是mysql
参数改错了……
四、管理员密码的设定
[root@san~]# mysqladmin -uroot -p password oldboy123
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@san~]#
[root@san~]# mysql -uroot -p
Enter password: 输密码
管理员用户密码忘记了
1.关闭数据库
[root@san~]# systemctl stop mysqld
[root@san~]# ps -ef|grep mysql
root 8939 7293 0 20:30 pts/0 00:00:00 grep --color=auto mysql
2.启动数据库到维护模式
[root@san~]# mysqld_safe --skip-grant-tables --skip-networking &
[1] 8942
[root@san~]# 2019-06-13T12:31:14.677790Z mysqld_safe Logging to '/data/mysql/data/san.err'.
2019-06-13T12:31:14.702100Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
3.登录并修改密码
[root@san~]#
[root@san~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 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.
mysql> alter user root@'localhost' identified by '123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
五、MySQL体系结构与管理
5.1、MySQL C/S结构介绍
两种连接方式:
1.TCP/IP:
[root@san~]# mysql -uroot -p123 -h 10.0.0.51 -p3306
2.Socket:
[root@san~]# mysql -uroot -p123 -S /tmp/mysql.sock
5.2、MySQL实例的构成
5.3、MySQL中mysqld服务器近程结构
1.SQL语句引入
结构化的查询语言:
DQL:数据查询语言
DDL:数据定义语言
DML:属于操作语言
DCL:数据控制语言
mysql> select user,host from mysql.user;
5.3.2.连接层
(1)提供连接协议
Socket
TCPIP
(2)验证用户名(root@locahost)密码合法性,进行匹配专门的授权表
(3)派生一个专用连接线程(接收SQL,返回结果)
mysql> show processlist;
5.3.3.SQL层(优化方面至关重要的)
(1)验证SQL语法和SQL_MODE
(2)验证语义
(3)验证权限
(4)解析器进行语句解析,生成执行计划(解析树)
(5)优化器(各种算法,基于执行代价),根据算法,找到代价最低的执行计划
代价:CPU IO MEM
(6)执行器按照优化器选择执行计划,执行SQL语句,得出获取数据的方法
(7)提供query cache(默认不开),一般不开,会用redis
(8)记录操作日志(binlog),默认不开
……
未完待续
5.3.4.存储引擎层
真正和磁盘打交道的一个层次
根据SQL层提供的获取数据的方法,拿到数据,返回给SQL,结构化成表,再给连续层线程返回给用户
网友评论