当前位置:网站首页>MySQL 5.7 is about to be stopped and only maintained. It's time to learn a wave of MySQL 8
MySQL 5.7 is about to be stopped and only maintained. It's time to learn a wave of MySQL 8
2022-06-21 10:49:00 【Xianling Pavilion】
MySQL 8 New characteristics
choice MySQL 8 The background of :MySQL 5.6 Version update has been stopped , about MySQL 5.7 edition , It will be 2023 year 10 month 31 Japan Stop support . Subsequent official will not conduct subsequent code maintenance .
in addition ,MySQL 8.0 Full memory access can be easily accessed 200W QPS,I/O Extreme high load scenario 16W QPS, Here's the picture :



The above three figures are from MySQL Official website :www.mysql.com/why-mysql/b…
In addition to high performance ,MySQL 8 Many new functions have been added , I've found some new features with characteristics , Here's a summary .
This article uses MySQL Version is 8.0.29
Accounts and security
User creation and Authorization
stay MySQL Previous version , Creating a user and authorizing the created user can be completed with one statement :
grant all privileges on . to ‘zhangsan’@‘%’ identified by ‘[email protected]’;
Copy code

stay MySQL 8 in , User creation and authorization need to be performed separately , Otherwise, an error will be reported , Execution unsuccessful :
stay MySQL 8 in , Need points 2 Do not complete the operation of creating users and authorizations :
-- Create user
create user 'zhangsan'@'%' identified by '[email protected]';
-- to grant authorization
grant all privileges on *.* to 'zhangsan'@'%';
Copy code
When creating a user , The following error occurred :
It's because of my MySQL 8 After installation , Enter the command line with a temporary password , No modification root The initial password for , You need to change your password before you can operate .
Change password operation :
-- modify root password
alter user user() identified by '[email protected]';
Copy code
Then create a user :
mysql> create user 'zhangsan'@'%' identified by '[email protected]';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on *.* to 'zhangsan'@'%';
Query OK, 0 rows affected (0.00 sec)
Copy code
Authentication plug-in
stay MySQL in , It can be used show variables Command to view some settings MySQL Variable , The variable name of the password authentication plug-in is default_authentication_plugin .
MySQL 5.7 edition :
mysql> show variables like ‘%default_authentication%’;
±------------------------------±----------------------+
| Variable_name | Value |
±------------------------------±----------------------+
| default_authentication_plugin | mysql_native_password |
±------------------------------±----------------------+
1 row in set (0.02 sec)
Copy code
MySQL 8 edition :
mysql> show variables like ‘%default_authentication%’;
±------------------------------±----------------------+
| Variable_name | Value |
±------------------------------±----------------------+
| default_authentication_plugin | caching_sha2_password |
±------------------------------±----------------------+
1 row in set (0.07 sec)
Copy code
It can be seen that ,5.7 The default authentication plug-in for version is mysql_native_password , and 8.0 The default authentication plug-in for version is caching_sha2_password .
caching_sha2_password The problem with this authentication plug-in is , We connect directly on the client side MySQL It won't connect , For example, use Navicat :
We can temporarily modify the authentication plug-in to mysql_native_password , Let's see if it can be connected , Modify the command to :
mysql> alter user 'zhangsan'@'%' identified with mysql_native_password by '[email protected]';
Copy code

here , Let's take a look user The plug-in information in the table :
zhangsan The user's authentication plug-in is changed to mysql_native_password , Other authentication plug-ins are still the default caching_sha2_password .
Of course ,alter user The method of modifying plug-ins can only be used as temporary modification , But to permanently modify , It needs to be modified MySQL The configuration file /etc/my.cnf Configuration in :
And then restart MySQL The service can be .
Password management
MySQL 8 Added password management function , Start allowing restrictions on reuse of previous passwords :
Here are a few properties , among :
password_history : This variable defines the global policy , When changing the password , The password can be reused the number of times the previous password has been changed . If the value is 0( The default value is ), There is no reuse limit based on the number of password changes .eg: The value is 2, Indicates that the password cannot be changed with the latest 2 Second consistency .
password_require_current : This variable defines the global policy , Used to control whether an attempt to change the account password must specify the current password to replace . It means whether the old password needs to be verified (off Don't check 、 on check )( For non root user ).
password_reuse_interval : For previously used account passwords , This variable indicates the number of days that must elapse before the password can be reused . If the value is 0( The default value is ), There is no reuse restriction based on elapsed time .
modify password_history Overall strategy :
-- The password cannot be changed with the latest 2 Second consistency
set persist password_history=2;
Copy code
If you want to modify the user level password_history , The order is :
alter user 'zhangsan'@'%' password history 2;
Copy code

Let's change the password .
– zhangsan The original password for is [email protected], Perform password modification , Change the password to [email protected], According to the password policy, it is not allowed to connect with the latest 2 The passwords of the times are the same , It should be modified unsuccessfully
alter user 'zhangsan'@'%' identified by '[email protected]';
Copy code

If the global parameter password_history Change it to 0, For root There is no such restriction for users :
Index enhancement
MySQL 8 There are also corresponding enhancements to the index , Add a convenient test Hide index , real descending index , Also added Function index .
Hide index
MySQL 8 Start supporting hidden indexes (invisible index), Also called invisible index . Hidden indexes are not used by the optimizer , But it still needs maintenance - establish 、 Delete etc. .
Its common application scenarios are : Soft delete 、 Grayscale Publishing .
Soft delete : We often delete and create indexes online , Previous versions , If we delete the index , Later, I found that the deletion was wrong , I need to create another index , Doing so has a significant impact on performance . stay MySQL 8 We can do this in , Turn an index into a hidden index ( The index is not available , The query optimizer doesn't work either ), Finally, we will delete the index only after we confirm to delete the index .
Grayscale Publishing : It's the same thing , We want to do some tests online , You can create a hidden index first , Will not affect the current production environment , Then we passed some additional tests , There is no problem finding this index , Then change this index into a formal index , Make the online environment work .
With Hide index , It greatly facilitates our testing , It can be said to be very considerate !
Here is an example to see how to use hidden indexes .
Create a table t_test , And create a normal index idx_name , A hidden index idx_age :
create table t_test(id int, name varchar(20), age int);
create index idx_name on t_test(name);
create index idx_age on t_test(age) invisible;
Copy code
here , Look at the index information :
mysql> show index from t_test\G
*************************** 1. row ***************************
Table: t_test
Non_unique: 1
Key_name: idx_name
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
Visible: YES
Expression: NULL
*************************** 2. row ***************************
Table: t_test
Non_unique: 1
Key_name: idx_age
Seq_in_index: 1
Column_name: age
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
Visible: NO
Expression: NULL
2 rows in set (0.01 sec)
Copy code
General index of Visible The property value is OFF, Hide index as ON.
Let's take a look at MySQL How the optimizer handles these two indexes :
You can see , Hidden indexes are not used in query , It's just like without this index , that Hide index What exactly is the use of ?
Here you can use the optimizer switch –optimizer_switch ,
mysql> select @@optimizer_switch\G
*************************** 1. row ***************************
@@optimizer_switch: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,duplicateweedout=on,subquery_materialization_cost_based=on,use_index_extensions=on,condition_fanout_filter=on,derived_merge=on,use_invisible_indexes=off,skip_scan=on,hash_join=on,subquery_to_derived=off,prefer_ordering_index=on,hypergraph_optimizer=off,derived_condition_pushdown=on
Copy code
notice use_invisible_indexes The default configuration is OFF Of , Open it to see the effect :
– Setting the query optimizer at the session level allows you to see hidden indexes
set session optimizer_switch="use_invisible_indexes=on";
Copy code
Let's take a look at hidden indexes idx_age Whether to take effect :
666!!!
In this way, it will be convenient for our project to do grayscale publishing , Before the project goes online , I want to test whether the new index added is useful , You can now set it to hide the index , This will not affect the online business , Open the hidden index at the session level for testing , Turn to visible index after finding no problem .
Conversion between visible index and hidden index SQL sentence :
-- Convert to visible index
alter table t_test alter index idx_age visible;
-- Convert to hidden index
alter table t_test alter index idx_age invisible;
Copy code
descending index
MySQL 8 Support descending index :DESC Definitions in the index are no longer ignored , Instead, it causes the key values to be stored in descending order .
before , The index can be scanned in reverse order , But it reduces performance . A descending index can be scanned in a positive order , More efficient .
When the most efficient scan order mixes the ascending order of some columns with the descending order of others , The descending index also allows the optimizer to use multiple column indexes .
for instance , stay MySQL 8 and MySQL 5.7 The following table creation statements are executed in :
CREATE TABLE t (
c1 INT, c2 INT,
INDEX idx1 (c1 ASC, c2 ASC),
INDEX idx2 (c1 ASC, c2 DESC),
INDEX idx3 (c1 DESC, c2 ASC),
INDEX idx4 (c1 DESC, c2 DESC)
);
Copy code
Then look at the index information of the table :
Where are the specific uses ? Insert some data to see .
insert into t(c1, c2) values (1, 10),(2, 20),(3, 30),(4, 40),(5, 50);
Copy code

Function index
Prior to MySQL In the version , Perform function operation on the index during query , Then the index does not take effect , Based on this ,MySQL 8 Introduced in Function index .
Let's take a simple example : Create a table t2, Field c1 A common index is built on it , Field c2 Shangjian upper function ( Functions that convert letters to uppercase ) Indexes .
create table t2(c1 varchar(10), c2 varchar(10));
create index idx_c1 on t2(c1);
create index idx_c2 on t2((upper(c2)));
Copy code

adopt show index from t2\G to glance at :
Let's check the following items respectively , Look at the usage of the index :
because c1 Field is a normal index , Use upper(c1) Index optimization is not used in query , and c2 There is a functional index on the field upper(c2), You can put the whole upper(c2) As an index field , The index takes effect when querying !
The implementation principle of functional index :
The functional index is in MySQL Is equivalent to adding a new column , This column will calculate the result according to the function , Then, when using the functional index, the computed column will be used as the index , In fact, a virtual column is added , Then query according to the virtual column , So as to achieve the purpose of using the index .
atom DDL operation
MySQL 8.0 Support atomic data definition language (DDL) sentence . This function is called atom DDL. atom DDL The statement will be associated with DDL Update the associated data dictionary 、 Storage engine operations and binary log writes are combined into a single atomic operation .
The operation is either submitted , Applicable changes are persisted to the data dictionary 、 Storage engine and binary logs , Or it can be rolled back , Even if the server stops during operation .
A simple example : There are tables in the database t1, No table t2, Execute statement delete t1 and t2.
mysql> create table t1(c1 int);
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> drop table t1,t2;
ERROR 1051 (42S02): Unknown table 'test.t2'
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
Copy code
It's on it MySQL 8 The operation , You can see that this operation does not delete the table t1, What about the previous version , The following is MySQL 5.7 Do the same in version :
mysql> create table t1(c1 int);
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> drop table t1,t2;
ERROR 1051 (42S02): Unknown table 'test.t2'
mysql> show tables;
Empty set (0.00 sec)
Copy code
Although there is also an error message saying t2 Table does not exist , however t1 The table is real and has been deleted !
TIPS: If it is necessary to execute drop Table operations , Please use if exists To prevent errors when deleting nonexistent tables .
An atom DDL The operation contents include :
Update the data dictionary
Storage engine layer operation
stay binlog It's recorded in DDL operation
Support table related DDL:
database
Table space
surface
Indexed CREATE、ALTER、DROP as well as TRUNCATE TABLE
Other support DDL : Storage program 、 trigger 、 View 、UDF Of CREATE、DROP as well as ALTER sentence .
Support account management related DDL: Of users and roles CREATE、ALTER、DROP And where applicable RENAME, as well as GRANT and REVOKE sentence .
General expression (CTE)
Common Table Expressions(CTE) General expression , That is to say MySQL 8 Medium with sentence .
Take a simple example to understand .
idx Exhibition 1~10 That's ok , Can directly select 1 union select 2 …select 10 such :
select 1 as idx
UNION
select 2 as idx
UNION
select 3 as idx
UNION
select 4 as idx
UNION
select 5 as idx
UNION
select 6 as idx
UNION
select 7 as idx
UNION
select 8 as idx
UNION
select 9 as idx
UNION
select 10 as idx;
Copy code

adopt CTE expression , It can be reduced recursively to the following expression :
with recursive cte(idx) as
(
select 1
UNION
select idx+1 from cte where idx<10
)
select * from cte;
Copy code
Another example , There's a scene like this , View the hierarchical relationship of an employee , You can go through CTE Recursively find out .
other
MySQL 8 There are many more practical new features , such as :
Window Function, For each row in the query , Use the row associated with the row to perform the calculation .
JSON enhance
InnoDB Other improvements , For example, deadlock check control innodb_deadlock_detect, For highly concurrent systems , Disabling deadlock checking may lead to performance improvements .
There are no more examples here ( Is there a possibility that the author is too lazy ?), The official documents are quite detailed !
The source code attachment has been packaged and uploaded to Baidu cloud , You can download it yourself ~
link : https://pan.baidu.com/s/14G-bpVthImHD4eosZUNSFA?pwd=yu27
Extraction code : yu27
Baidu cloud link is unstable , It may fail at any time , Let's keep it tight .
If Baidu cloud link fails , Please leave me a message , When I see it, I will update it in time ~
Open source address
Code cloud address :
http://github.crmeb.net/u/defu
Github Address :
http://github.crmeb.net/u/defu
Open source is not easy ,Star Show respect , Interested friends welcome Star, Submit PR, Maintain open source projects together , Benefit more people !
Link to the original text :https://juejin.cn/post/7111255789876019208
边栏推荐
- Revenue of Fortinet will increase by 29% in 2021 founder Xieqing talks about the future of network security industry
- WCF restful+jwt authentication
- 性能优化——图片压缩、加载和格式选择
- Are you still using localstorage directly? The thinnest in the whole network: secondary encapsulation of local storage (including encryption, decryption and expiration processing)
- Optional classes, convenience functions, creating options, optional object operations, and optional streams
- Introduction to ThreadPoolExecutor
- Optimisation des performances - compression, chargement et formatage des images
- 记一次协程环境下类成员变量污染的问题
- 国金证券开户安全吗?
- Handling of legal instruction problems
猜你喜欢

中国国际电子商务中心与易观分析联合发布:2021年4季度全国网络零售发展指数同比增长0.6%

New year's Eve, are you still changing the bug?

《Feature-metric Loss for Self-supervised Learning of Depth and Egomotion》论文笔记

Esp8266/esp32 +1.3 "or 0.96" IIC OLED pointer clock

Huawei releases wireless innovative products and solutions to build 5gigaverse Society

Matplotlib 绘制圆环图的两种方法!

年轻人不愿换手机,因选择了更耐用的iPhone,国产手机参数论失效

The backbone of the top 100 security companies! Meichuang technology was selected into the 2022 China top 100 Digital Security Report

中部“第一城”,网安长沙以何安网?

ES复合查询工作量评估
随机推荐
Research and implementation of embedded software framework based on multi process architecture
AI越进化越跟人类大脑像!Meta找到了机器的“前额叶皮层”,AI学者和神经科学家都惊了...
Cvte side
The execution process before executing the main function after the DSP chip is powered on
知识点滴 - 什么是加速移动网页(AMP)?
Summary of embedded development -- General Catalog
abnormal
Is it safe for Guojin securities to open an account?
聊聊大火的多模态项目
Running view of program
wangeditor封装插件初步
06. Redis log: the trump card for fast recovery without fear of downtime
K-means introduction
Port occupancy
基因型填充前的质控条件简介
Optional classes, convenience functions, creating options, optional object operations, and optional streams
开课报名|「Takin开源特训营」第一期来啦!手把手教你搞定全链路压测!
Redis core: usage specification
WCF RestFul+JWT身份验证
The bilingual live broadcast of Oriental selection is popular, and the transformation of New Oriental is beginning to take shape