MySQL有何常見(jiàn)問(wèn)題及對(duì)應(yīng)的解決方案

下文給大家?guī)?lái)關(guān)于MySQL有何常見(jiàn)問(wèn)題及對(duì)應(yīng)的解決方案,感興趣的話就一起來(lái)看看這篇文章吧,相信看完MySQL有何常見(jiàn)問(wèn)題及對(duì)應(yīng)的解決方案對(duì)大家多少有點(diǎn)幫助吧。

從網(wǎng)站建設(shè)到定制行業(yè)解決方案,為提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)服務(wù)體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設(shè)解決方案,助力業(yè)務(wù)快速發(fā)展。創(chuàng)新互聯(lián)將不斷加快創(chuàng)新步伐,提供優(yōu)質(zhì)的建站服務(wù)。

一、 忘記 MySQL 的 root 密碼

1. 登錄到數(shù)據(jù)庫(kù)所在的云服務(wù)器,手工 kill 掉 mysql 進(jìn)程。

(1) 登錄到數(shù)據(jù)庫(kù)所在的云服務(wù)器,手工 kill 掉 MySQL 進(jìn)程:

root@bogon:/data/mysql# kill `cat ./mysql.pid`

其中,mysql.pid 指的是 MySQL 數(shù)據(jù)目錄下的 pid 文件,它記錄了 MySQL 服務(wù)的進(jìn)程號(hào)。

(2) 使用 --skip-grant-tables 選項(xiàng)重啟 MySQL 服務(wù):

zj@bogon:/data/mysql$ sudo /usr/local/mysql/bin/mysqld --skip-grant-tables --user=root &

--skip-grant-tables 選項(xiàng)意思是啟動(dòng) MySQL 服務(wù)時(shí)跳過(guò)權(quán)限表認(rèn)證。啟動(dòng)后,連接到 MySQL 的 root 將不需要口令。

(3) 用空密碼的 root 用戶連接到 mysql ,并且更改 root 口令:

zj@bogon:/usr/local/mysql/bin$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3Server version: 5.7.18-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> set password = password('123456');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statementMySQL [(none)]> use mysql
Database changed
MySQL [mysql]> update user set authentication_string=password('123456') where user="root" and host="localhost";
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 1MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [mysql]> exit;
Bye
****************************************************************
zj@bogon:/usr/local/mysql/bin$ mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7Server version: 5.7.18-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>

由于使用了 --skip-grant-tables 選項(xiàng)啟動(dòng),使用 “set password” 命令更改密碼失敗,直接更新 user 表的 authentication_string(測(cè)試版本為5.7.18,有的版本密碼字段是 ‘password’) 字段后,更改密碼成功。刷新權(quán)限表,使權(quán)限認(rèn)證重新生效。重新用 root 登錄時(shí),就可以使用剛剛修改后的口令了。

二、如何處理 myisam 存儲(chǔ)引擎的表?yè)p壞

有的時(shí)候可能會(huì)遇到 myisam 表?yè)p壞的情況。一張損壞的表的癥狀通常是查詢意外中斷,并且能看到下述錯(cuò)誤:

'table_name.frm' 被鎖定不能更改

不能找到文件 'tbl_name.MYYI' (errcode:nnn)

文件意外結(jié)束

記錄文件被毀壞

從表處理器得到錯(cuò)誤 nnn。

通常有以下兩種解決方法:

1. 使用 myisamchk 工具

使用 MySQL 自帶的 myisamchk 工具進(jìn)行修復(fù):

shell> myisamchk -r tablename

其中 -r 參數(shù)的含義是 recover,上面的方法幾乎能解決所有問(wèn)題,如果不行,則使用命令:

shell> mysiamchk -o tablename

其中 -o 參數(shù)的含義是 --safe-recover,可以進(jìn)行更安全的修復(fù)。

2. 使用 sql 命令

使用 MySQL 的 check table 和 repair table 命令一起進(jìn)行修復(fù),check table 用來(lái)檢查表是否有損壞;repair table 用來(lái)對(duì)壞表進(jìn)行修復(fù)。

三、 數(shù)據(jù)目錄磁盤(pán)空間不足的問(wèn)題

系統(tǒng)上線后,隨著數(shù)據(jù)量的不斷增加,會(huì)發(fā)現(xiàn)數(shù)據(jù)目錄下的可用空間越來(lái)越小,從而給應(yīng)用造成了安全隱患。

1. 對(duì)于 myisam 存儲(chǔ)引擎的表

對(duì)于 myisam 存儲(chǔ)引擎的表,在建表時(shí)可以用如下選項(xiàng)分別制定數(shù)據(jù)目錄和索引目錄存儲(chǔ)到不同的磁盤(pán)空間,而默認(rèn)會(huì)同時(shí)放在數(shù)據(jù)目錄下:

data directory = 'absolute path to directory'index directory = 'absolute path to directory'

如果表已經(jīng)創(chuàng)建,只能先停機(jī)或者將表鎖定,防止表的更改,然后將表的數(shù)據(jù)文件和索引文件 mv 到磁盤(pán)充足的分區(qū)上,然后在原文件處創(chuàng)建符號(hào)鏈接即可。

2. 對(duì)于 innodb 存儲(chǔ)引擎的表

因?yàn)閿?shù)據(jù)文件和索引文件是存放在一起的,所以無(wú)法將它們分離。當(dāng)磁盤(pán)空間出現(xiàn)不足時(shí),可以增加一個(gè)新的數(shù)據(jù)文件,這個(gè)文件放在充足空間的磁盤(pán)上。
具體實(shí)現(xiàn)方法是在參數(shù) innodb_data_file_path 中增加此文件,路徑寫(xiě)為新磁盤(pán)的絕對(duì)路徑。
例如,如果 /home 下空間不足,希望在 /home1 下新增加一個(gè)可自動(dòng)擴(kuò)充數(shù)據(jù)的文件,那么參數(shù)可以這么寫(xiě):

innodb_data_file_path = /home/ibdata1:2000M;/home1/ibdata2:2000M:autoextend

參數(shù)修改后,必須重啟數(shù)據(jù)庫(kù)才可以生效。

四、DNS反向解析的問(wèn)題 (5.0 以后的版本默認(rèn)跳過(guò)域名逆向解析)

在客戶端執(zhí)行 show processlist 命令,有時(shí)會(huì)出現(xiàn)很多進(jìn)程,類(lèi)似于:

unauthenticated user | 192.168.10.10:55644 | null | connect | null | login | null

這些進(jìn)程會(huì)累計(jì)的越來(lái)越多,并且不會(huì)消失,應(yīng)用無(wú)法正常相應(yīng),導(dǎo)致系統(tǒng)癱瘓。

MySQL 在默認(rèn)情況下對(duì)于遠(yuǎn)程連接過(guò)來(lái)的 IP 地址會(huì)進(jìn)行域名的逆向解析,如果系統(tǒng)的 hosts 文件中沒(méi)有與之對(duì)應(yīng)的域名,MySQL 就會(huì)將此連接認(rèn)為是無(wú)效用戶,所以下進(jìn)程中出現(xiàn) unauthenticated user 并導(dǎo)致進(jìn)程阻塞。

解決的方法很簡(jiǎn)單,在啟動(dòng)時(shí)加上 --skip-name-resolve 選項(xiàng),則 MySQL 就可以跳過(guò)域名解析過(guò)程,避免上述問(wèn)題。

五、mysql.sock 丟失后如何連接數(shù)據(jù)庫(kù)

在 MySQL云服務(wù)器本機(jī)上連接數(shù)據(jù)庫(kù)時(shí),經(jīng)常會(huì)出現(xiàn) mysql.sock 不存在,導(dǎo)致無(wú)法連接的問(wèn)題。這是因?yàn)槿绻付?localhost 作為一個(gè)主機(jī)名,則 mysqladmin 默認(rèn)使用 Unix 套接字文件連接,而不是 tcp/ip。而這個(gè)套接字文件(一般命名為 mysql.sock)經(jīng)常會(huì)因?yàn)楦鞣N原因而被刪除。通過(guò) --protocol=TCP|SOCKET|PIPE|MEMORY 選項(xiàng),用戶可以顯式地指定連接協(xié)議,下面演示使用了 Unix 套接字失敗后使用 tcp 協(xié)議連接成功的例子。

1. Unix 套接字連接:

zj@bogon:~$ mysqlERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

2. tcp 連接

zj@bogon:~$ mysql --protocol=TCP

看了以上關(guān)于MySQL有何常見(jiàn)問(wèn)題及對(duì)應(yīng)的解決方案詳細(xì)內(nèi)容,是否有所收獲。如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。

名稱(chēng)欄目:MySQL有何常見(jiàn)問(wèn)題及對(duì)應(yīng)的解決方案
路徑分享:http://bm7419.com/article34/iihepe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)品牌網(wǎng)站制作、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、虛擬主機(jī)網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司

廣告

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

微信小程序開(kāi)發(fā)