小蛙初识MySQL

工欲善其事必先利其器,来~先装个mysql(Mac电脑)

这边要先了解一个观念,mysql是由两个部分组成(client端与server端)
装完mysql server端起了服务之後
再透过mysql -u root -p指令连线到client端介面去操作(新增、删除、修改资料)
(client端介面常见的有phpmyadmin、mysql workbench等,皆免费软件,可自行下载)

Server端安装与启动

在终端机执行以下命令

brew install mysql

由於我已安装过,所以是升级版本到8.0.26

mysql 8.0.25_1 is installed but outdated
==> Upgrading mysql
  8.0.25_1 -> 8.0.26 

启动mysql

brew services start mysql

执行mysql_secure_installation设定

Securing the MySQL server deployment.

Enter password for user root: 
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : no

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : yes
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : no

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

### Client端
执行mysql -u root -p 输入刚才设定的密码成功连进去
➜  bin mysql -u root -p

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.25 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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>

过去我较常用的是phpmyadmin,这次尝试使用mysql workbench
https://ithelp.ithome.com.tw/upload/images/20210902/20140826OKkEPFg3n8.png
登入後的样子如下
https://ithelp.ithome.com.tw/upload/images/20210902/20140826H1VK62N1vM.png

take a break


接下来很重要
如果想更上一层楼
就要弄清楚client端连接到server端的具体细节到底做了什麽?
第一步 初始连接做了什麽?
在client端连接到server端的时候,server端都会配置一个执行绪给它使用,而在连线断开的时候并不会直接释放掉,而是快取起来让下一个连线进来的人使用,是为了避免大量配置执行绪拖垮效能。

第二步 如何处理?
(1)查询快取
先知道mysql很聪明,当查询完全一样(空格、注解、大小写都列入判断)的时候,它是会快取起来给所有的client端共用,进而增加效能,但为了维护这个快取也是会额外造成系统负担,所以在mysql8.0後这个快取机制取消掉了。
所以变笨了?当然不是。而是官方希望我们透过Server端查询重写ProxySQL机制来做快取。

(2)语法解析
当查询没有快取命中时,就进入正式查询阶段。
client传送给server的请求实际上只是一段文字。server会判断语法是否正确,然後将要查询的表、各种查询准则提取出来并储存。

(3)查询最佳化
此前server已获得需要的资讯(查询的表、各种查询准则等等),但这样还不够,因其效率可能不是很好,所以会在做查询最佳化(之後会进一步说明怎麽做)以增加效率

第三步 储存引擎?
在查询最佳化前都还没有真实存取资料,一直执行到储存引擎这才会真实的去存取资料。
我们知道表是由一笔一笔的资料构成的。而资料如何呈现、如何读取表中的资料、如何将资料写入物理记忆体皆由储存引擎在负责。常见的储存引擎有InnoDB、MyISAM、Memory,可以针对不同的表设定不同的储存引擎(不同的储存结构及存取方式)。

可以下show engines;看看现有的引擎
https://ithelp.ithome.com.tw/upload/images/20210902/201408265IWcROF53M.png
(额外说明:为何叫储存引擎?
这只是个比较酷的名子,以前叫表处理器(太土了),叫这个好像比较高大上
)

人们通常会将处理请求的过程分成server层(第一步、第二步)及储存引擎层(第三步)
储存引擎层为server层提供一个统一呼叫的介面,让前面两个步骤完成後去呼叫底层介面取得资料丢回给使用者。

到这整个流程结束。喔耶去吃苍蝇吧。

我们今天安装了mysql并对整个请求流程具体步骤有了初步的了解
接下来会进一步说明mysql server的相关重要设定(像是client端的连线数量、预设的储存引擎、快取的大小等)
敬请期待


<<:  2.建立unity专案、页面简介、建立角色

>>:  C语言和你 SAY HELLO!!

Day 0x8 UVa10193 All You Need Is Love

Virtual Judge ZeroJudge 题意 输入两字串 S1、S2,问是否能找出对两字串...

[Day 30] 完赛心得

前言 很开心能够确实每天发文,并且持续30天成功完赛! 虽然这些天的发文大多都是过去学习中累计下来的...

谈谈 Spring boot Controller API 怎麽设计

说到了 controller 就不得不说一下 API,简单来说就是负责建立客户所需的内容和产生所需回...

Chapter3 今天来学习画一棵树(I)学学人家DOM 自己用递回做一个树状图结构

你是说...树吗? 嘿~丢!铁人赛至今已经过半,实在是油尽灯枯,想不到主题了,刚好看到这两个很赞的树...

Day18-Go错误处理(上)

前言 在执行程序时,遇到系统错误或是网路异常是无可避免的,这时我们可能印出错误讯息并且让程序中断。 ...