Step1: 檢測系統是否自帶安裝mysql
# yum list installed | grep mysql
Step2: 刪除系統自帶的mysql及其依賴
命令:
# yum -y remove mysql-libs.x86_64
1 查看Linux發行版本
[root@typecodes ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)
2 下載MySQL官方的Yum Repository
根據Linux發行版本(CentOS、Fedora都屬於紅帽系),從mysql官方(http://dev.mysql.com/downloads/repo/yum/)獲取Yum Repository。
[root@typecodes ~]# wget -i http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
3 安裝MySQL的Yum Repository
安裝完MySQL的Yum Repository,每次執行yum update都會檢查MySQL是否更新。
[root@typecodes ~]# yum -y install mysql57-community-release-el7-7.noarch.rpm
4 安裝MySQL數據庫的服務器版本
[root@typecodes ~]# yum -y install mysql-community-server
5 啓動數據庫:
[root@typecodes ~]# systemctl start mysqld.service
然後使用命令systemctl status mysqld.service查看MySQL數據庫啓動後的服務狀態:
systemctl命令查看MySQL服務狀態
6 獲取初始密碼
使用YUM安裝並啓動MySQL服務後,MySQL進程會自動在進程日誌中打印root用戶的初始密碼:
#######從mysql進程日誌中獲取root用戶的初始密碼:ra%yk7urCBIh
[root@typecodes ~]# grep "password" /var/log/mysqld.log
在Centos 7系統上使用rpm命令安裝Mysql後,mysql的配置文件是/etc/my.cnf,打開該文件,可以看到mysql的datadir和log文件等的配置信息,如下:
datadir=/var/lib/mysql
log-error=/var/log/mysqld.log
打開/var/log/mysqld.log文件,搜索字符串A temporary password is generated for root@localhost:,可以找到這個隨機密碼,通常這一行日誌在log文件的最初幾行,比較容易看到。
使用找到的隨機密碼登錄mysql,首次登錄後,mysql要比必須修改默認密碼,否則不能執行任何其他數據庫操作,這樣體現了不斷增強的Mysql安全性。
方法二:
可以進行如下的步驟重新設置MySQL的root密碼:
1.首先確認服務器出於安全的狀態,也就是沒有人能夠任意地連接MySQL數據庫。
因爲在重新設置MySQL的root密碼的期間,MySQL數據庫完全出於沒有密碼保護的
狀態下,其他的用戶也可以任意地登錄和修改MySQL的信息。可以採用將MySQL對
外的端口封閉,並且停止Apache以及所有的用戶進程的方法實現服務器的準安全
狀態。最安全的狀態是到服務器的Console上面操作,並且拔掉網線。
2.修改MySQL的登錄設置:
# vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve
skip-grant-tables
保存並且退出vi。
3.重新啓動mysqld
# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
4.登錄並修改MySQL的root密碼
# /usr/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 3.23.56
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> USE mysql ;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
更改密碼: update mysql.user set authentication_string=password('#20as3SElksds0ew98') where user='root' and Host = 'localhost';
*特別提醒注意的一點是,新版的mysql數據庫下的user表中已經沒有Password字段了
而是將加密後的用戶密碼存儲於authentication_string字段
mysql> flush privileges;
mysql> quit;
# vi /etc/my.cnf
將剛纔在[mysqld]的段中加上的skip-grant-tables刪除
保存並且退出vi。
6.重新啓動mysqld
# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]