解決本地鏈接外部MySQL數(shù)據(jù)庫時慢的問題

2022-07-08    分類: 網(wǎng)站建設(shè)

服務(wù)器放在局域網(wǎng)內(nèi)進行測試時,MySQL數(shù)據(jù)庫的訪問速度還是很快。但當(dāng)服務(wù)器托管放到IDC數(shù)據(jù)中心機房時要訪問外網(wǎng)后,MySQL數(shù)據(jù)庫的訪問速度就變得非常慢。

后來在問下了網(wǎng)絡(luò)運維的老同事,給出了解決方法。在MySQL配置文件中my.cnf里面添加
[mysqld]
skip-name-resolve    #跳過名稱解析
skip-grant-tables      #跳過授權(quán)表

這樣速度就快了

skip-name-resolve

選項就能禁用DNS解析,連接速度會快很多。不過,這樣的話就不能在MySQL的授權(quán)表中使用主機名了而只能用ip格式。

還有權(quán)限的問題,當(dāng)用戶設(shè)置限制只能訪問某個數(shù)據(jù)庫,如果這個數(shù)據(jù)庫被刪了,再重建這個指定數(shù)據(jù)庫,限制用戶還是不能訪問這個數(shù)據(jù),大概是刪除數(shù)據(jù)庫的時間,把該用戶的訪問權(quán)限也級聯(lián)刪除了,詳細可以查看mysql.db的記錄

若使用–skip-grant-tables系統(tǒng)將對任何用戶的訪問不做任何訪問控制,但可以用 mysqladmin flush-privileges或mysqladmin reload來開啟訪問控制;默認情況是show databases語句對所有用戶開放。

如果mysql服務(wù)器沒有開遠程帳戶,就在my.cnf里面加上skip-grant-tables

排除網(wǎng)絡(luò)問題。

就MySQL本身而言,問題出在在mysql dns反解析

mysql>show processlist;

| 20681949 | unauthenticated user | 10.10.4.193:52497 | NULL | Connect | | Reading from net | NULL | 
| 20681948 | unauthenticated user | 10.10.4.193:52495 | NULL | Connect | | Reading from net | NULL

發(fā)現(xiàn)有非常多的 unauthenticated user 嘗試做登入使用 mysql 的情況 ,當(dāng)這種情況無限制發(fā)生時就會造成系統(tǒng)十分緩慢。

查閱mysql官方網(wǎng)站得知,這屬于官方一個系統(tǒng)上的特殊設(shè)定,就把他當(dāng)成mysql的一個bug算了,不管鏈接的的方式是經(jīng)過 hosts 或是 IP 的模式,他都會對 DNS 做反查。mysqld 會嘗試去反查 IP -> dns ,由于反查解析過慢,就會無法應(yīng)付過量的查詢。

解決辦法:

/usr/local/mysql/bin/mysqld_safe --skip-name-resolve --user=mysql&

加 --skip-name-resolve 這么一個參數(shù)就可以,關(guān)閉mysql的dns反查功能。

或者修改mysql配置文件。

編輯/etc/my.cnf

在[mysqld]段中加入

skip-name-resolve

重啟mysql

在/etc/my.cnf 的配置文件中加入如下一句,禁用DNS反響解析,就能大大加快MySQL連接的速度。
[mysqld]

下面加上這句
skip-name-resolve

#注意有些文章中寫道加入–skip-name-resolve,經(jīng)驗證,在CentOS5下加入–skip-name-resolve會導(dǎo)致mysql守護進程無法啟動。估計在其他linux系統(tǒng)下是一樣的,windows下沒有測試,skip-name-resolve應(yīng)該就可以。

**************************************

問題現(xiàn)象是這樣的:

我在一臺機器上(61.183.23.23)啟動了一個mysql,然后開通一個賬號可以從127.0.0.1或者從61.183.23.23訪問。但是遇到一個問題就是使用下面兩個命令行訪問的時候,速度差別非常大:

mysql –h 127.0.0.1 –u user
mysql –h 61.183.23.23 –u user

然后我使用ping,判斷兩個IP的速度差不多。

使用127.0.0.1的IP速度比另一個快太多了。雖然說這里的61.183.23.23需要去外網(wǎng)走一圈,但是速度差別怎么會差這么多。

解決辦法:

mysql使用skip-name-resolve禁止掉DNS的查詢。
mysql會在用戶登錄過程中對客戶端IP進行DNS反查,不管你是使用IP登錄還是域名登錄,這個反查的過程都是在的。所以如果你的mysql所在的服務(wù)器的DNS有問題或者質(zhì)量不好,那么就有可能造成我遇到的這個問題,DNS解析出現(xiàn)問題。

修改mysql配置文件

[mysqld]
skip-name-resolve

補充:

做個提醒,增加skip-name-resolve有可能導(dǎo)致賬號失效,比如我原先的賬號是yejianfeng@localhost,然后其實我使用mysql -h127.0.0.1 -uyejianfeng 是可以登錄的。但是一旦加上了skip-name-resolve,就不能登錄的了。需要加上賬號yejianfeng@127.0.0.1

官方解釋信息如下:

How MySQL uses DNS

When a new thread connects to mysqld, mysqld will spawn a new thread to handle the request. This thread will first check if the hostname is in the hostname cache. If not the thread will call gethostbyaddr_r() and gethostbyname_r() to resolve the hostname.

If the operating system doesn't support the above thread-safe calls, the thread will lock a mutex and call gethostbyaddr() and gethostbyname() instead. Note that in this case no other thread can resolve other hostnames that is not in the hostname cache until the first thread is ready.

You can disable DNS host lookup by starting mysqld with –skip-name-resolve. In this case you can however only use IP names in the MySQL privilege tables.

If you have a very slow DNS and many hosts, you can get more performance by either disabling DNS lookop with –skip-name-resolve or by increasing the HOST_CACHE_SIZE define (default: 128) and recompile mysqld.

You can disable the hostname cache with –skip-host-cache. You can clear the hostname cache with FLUSH HOSTS or mysqladmin flush-hosts.

If you don't want to allow connections over TCP/IP, you can do this by starting mysqld with –skip-networking.

翻譯

MySQL如何使用DNS

當(dāng)一個線程連接到mysqld,mysqld將產(chǎn)生新的線程來處理請求。這個帖子會先檢查你的名字是在服務(wù)器緩存。如果沒有線程將調(diào)用gethostbyaddr_r()和gethostbyname_r()來解析主機名。

如果操作系統(tǒng)不支持上述線程安全調(diào)用,螺紋鎖互斥調(diào)用()和gethostbyname()相反。請注意,在這種情況下,沒有其他線程可以解決其他主機不在服務(wù)器緩存直到第一個線程已經(jīng)準備好。

你可以從–跳過名字mysqld解決禁用DNS主機查找。在這種情況下,您只能在MySQL特權(quán)表中使用IP名稱。

如果你有一個非常緩慢的DNS和許多主機,你可以獲得更多的性能通過禁用DNS名稱解析lookop與–跳過或增加host_cache_size定義(默認值:128)和編譯mysqld。

您可以禁用服務(wù)器與–跳過主機緩存。你可以用沖洗主機或mysqladmin沖洗主機清除服務(wù)器緩存。

如果你不想讓連接在TCP / IP,你可以通過從–跳過網(wǎng)絡(luò)mysqld。

網(wǎng)站標(biāo)題:解決本地鏈接外部MySQL數(shù)據(jù)庫時慢的問題
瀏覽地址:http://www.muchs.cn/news25/175875.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站手機網(wǎng)站建設(shè)、定制開發(fā)、網(wǎng)站導(dǎo)航定制網(wǎng)站、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)