天天看點

CentOS上如何順利地安裝MySQL?

我在CentOS上安裝過無數次的MySQL,但有的時候很順利,有的時候卻大費周章。為了給需要的小夥伴們有用的幫助,我把這些經驗分享出來,為你節省大量的調研時間。

一、安裝檔案

為了友善大家的下載下傳,我将MySQL的CentOS安裝檔案上傳到了百度雲,位址為:

http://pan.baidu.com/s/1eQrvAuu

Client:MySQL-client-5.7.4_m14-1.el6.x86_64.rpm

Server:MySQL-server-5.7.4_m14-1.el6.x86_64.rpm

二、安裝

①、上傳檔案

如果你的環境中安裝了使用rz/sz環境,可以使用該指令直接上傳。但速度可能會比較慢,建議你使用Filezila工具上傳,上傳完畢後,見檔案如下:

②、清理MySQL

這一步非常關鍵!必須保證在安裝之前,CentOS沒有安裝過MySQL,否則會導緻安裝失敗。

我在《CentOS上如何順利地更新MySQL到5.7版本?》 (http://blog.csdn.net/qing_gee/article/details/41774265),這篇文章中有詳細介紹,可參照。

③、安裝MySQL-server

使用rpm -ivh MySQL-server-5.7.4_m14-1.el6.x86_64.rpm先安裝Server,資訊如下。

[root@iZ23gsv94suZ soft]# rpm -ivh MySQL-server-5.7.4_m14-1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
find: `/var/lib/mysql': No such file or directory
   1:MySQL-server           ########################################### [100%]
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
 http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com      

tips:

注意/root/.mysql_secret這個檔案(儲存了MySQL初始化安裝時候的初始密碼)。

注意no other statement but 'SET PASSWORD' will be accepted這句話(後面要用)。

④、安裝MySQL-client

使用rpm -ivh MySQL-client-5.7.4_m14-1.el6.x86_64.rpm再安裝Client。

⑤、啟動MySQL服務

使用service mysql start啟動MySQL服務。

[root@iZ23gsv94suZ mysql]# service mysql start

Starting MySQL. SUCCESS!

⑥、登陸MySQL用戶端

使用mysql -uroot -p登陸MySQL用戶端,密碼在哪裡呢?密碼在/root/.mysql_secret這個檔案中。

[root@iZ23gsv94suZ mysql]# mysql -uroot -p

Enter password:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

⑦、修改預設密碼

預設密碼不好記,最好修改為我們記住的(但要保證複雜度)。

mysql> use mysql

ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

此時出現了ERROR 1820 (HY000)錯誤,需要先執行set password=password("root")(有沒有想起來之前tips中的第二個注意點?)。

mysql> set password=password("root");

Query OK, 0 rows affected (0.00 sec)

⑧、修改密碼

終于到了修改密碼的時候了。

use mysql

update user set password=PASSWORD("lixiaoli") where user="root";

flush privileges;

密碼為什麼是“lixiaoli”,因為我喜歡李孝利。你聽過她的歌,看過她跳的MV嗎?(李孝利經典歌曲10Minutes,當年天後地位無人能撼動,視訊位址:

https://www.iqiyi.com/w_19rtp6vr59.html

⑨、開啟防火牆

為了保證安全性,我們需要開啟防火牆。但請記得釋放MySQL的3306端口。

1、關閉防火牆

service iptables stop

2、編輯配置

vim /etc/sysconfig/iptables

3、打開配置檔案後,增加如下内容

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

4、儲存配置檔案後啟動

service iptables save

service iptables start

⑩、開啟遠端通路權限

登陸MySQL用戶端後,使用以下指令開啟遠端通路權限(這樣就可以在指定IP通過Navicat上對MySQL上的資料進行增删改查)。

mysql> grant all privileges on *.* to root@'192.168.44.11' identified by "lixiaoli";

mysql> flush privileges;

簡單解釋一下這條指令。

“grant all privileges”:開啟所有權限,包括增删改查了!

“.”:當有資料庫的資料表。

“root”:user名了!

“‘192.168.44.11’”自然就是對那一台機器開啟遠端權限了。

“‘lixiaoli’”:自然是對應的密碼了