æ°çæ´å®å ¨æ´å¿«çç认è¯æ¹å¼
å¯ç 管ç
5.7ä¸æ å¯ç 管ç
æ°å¢ä¸ä¸ªå¯ç 管çç¸å ³é 置项
1 2 3 4 5 | Â Â |
è§è²ç®¡ç
MySQLè§è²æ¯æå®çæééå.
åç¨æ·å¸æ·ä¸æ ·ï¼è§è²å¯ä»¥æ¥ææäºåæ¤æ¶çæé:
- å¯ä»¥æäºç¨æ·å¸æ·è§è²ï¼æäºè¯¥å¸æ·ä¸æ¯ä¸ªè§è²ç¸å ³çæé
- ç¨æ·è¢«æäºè§è²æéï¼å该ç¨æ·æ¥æ该è§è²çæéã
以ä¸å表æ»ç»äºMySQLæä¾çè§è²ç®¡çåè½ï¼
- CREATE ROLE并 DROP ROLEè§è²å建åå é¤ï¼
- GRANT并 REVOKE为ç¨æ·åè§è²åé åæ¤éæéï¼
- SHOW GRANTS æ¾ç¤ºç¨æ·åè§è²çæéåè§è²åé ï¼
- SET DEFAULT ROLE æå®åªäºå¸æ·è§è²é»è®¤å¤äºæ´»å¨ç¶æï¼
- SET ROLE æ´æ¹å½åä¼è¯ä¸çæ´»å¨è§è²ã
- CURRENT_ROLE()åè½æ¾ç¤ºå½åä¼è¯ä¸çæ´»å¨è§è²ã
å建è§è²å¹¶æäºç¨æ·è§è²æé
èèå¦ä¸å ç§åºæ¯ï¼
åºç¨ç¨åºä½¿ç¨å为app_dbçæ°æ®åº ã
ä¸åºç¨ç¨åºç¸å ³èï¼å¯ä»¥ä¸ºå建åç»´æ¤åºç¨ç¨åºçå¼å人å以å管çåè´¦æ·ã
å¼å人åéè¦å®å ¨è®¿é®æ°æ®åºãæçç¨æ·åªéè¦è¯»åæéï¼æçç¨æ·éè¦è¯»å/åå ¥æéã
ä¸ºæ¸ æ¥åºåè§è²çæéï¼å°è§è²å建为æéæééçå称ãéè¿ææéå½çè§è²ï¼å¯ä»¥è½»æ¾å°ä¸ºç¨æ·å¸æ·æäºæéçæéã
å建è§è²ï¼CREATE ROLE
1 | |
è§è²å称ä¸ç¨æ·å¸æ·å称é常ç¸ä¼¼ï¼ç±æ ¼å¼ä¸çç¨æ·é¨åå主æºé¨åç»æ.
主æºé¨åï¼å¦æçç¥ï¼åé»è®¤ä¸º%ãç¨æ·å主æºé¨åå¯ä»¥ä¸å å¼å·ï¼é¤éå®ä»¬å å«ç¹æ®å符
ä¸å¸æ·å称ä¸åï¼è§è²å称ç
ç¨æ·é¨åä¸è½ä¸ºç©º
为è§è²åé æéï¼ä½¿ç¨ä¸ä¸ºç¨æ·åé æéç¸åçè¯æ³æ§è¡ï¼
1 2 3 | |
ç°å¨å设æåéè¦ä¸ä¸ªå¼å人åå¸æ·ï¼ä¸¤ä¸ªéè¦åªè¯»è®¿é®æçç¨æ·ä»¥åä¸ä¸ªéè¦è¯»å/åå ¥æéçç¨æ·.
使ç¨CREATEUSERå建ç¨æ·ï¼
1 2 3 4 | |
è¦ä¸ºæ¯ä¸ªç¨æ·åé å ¶æéçæéï¼å¯ä»¥ä½¿ç¨GRANTä¸åææ¾ç¤ºçå½¢å¼ç¸åçè¯å¥ï¼ä½è¿éè¦å举æ¯ä¸ªç¨æ·ç个人æéãç¸åï¼ä½¿ç¨GRANTå 许ææè§è²èéæéçæ¿ä»£è¯æ³ï¼
1 2 3 | |
ç»åè§è²æéç读åååå ¥æéï¼å¨GRANTä¸ææ rw_user1ç¨æ·è¯»åååå ¥çè§è²ã
å¨GRANTææè§è²çè¯æ³åææç¨æ·çè¯æ³ä¸åï¼æä¸ä¸ªONæ¥åºåè§è²åç¨æ·çææï¼æONç为ç¨æ·ææï¼è没æONç¨æ¥åé è§è²ã
ç±äºè¯æ³ä¸åï¼å æ¤ä¸è½å¨åä¸è¯å¥ä¸æ··ååé ç¨æ·æéåè§è²ãï¼å 许为ç¨æ·åé æéåè§è²ï¼ä½å¿ 须使ç¨åç¬çGRANTè¯å¥ï¼æ¯ç§è¯å¥çè¯æ³é½è¦ä¸ææçå 容ç¸å¹é ãï¼
æ£æ¥è§è²æé
è¦éªè¯åé ç»ç¨æ·çæéï¼ä½¿ç¨ SHOW GRANTS
1 2 3 4 5 6 7 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
ä½æ¯ï¼å®ä¼æ¾ç¤ºæ¯ä¸ªæäºçè§è²ï¼èä¸ä¼å°å ¶æ¾ç¤ºä¸ºè§è²æ代表çæéãå¦æè¦æ¾ç¤ºè§è²æéï¼æ·»å ä¸ä¸ª USINGæ¥æ¾ç¤ºï¼
1 2 3 4 5 6 7 8 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
åæ ·éªè¯å ¶ä»ç±»åçç¨æ·ï¼
mysql> SHOW GRANTS FOR âread_user1â@âlocalhostâ USING âapp_readâ;
±-------------------------------------------------------+
| Grants for [email protected] |
±-------------------------------------------------------+
| GRANT USAGE ON . TOÂ
read_user1
@
localhost
 |
| GRANT SELECT ONÂ
app_db
.* TOÂ
read_user1
@
localhost
 |
| GRANTÂ
app_read
@
%
 TOÂ
read_user1
@
localhost
 |
±-------------------------------------------------------+
mysql> SHOW GRANTS FOR ârw_user1â@âlocalhostâ USING âapp_readâ, âapp_writeâ;
±-----------------------------------------------------------------------------+
| Grants for [email protected] |
±-----------------------------------------------------------------------------+
| GRANT USAGE ON . TOÂ
rw_user1
@
localhost
 |
| GRANT SELECT, INSERT, UPDATE, DELETE ONÂ
app_db
.* TOÂ
rw_user1
@
localhost
 |
| GRANTÂ
app_read
@
%
,
app_write
@
%
 TOÂ
rw_user1
@
localhost
 |
±-----------------------------------------------------------------------------+
2.3 æ¤æ¶è§è²æè§è²æé
æ£å¦å¯ä»¥æææ个ç¨æ·çè§è²ä¸æ ·ï¼å¯ä»¥ä»å¸æ·ä¸æ¤éè¿äºè§è²ï¼
REVOKE role FROM user;
REVOKEå¯ä»¥ç¨äºè§è²ä¿®æ¹è§è²æéãè¿ä¸ä» å½±åè§è²æ¬èº«æéï¼è¿å½±åä»»ä½æäºè¯¥è§è²çç¨æ·æéãå设æ³ä¸´æ¶è®©ææç¨æ·åªè¯»ï¼ä½¿ç¨REVOKEä»è¯¥app_writeè§è²ä¸æ¤æ¶ä¿®æ¹æé ï¼
REVOKE INSERT, UPDATE, DELETE ON app_db.* FROM âapp_writeâ;
碰巧ï¼æ个è§è²å®å ¨æ²¡æä»»ä½æéï¼æ£å¦å¯ä»¥çå°çé£æ ·SHOW GRANTS ï¼è¿ä¸ªè¯å¥å¯ä»¥åè§è²ä¸èµ·ä½¿ç¨ï¼èä¸ä» ä» æ¯æ¥è¯¢ç¨æ·æéå¯ç¨ï¼ï¼
mysql> SHOW GRANTS FOR âapp_writeâ;
±--------------------------------------+
| Grants for [email protected]% |
±--------------------------------------+
| GRANT USAGE ON . TOÂ
app_write
@
%
 |
±--------------------------------------+
ä»è§è²ä¸æ¤éæéä¼å½±åå°è¯¥è§è²ä¸ä»»ä½ç¨æ·çæéï¼å æ¤ rw_user1ç°å¨å·²ç»æ²¡æ表修æ¹æéï¼INSERTï¼ UPDATEï¼å DELETEæéå·²ç»æ²¡æäºï¼ï¼
mysql> SHOW GRANTS FOR ârw_user1â@âlocalhostâ
USING âapp_readâ, âapp_writeâ;
±---------------------------------------------------------------+
| Grants for [email protected] |
±---------------------------------------------------------------+
| GRANT USAGE ON . TOÂ
rw_user1
@
localhost
 |
| GRANT SELECT ONÂ
app_db
.* TOÂ
rw_user1
@
localhost
 |
| GRANTÂ
app_read
@
%
,
app_write
@
%
 TOÂ
rw_user1
@
localhost
 |
±---------------------------------------------------------------+
å®é ä¸ï¼rw_user1读/åç¨æ·å·²æ为åªè¯»ç¨æ·ã对äºè¢«æäºapp_writeè§è²çä»»ä½å ¶ä»ç¨æ·ä¹ä¼åçè¿ç§æ åµï¼è¯´æä¿®æ¹ä½¿ç¨è§è²èä¸å¿ ä¿®æ¹ä¸ªäººå¸æ·çæéã
è¦æ¢å¤è§è²çä¿®æ¹æéï¼åªééæ°æäºå®ä»¬å³å¯ï¼
GRANT INSERT, UPDATE, DELETE ON app_db.* TO âapp_writeâ;
ç°å¨rw_user1åæ¬¡å ·æä¿®æ¹æéï¼å°±åææ该app_writeè§è²çå ¶ä»ä»»ä½å¸æ·ä¸æ ·ã
2.4 å é¤è§è²
è¦å é¤è§è²ï¼è¯·ä½¿ç¨DROP ROLEï¼
DROP ROLE âapp_readâ, âapp_writeâ;
å é¤è§è²ä¼ä»ææå®çæ¯ä¸ªå¸æ·ä¸æ¤æ¶è¯¥è§è²ã
2.5 è§è²åç¨æ·å¨å®é ä¸çåºç¨
å设éçåºç¨å¼å项ç®å¨MySQLä¸çè§è²åºç°ä¹åå¼å§ï¼å æ¤ä¸è¯¥é¡¹ç®ç¸å ³èçææç¨æ·é½æ¯ç´æ¥æäºæéï¼èä¸æ¯æäºè§è²æéï¼ãå ¶ä¸ä¸ä¸ªå¸æ·æ¯æå被æäºæéçå¼åè ç¨æ·ï¼å¦ä¸æ示ï¼
CREATE USER âold_app_devâ@âlocalhostâ IDENTIFIED BY âold_app_devpassâ;
GRANT ALL ON old_app.* TO âold_app_devâ@âlocalhostâ;
å¦ææ¤å¼å人å离å¼é¡¹ç®ï¼åæå¿ è¦å°æéåé ç»å ¶ä»ç¨æ·ï¼æè 项ç®åä¸äººå¢å¤ï¼åå¯è½éè¦å¤ä¸ªç¨æ·ã以ä¸æ¯è§£å³è¯¥é®é¢çä¸äºæ¹æ³ï¼
ä¸ä½¿ç¨è§è²ï¼æ´æ¹å¸æ·å¯ç ï¼ä»¥ä¾¿åå§å¼å人åä¸è½ä½¿ç¨å®ï¼å¹¶è®©æ°çå¼å人å使ç¨è¯¥å¸æ·ï¼
ALTER USER âold_app_devâ@âlocalhostâ IDENTIFIED BY ânew_passwordâ;
使ç¨è§è²ï¼éå®å¸æ·ä»¥é²æ¢ä»»ä½äººä½¿ç¨å®æ¥è¿æ¥æå¡å¨ï¼
ALTER USER âold_app_devâ@âlocalhostâ ACCOUNT LOCK;
ç¶åå°è¯¥å¸æ·è§ä¸ºè§è²ã对äºæ¯ä¸ªæ°å¼å项ç®çå¼åè ï¼å建ä¸ä¸ªæ°å¸æ·å¹¶æäºå ¶åå§å¼åè å¸æ·ï¼
CREATE USER ânew_app_dev1â@âlocalhostâ IDENTIFIED BY ânew_passwordâ;
GRANT âold_app_devâ@âlocalhostâ TO ânew_app_dev1â@âlocalhostâ;
å ¶æææ¯å°åå§å¼åè å¸æ·æéåé ç»æ°å¸æ·ã
MySQL8.0çç¨æ·åè§è²ç®¡çä¹è¶æ¥è¶åOracleäºï¼8.0ä¸æä¸å°æ°çç¹æ§ï¼ååè¿æ¯å¾å¤§çï¼éè¦DBAä¸æçå¦ä¹ åæµè¯ï¼æ´æ°å¯¹MySQLæ°çç认ç¥ï¼æ´å¥½å°è¿ç»´MySQLæ°æ®åºãæªæ¥MySQLæ°æ®åºèªæ²»åæºè½æ°æ®åºæ¯å¿ ç¶åå±è¶å¿ï¼å¯¹DBAæ¥è¯´æ¯è§£æ¾ï¼ä¹æ¯ææã
åæ¶ä¹é常æ谢好åç¥åMySQLæ°æ®åºä¸å®¶å´ç³é¡èå¸å¨ç¾å¿ä¸æ½ç©ºå¯¹æ¬æè¿è¡æ ¡å¯¹ã
ä¼åå¨ç´¢å¼
éèç´¢å¼
MySQL 8.0æä¸ä¸ªç§°ä¸ºâéèç´¢å¼âçæ°åè½ï¼å®å 许快éå¯ç¨/ç¦ç¨MySQL Optimizer使ç¨çç´¢å¼ã
æä»ä¹ç¨ï¼
ä¸æ¯å¦æä½ æ³å é¤ä¸ä¸ªç´¢å¼ï¼ä½åæ³äºå ç¥éææãä½ å°±å¯ä»¥ä½¿å®å¯¹ä¼åç¨åºä¸å¯è§ãè¿æ¯ä¸ä¸ªå¿«éçå æ°æ®æ´æ¹ï¼ä½¿ç´¢å¼ä¸å¯è§ãä¸æ¦ç¡®å®æ²¡ææ§è½ä¸éï¼å°±å¯ä»¥çæ£å»å é¤ç´¢å¼ã
å ³é®çä¸ç¹æ¯ï¼éèç´¢å¼ä¸è½ä¾ä¼åå¨ä½¿ç¨ï¼ä½å®ä»ç¶åå¨ï¼å¹¶éè¿åå ¥æä½ä¿æææ°ãå³ä¾¿æ们å°è¯âFORCE INDEXâï¼ä¼åå¨ä¹ä¸ä¼ä½¿ç¨å®ï¼è½ç¶æ认为æ们åºè¯¥è½å¤å¨æç§ç¨åº¦ä¸å¼ºå¶å®ãå¯è½ä¼æè¿æ ·çæ åµï¼
æ们å¯ä»¥å建ä¸ä¸ªæ°çé形索å¼ï¼ä½å¦ææ³è¦æµè¯å®ï¼å¿ 须使å®å¯è§ãè¿æå³çææ对åºç¨ç¨åºæå³æ¶å½±åçæ¥è¯¢é½å°è½å¤ä½¿ç¨å®ãå¦æç®çåªæ¯æ³æµè¯å®ï¼æä¸è®¤ä¸ºè¿æ¯æ好çæ¹æ³ï¼ä¸æ¯ææ人çæå¡å¨ä¸é½æç¸åçæ°æ®å¤§å°åçå®æ°æ®ã强å¶éèç´¢å¼è¿æ¶åå¯è½ä¼å¾æç¨ã
ä½ æ许å¤ç´¢å¼ï¼ä½ä¸ç¡®å®åªä¸ä¸ªæªä½¿ç¨ãä½ å¯ä»¥å°ä¸ä¸ªç´¢å¼æ´æ¹ä¸ºä¸å¯è§ï¼ä»¥æ¥çæ¯å¦åå¨ä»»ä½æ§è½ä¸éãå¦ææ¯ï¼ä½ å¯ä»¥ç«å³æ´æ¹ã
ä½ å¯è½æä¸ä¸ªç¹æ®æ åµï¼åªæä¸ä¸ªæ¥è¯¢å¯ä»¥ä½¿ç¨è¯¥ç´¢å¼ãå¨è¿ç§æ åµä¸ï¼éèç´¢å¼å¯è½æ¯ä¸ä¸ªå¾å¥½ç解å³æ¹æ¡ã
å建éèç´¢å¼
æ两个é项
- æ们å¯ä»¥å建ä¸ä¸ªå ·æéèç´¢å¼ç表
-
æè æ们å¯ä»¥ä½¿ç¨alter table并å°ç´¢å¼æ´æ¹ä¸ºéè
![](https://img-blog.csdnimg.cn/20190310004149713.png)
使ç¨éèç´¢å¼
å¦ææ们ç°å¨è¦å é¤ç´¢å¼ï¼æ们å¯ä»¥å°å ¶æ´æ¹ä¸ºéèã ä½æ¯ä½¿ç¨âFORCE / USE INDEXâçæ¥è¯¢æä¹æ ·ï¼ ä»ä»¬æ¯å¦ä¼æåºä¸ä¸ªéè¯¯ï¼ å¦æ强å¶ä¸åå¨çç´¢å¼ï¼ä½ ä¼æ¶å°é误ã ä½ ä¸ä¼çå°éèç´¢å¼çé误ã ä¼åå¨ä¸ä¼ä½¿ç¨å®ï¼ä½ç¥éå®åå¨ã
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | Â Â Â Â Â Â Â Â Â Â Â Â Â |
æ£å¦ä½ æçå°çï¼å¦ææ们使ç¨å¸¦æéèç´¢å¼çâFORCE INDEXâï¼MySQLä¼æ§è¡å ¨è¡¨æ«æã MySQLä¸ä¼æåºä»»ä½é误ï¼å 为索å¼åå¨ï¼ä½å®ä¸å¯è§ã å³ä½¿æå¦ä¸ä¸ªå¯ç¨çç´¢å¼ï¼å®ä¹å°æ§è¡å ¨è¡¨æ«æã å¨å¤§å表ä¸ï¼è¿å¯è½ä¼å¯¼è´ä¸¥éçæ§è½é®é¢ã å³ä½¿MySQLå¨æ¥è¯¢æ§è¡æé´ä¸æåºä»»ä½é误ï¼å®ä¹åºè¯¥ä¼å¨é误æ¥å¿ä¸è®°å½ä¸ä¸ªè¦åã
éåºç´¢å¼
éç¨è¡¨è¾¾å¼
MySQLéå½CTEç®ä»
éå½å ¬ç¨è¡¨è¡¨è¾¾å¼(CTE)æ¯ä¸ä¸ªå ·æå¼ç¨CTEå称æ¬èº«çåæ¥è¯¢çCTEã以ä¸è¯´æéå½CTEçè¯æ³
1 2 3 4 5 6 | |
SQLéå½CTEç±ä¸ä¸ªä¸»è¦é¨åç»æï¼
å½¢æCTEç»æçåºæ¬ç»æéçåå§æ¥è¯¢(initial_query)
åå§æ¥è¯¢é¨å被称为éæåãéå½æ¥è¯¢é¨åæ¯å¼ç¨CTEå称çæ¥è¯¢ï¼å æ¤ç§°ä¸ºéå½æåãéå½æåç±ä¸ä¸ªUNION ALLæUNION DISTINCTè¿ç®ç¬¦ä¸éæåç¸è¿
ç»æ¢æ¡ä»¶æ¯å½éå½æå没æè¿åä»»ä½è¡æ¶ï¼ç¡®ä¿éå½åæ¢ã
éå½CTEçæ§è¡é¡ºåºå¦ä¸ï¼
- é¦å ï¼å°æåå为两个ï¼éç¹åéå½æåã
- æ¥ä¸æ¥ï¼æ§è¡éæåå½¢æåºæ¬ç»æé(R0)ï¼å¹¶ä½¿ç¨è¯¥åºæ¬ç»æéè¿è¡ä¸ä¸æ¬¡è¿ä»£
- ç¶åï¼å°Riç»æéä½ä¸ºè¾å ¥æ§è¡éå½æåï¼å¹¶å°Ri+1ä½ä¸ºè¾åº
- ä¹åï¼éå¤ç¬¬ä¸æ¥ï¼ç´å°éå½æåè¿åä¸ä¸ªç©ºç»æéï¼æ¢å¥è¯è¯´ï¼æ»¡è¶³ç»æ¢æ¡ä»¶
- æåï¼ä½¿ç¨UNION ALLè¿ç®ç¬¦å°ç»æéä»R0å°Rnç»åã
éå½æåéå¶éå½æåä¸è½å å«ä»¥ä¸ç»æ
- èåå½æ°ï¼å¦MAXï¼MINï¼SUMï¼AVGï¼COUNTç
- GROUP BYåå¥
- ORDER BYåå¥
- LIMITåå¥
- DISTINCT
请注æï¼ä¸è¿°çº¦æä¸éç¨äºéå®æåã å¦å¤ï¼åªæå¨ä½¿ç¨UNIONè¿ç®ç¬¦æ¶ï¼è¦ç¦æ¢DISTINCTæéç¨ã å¦æ使ç¨UNION DISTINCTè¿ç®ç¬¦ï¼åå 许使ç¨DISTINCTã
å¦å¤ï¼éå½æååªè½å¨å ¶åå¥ä¸å¼ç¨CTEå称ï¼èä¸æ¯å¼ç¨ä»»ä½åæ¥è¯¢ã
ç®åçMySQLéå½CTE示ä¾è¯·åé 以ä¸ç®åçéå½CTE 示ä¾ï¼
1 2 3 4 5 6 7 8 9 10 | |
SQLå¨æ¤ç¤ºä¾ä¸ï¼ä»¥ä¸æ¥è¯¢ï¼SELECT 1
SQLæ¯ä½ä¸ºåºæ¬ç»æéè¿å1çéæåã以ä¸æ¥è¯¢
1 2 3 | |
æ¯éå½æåï¼å 为å®å¼ç¨äºcte_countçCTEå称ãéå½æåä¸ç表达å¼<3æ¯ç»æ¢æ¡ä»¶ãå½nçäº3ï¼éå½æåå°è¿åä¸ä¸ªç©ºéåï¼å°åæ¢éå½ãä¸å¾æ¾ç¤ºäºä¸è¿°CTEçå ç´ ï¼
éå½CTEè¿å以ä¸è¾åºï¼
éå½CTEçæ§è¡æ¥éª¤å¦ä¸ï¼
- é¦å ï¼å离éåéå½æåã
- æ¥ä¸æ¥ï¼éå®æåå½¢æåå§è¡(SELECT 1)ï¼å æ¤ç¬¬ä¸æ¬¡è¿ä»£å¨n = 1æ¶äº§ç1 + 1 = 2ã
- ç¶åï¼ç¬¬äºæ¬¡è¿ä»£å¯¹ç¬¬ä¸æ¬¡è¿ä»£çè¾åº(2)è¿è¡æä½ï¼å¹¶ä¸å¨n = 2æ¶äº§ç2 + 1 = 3ã
- ä¹åï¼å¨ç¬¬ä¸æ¬¡æä½(n = 3)ä¹åï¼æ»¡è¶³ç»æ¢æ¡ä»¶(n < 3)ï¼å æ¤æ¥è¯¢åæ¢ã
- æåï¼ä½¿ç¨UNION ALLè¿ç®ç¬¦ç»åææç»æé1,2å3ã