MySQL8.0連接協(xié)議及3306、33060、33062端口的作用解析
一、MySQL連接層
連接層為每個(gè)連接維護(hù)一個(gè)線程。該線程處理查詢執(zhí)行。 在連接可以開始發(fā)送 SQL 查詢之前,連接由驗(yàn)證用戶名、密碼和客戶端主機(jī)。
連接層通過多種連接協(xié)議接受來自應(yīng)用程序的連接:
- TCP/IP
- UNIX 套接字
- 共享內(nèi)存
- 命名管道
如下圖所示:

二、連接協(xié)議
協(xié)議在客戶端庫和驅(qū)動(dòng)程序中實(shí)現(xiàn)。
連接協(xié)議的速度因本地設(shè)置而異。
除了舊版 MySQL 經(jīng)典協(xié)議之外,MySQL X 協(xié)議還引入了MySQL 5.7.12 并在 MySQL 8.0 中默認(rèn)啟用。

MySQL 使用 TCP 將消息從客戶端通過網(wǎng)絡(luò)傳輸?shù)椒?wù)器,可以使用以 mysqlx 為前綴的變量和選項(xiàng)來配置 MySQL X 協(xié)議。
mysqlx 變量的一些示例:
- • mysqlx
- • mysqlx_bind_address
- • mysqlx_max_connections
- • mysqlx_port
- • mysqlx_socket
三、本地和遠(yuǎn)程連接協(xié)議:TCP/IP
TCP/IP(傳輸控制協(xié)議/互聯(lián)網(wǎng)協(xié)議):
1、是用于連接 Internet 上的主機(jī)的連接協(xié)議套件
2、使用 IP 地址或 DNS 主機(jī)名來識(shí)別主機(jī)
3、使用 TCP 端口號(hào)來標(biāo)識(shí)每個(gè)主機(jī)上的特定服務(wù)
MySQL 默認(rèn) TCP 端口號(hào):
1、3306 用于 MySQL Classic 協(xié)議(服務(wù)器端口選項(xiàng))
2、33060 用于 MySQL X 協(xié)議(服務(wù)器 mysqlx_port 選項(xiàng))
3、33062 用于使用 MySQL Classic 協(xié)議的管理連接(服務(wù)器 admin_port 選項(xiàng))
修改my.cnf
admin_address='localhost'
修改前后對(duì)比:
[root@hadoop1 ~]# mysql -e "show variables like 'admin%'"; +------------------------+-----------------+ | Variable_name | Value | +------------------------+-----------------+ | admin_address | | | admin_port | 33062 | | admin_ssl_ca | | | admin_ssl_capath | | | admin_ssl_cert | | | admin_ssl_cipher | | | admin_ssl_crl | | | admin_ssl_crlpath | | | admin_ssl_key | | | admin_tls_ciphersuites | | | admin_tls_version | TLSv1.2,TLSv1.3 | +------------------------+-----------------+ [root@hadoop1 ~]# systemctl restart mysqld.service [root@hadoop1 ~]# mysql -e "show variables like 'admin%'"; +------------------------+-----------------+ | Variable_name | Value | +------------------------+-----------------+ | admin_address | localhost | | admin_port | 33062 | | admin_ssl_ca | | | admin_ssl_capath | | | admin_ssl_cert | | | admin_ssl_cipher | | | admin_ssl_crl | | | admin_ssl_crlpath | | | admin_ssl_key | | | admin_tls_ciphersuites | | | admin_tls_version | TLSv1.2,TLSv1.3 | +------------------------+-----------------+ [root@hadoop1 ~]#
成功登錄:
[root@hadoop1 ~]# mysql -P 33062 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server version: 8.0.28 MySQL Community Server - GPL Copyright (c) 2000, 2022, 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>
網(wǎng)絡(luò)監(jiān)聽情況:
[root@hadoop1 ~]# netstat -anltp | grep 33062 tcp 0 0 127.0.0.1:33062 0.0.0.0:* LISTEN 1104641/mysqld [root@hadoop1 ~]#
設(shè)置最大連接數(shù)方便測(cè)試:
mysql> set global max_connections = 1; Query OK, 0 rows affected (0.00 sec)
重新連接:
[root@hadoop1 ~]# mysql ERROR 1040 (HY000): Too many connections [root@hadoop1 ~]# mysql -P 33062 --protocol tcp Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 21 Server version: 8.0.28 MySQL Community Server - GPL Copyright (c) 2000, 2022, 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>
從 MySQL 8.0.14 開始,MySQL 服務(wù)器允許專門為管理連接配置 TCP/IP 端口。這為用于普通連接的網(wǎng)絡(luò)接口上允許的單個(gè)管理連接提供了一種替代方法,即使已經(jīng)建立了 max_connections 連接。只有在啟動(dòng)時(shí)設(shè)置了 admin_address 系統(tǒng)變量以指示管理接口的 IP 地址時(shí),該接口才可用。如果未指定 admin_address 值,則服務(wù)器不維護(hù)管理界面。
只有SERVICE_CONNECTION_ADMIN 權(quán)限的用戶才允許連接。沒有限制管理連接的數(shù)量。 MySQL 服務(wù)器使用 DNS(域名系統(tǒng))來解析使用 TCP/IP 協(xié)議連接的客戶端主機(jī)的名稱,并將它們存儲(chǔ)在主機(jī)緩存中。對(duì)于在名稱解析過程中出現(xiàn)性能問題的大型網(wǎng)絡(luò),請(qǐng)使用 --skip-name-resolve 選項(xiàng)禁用 DNS 或增加 --host-cache-size 選項(xiàng)的值。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談mysqldump使用方法(MySQL數(shù)據(jù)庫的備份與恢復(fù))
下面小編就為大家?guī)硪黄獪\談mysqldump使用方法(MySQL數(shù)據(jù)庫的備份與恢復(fù))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Mysql數(shù)據(jù)庫之Binlog日志使用總結(jié)(必看篇)
下面小編就為大家?guī)硪黄狹ysql數(shù)據(jù)庫之Binlog日志使用總結(jié)(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
優(yōu)化 MySQL 3 個(gè)簡(jiǎn)單的小調(diào)整
本文給大家?guī)砹藘?yōu)化 MySQL 3 個(gè)簡(jiǎn)單的小調(diào)整,需要的朋友參考下2018-02-02
MySQL存儲(chǔ)過程輸入?yún)?shù)(in),輸出參數(shù)(out),輸入輸出參數(shù)(inout)
這篇文章主要介紹了MySQL存儲(chǔ)過程輸入?yún)?shù)(in),輸出參數(shù)(out),輸入輸出參數(shù)(inout),存儲(chǔ)過程就是一組SQL語句集,功能強(qiáng)大,可以實(shí)現(xiàn)一些比較復(fù)雜的邏輯功能,類似于JAVA語言中的方法;Python里面的函數(shù)2022-07-07

