CentOS系統安裝MySQL(5.7版本)
一、清理環境
1.檢視系統是否安裝了mysql資料庫
[root@vm01 ~]# rpm -qa | grep mysql
2.查詢到已安裝的MySQL後,執行以下命令依次解除安裝(若沒有查詢到,則說明沒有安裝Mysql庫,可直接跳過此步)
yum remove mysql-xxx-xxx
3.刪除MySQL的配置檔案(MySQL解除安裝不會自動刪除配置檔案,需自行清理)
[root@vm01 ~]# find / -name mysql
根據需求使用以下命令 依次 對配置檔案進行刪除
rm -rf /var/lib/mysql
4.刪除MariaDB的檔案
由於 在CentOS中預設安裝有
1)使用rpm命令查找出要刪除的mariadb檔案
[root@vm01 ~]# rpm -qa | grep mariadb
2)刪除查詢到的程式:
[root@localhost ~]# yum -y remove mariadb-libs-5.5.60-1.el7_5.x86_64
二、安裝MySQL
1.安裝wget命令(如果wget無法使用的話)
[root@localhost ~]# yum -y install wget
2.找到MySQL下載的yum源
下載地址為:https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
3.下載並安裝MySQL
1)下載MySQL的YUM連結:wget -i -c https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
2)安裝MySQL的YUM連結:yum -y install mysql57-community-release-el7-11.noarch.rpm
3)安裝MySQL的mysql-server:yum -y install mysql-community-server
注意:因為連到MySQL官方映象,下載巨慢,yum下載後的快取檔案路徑在:/var/cache/yum/x86_64/7/mysql57-community/packages/
最終解決方案:從國內映象下載對應的檔案,通過FTP上傳到伺服器,再重新執行命令:yum -y install mysql-community-server
國內映象站:http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/
http://uni.mirrors.163.com/mysql/Downloads/
4.安裝完成後啟動MySQL
[root@localhost ~]# systemctl start mysqld
5.檢視MySQL的執行狀態
[root@localhost ~]# systemctl status mysqld
出現以下資訊,說明MySQL已啟動
6.查詢首次啟動隨機生成的ROOT密碼
grep "password" /var/log/mysqld.log
7.登入MySQL
mysql -uroot -p 回車,輸入上面查詢到的密碼;
8.首次操作要求重置密碼,必須大小寫特殊字元組成
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'; newpassword 是要改的密碼。
9.授權遠端訪問%表示所有主機都可以訪問
grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option; passwod是你的密碼
10.重新整理許可權資訊
flush privileges;
11.退出
exit;
12.修改字符集
vi /etc/my.cnf
新增以下內容後,儲存
[client] default-character-set=utf8 [mysqld] character-set-server=utf8 collation-server=utf8_general_ci
13.重啟MySQL,並且檢視字符集是否設定成功
systemctl restart mysqld
mysql -uroot -p 回車後輸入密碼;
status;回車
出現以下資訊則代表設定成功