MySQL related introduction and MySQL8.0 installation and deployment

In the current project, MySQL is used as the database of the nacos cluster, and the MySQL database is generally used in participating in domestic projects. Therefore, one is to prepare for the follow-up preparation, study, research and deployment of nacos, and the other is to comprehensively review the relevant knowledge and information of MySQL and practice deploying MySQL Service, summarize and organize this article to prepare for further in-depth research in the future, and also provide reference for latecomers. There are inevitably omissions in the article. I hope readers can correct me, thank you very much!

1. Introduction to MySQL

1.1 Basic Information and Features

MySQL is a relational database management system developed by MySQL AB in Sweden and currently belongs to Oracle Corporation. MySQL is a relational database management system. Relational databases store data in different tables instead of putting all the data in one big warehouse, which increases speed and improves flexibility.

MySQL 是开源的,目前隶属于 Oracle 旗下产品。
MySQL 支持大型的数据库。可以处理拥有上千万条记录的大型数据库。
MySQL 使用标准的 SQL 数据语言形式。
MySQL 可以运行于多个系统上,并且支持多种语言。这些编程语言包括C、C++、Python、Java、Perl、PHP、Eiffel、Ruby 和 Tcl 等。
MySQL 对PHP有很好的支持。
MySQL 支持大型数据库,支持5000万条记录的数据仓库,32位系统表文件最大可支持4GB,64位系统支持最大的表文件为8TB。
MySQL 是可以定制的,采用了GPL协议,可以修改源码来开发自己的MySQL系统。

1.2 Storage Engine

Data in MySQL is stored in files (or in memory) using various techniques. Each of these technologies uses different storage mechanisms, indexing techniques, locking levels, and ultimately provides widely different functions and capabilities. By choosing a different technology, you can gain additional speed or functionality, thereby improving the overall functionality of your application.
For example, if you are working with large amounts of temporary data, you may need to use the in-memory MySQL storage engine. The in-memory storage engine can store all tabular data in memory. Or, maybe you need a database that supports transaction processing (to ensure the ability to roll back data when the transaction processing is not successful).
These different technologies and supporting related functions are called storage engines (also called table types) in MySQL.

  1. MyISAM storage engine
MyISAM基于ISAM存储引擎,并对其进行扩展。它是在Web、数据仓储和其他应用环境下最常使用的存储引擎之一。
MyISAM拥有较高的插入、查询速度,但不支持事务。
  1. InnoDB storage engine
InnoDB是事务型数据库的首选引擎,支持事务安全表(ACID),支持行锁定和外键,InnoDB是默认的MySQL引擎。
  1. Memory storage engine
Memory存储引擎将表中的数据存储到内存中,未查询和引用其他表数据提供快速访问。
  1. Archive storage engine
Archive是归档的意思,在归档之后很多的高级功能就不再支持了,仅仅支持最基本的插入和查询两种功能。

insert image description here
choose:

InnoDB:如果要提供提交、回滚、崩溃恢复能力的事务安全(ACID兼容)能力,并要求实现并发控制,InnoDB是一个好的选择

MyISAM:如果数据表主要用来插入和查询记录,则MyISAM(但是不支持事务)引擎能提供较高的处理效率

Memory:如果只是临时存放数据,数据量不大,并且不需要较高的数据安全性,可以选择将数据保存在内存中的Memory引擎,MySQL中使用该引擎作为临时表,存放查询的中间结果。数据的处理速度很快但是安全性不高。

Archive:如果只有INSERT和SELECT操作,可以选择Archive,Archive支持高并发的插入操作,但是本身不是事务安全的。Archive非常适合存储归档数据,如记录日志信息可以使用Archive

1.3 Transaction ACID

A transaction is a program execution unit that accesses and updates a database; a transaction may contain one or more SQL statements, which are either executed or not executed. In MySQL, only databases or tables that use the Innodb database engine support transactions.
Transactions must meet four conditions: Atomicity, Consistency, Isolation, and Durability

原子性:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节。
事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。

一致性:在事务开始之前和事务结束以后,数据库的完整性没有被破坏。
这表示写入的资料必须完全符合所有的预设规则,这包含资料的精确度、串联性以及后续数据库可以自发性地完成预定的工作。

隔离性:数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。
事务隔离分为不同级别,包括读未提交(Read uncommitted)、读已提交(read committed)、可重复读(repeatable read)和串行化(Serializable)。
隔离性要求同一时刻只能有一个事务对数据进行写操作,InnoDB通过锁机制来保证这一点。

持久性:事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。----日志(重做日志 redo log)

1.4 Index and data structure

The role of the index is to perform fast retrieval of data, and the essence of fast retrieval is the data structure. Through the selection of different data structures, various data can be quickly retrieved.
Mysql index underlying data structure selection (summarizing the advantages and disadvantages of evolution): B+ tree

(1)哈希表(Hash):
时间复杂度为 O(1),检索速度非常快。但是没办法做数据高效范围查找,因此哈希索引是不适合作为 Mysql 的底层索引的数据结构。

(2)二叉查找树(BST):
时间复杂度是 O(lgn),但是普通的二叉查找树有个致命缺点:极端情况下会退化为线性链表,二分查找也会退化为遍历查找,时间复杂退化为 O(n),检索性能急剧下降。

(3)AVL树和红黑树:
时间复杂度为 O(logn),也就保证了查找效率不会明显减低,也可以实现范围查找、数据排序。
数据库查询数据的瓶颈在于磁盘 IO,如果使用的是 AVL 树,我们每一个树节点只存储了一个数据,我们一次磁盘 IO 只能取出来一个节点上的数据加载到内存里,非常消耗时间。所以我们设计数据库索引时需要首先考虑怎么尽可能减少磁盘 IO 的次数。
磁盘 IO 有个有个特点,就是从磁盘读取 1B 数据和 1KB 数据所消耗的时间是基本一样的,我们就可以根据这个思路,我们可以在一个树节点上尽可能多地存储数据,一次磁盘 IO 就多加载点数据到内存,这就是 B 树,B+树的的设计原理了。

(4)B树 和 B+树:
时间复杂度为 O(h*logn),其中 h 为树高,n 为每个节点关键词的个数;尽可能少的磁盘 IO,加快了检索速度;可以支持范围查找。
B 树和 B+树的不同之处在于:
第一,B 树一个节点里存的是数据,而 B+树存储的是索引(地址),所以 B 树里一个节点存不了很多个数据,但是 B+树一个节点能存很多索引,B+树叶子节点存所有的数据。
第二,B+树的叶子节点是数据阶段用了一个链表串联起来,便于范围查找。

1.5 Log files

There are seven log files in MySQL, namely:
redo log (redo log), rollback log (undo log), binary log (binlog), error log (errorlog), slow query log (slow query log), general query Log (general log), relay log (relay log).

(1)重做日志(redo log)
确保事务的持久性。在事务的执行过程中,便开始写入redo log文件中。当对应事务的脏页写入到磁盘之后,redo log的使命也就完成了,重做日志占用的空间就可以重用(被覆盖)。
对应的物理文件:默认情况下,对应的物理文件位于数据库的data目录下的ib_logfile1&ib_logfile2。

(2)回滚日志(undo log)
保证数据的原子性,保存了事务发生之前的数据的一个版本,可以用于回滚。

(3)二进制日志(binlog)
用于复制,在主从复制中,从库利用主库上的binlog进行重播,实现主从同步。用于数据库的基于时间点的还原。
事务提交的时候,一次性将事务中的sql语句,按照一定的格式记录到binlog中。
对应的物理文件:配置文件的路径为log_bin_basename。

(4)错误日志(errorlog)
错误日志记录着mysqld启动和停止,以及服务器在运行过程中发生的错误的相关信息。在默认情况下,系统记录错误日志的功能是关闭的,错误信息被输出到标准错误输出。

(5)慢查询日志(slow query log)
慢日志记录执行时间过长和没有使用索引的查询语句,报错select、update、delete以及insert语句,慢日志只会记录执行成功的语句。

(6)一般查询日志(general log)
记录了服务器接收到的每一个查询或是命令,无论这些查询或是命令是否正确甚至是否包含语法错误,general log 都会将其记录下来,开启General log会产生不小的系统开销。 因此,Mysql默认是把General log关闭的。

(7)中继日志(relay log)
从数据库Slave服务的I/O线程从主数据库Master服务的二进制日志中读取数据库的更改记录并写入到中继日志中,然后在Slave数据库执行修改操作。这就是中继日志Relay Log。

2. Download and install

2.1 Installation environment

linux版本:CentOS Linux release 7.2.1511 (Core)
mysql版本:mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz

2.2 Download the installation package

Download address: https://dev.mysql.com/downloads/mysql/
insert image description here

2.2 Installation steps

1.创建安装路径以及移动安装包到安装路径下
 mkdir /usr/local/mysql
 mv mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz /usr/local/mysql/

2.解压安装包以及重命名
tar vxJf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.26-linux-glibc2.12-x86_64 mysql-8.0

3.创建存储文件夹日志文件夹
mkdir data
mkdir log

4.创建mysql用户组以及用户和密码 授权mysql用户mysql文件权限
groupadd mysql
useradd -g mysql mysql
chown -R mysql.mysql /usr/local/mysql/

5.切换bin目录下,初始化基础信息
cd mysql-8.0/bin/
./mysqld --user=mysql --basedir=/usr/local/mysql/mysql-8.0 --datadir=/usr/local/mysql/mysql-8.0/data/ --initialize
并得到临时密码:A temporary password is generated for root@localhost: ZL*sddd4bzdC

6.编辑配置文件/etc/my.cnf
注释mysqld_safe
修改信息 
[mysqld]
symbolic-links=0
basedir=/usr/local/mysql/mysql-8.0
datadir=/usr/local/mysql/mysql-8.0/data
socket=/tmp/mysql.sock
character-set-server=UTF8MB4

# 开启binlog
server-id=1
log-bin=/usr/local/mysql/mysql-8.0/log/mysql-bin

#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid

7.添加mysqld服务到系统  
cp -a ./support-files/mysql.server /etc/init.d/mysql

授权以及添加服务
  chmod +x /etc/init.d/mysql
  chkconfig --add mysql

8. 启动命令查看启动状态 
service mysql start
service mysql status
service mysql restart

9.将mysql命令添加到服务  
ln -s /usr/local/mysql-8.0/bin/mysql /usr/bin

10.登录mysql,修改密码
登录mysql
mysql -uroot -p 密码使用之前随机生成的密码

修改root密码 
alter user 'root'@'localhost' identified with mysql_native_password by '123456'; 
其中123456是新的密码自己设置

执行命令,使密码生效
flush privileges; 

11.修改远程连接并生效 
选择mysql数据库  
use mysql;
update user set host='%' where user='root';
flush privileges;

insert image description here
insert image description here

3. References

[1] https://www.mysql.com/
[2] https://www.runoob.com/mysql/mysql-tutorial.html
[3] https://zhuanlan.zhihu.com/p/113917726
[4] https://www.cnblogs.com/myseries/p/10728533.html
[5] https://blog.csdn.net/young_0609/article/details/109015666

Guess you like

Origin blog.csdn.net/shy871/article/details/120671692