最近在CentOS7上安裝了MySQL8.0,踩了一些坑晋渺,發(fā)現(xiàn)了一個(gè)比較簡(jiǎn)單的步驟,參照了這篇文章:https://www.if-not-true-then-false.com/2010/install-mysql-on-fedora-centos-red-hat-rhel/袭景,記錄如下挡育。
1.安裝MySQL的yum源
yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
2.安裝MySQL
yum install mysql-community-server
3.啟動(dòng)MySQL服務(wù)
systemctl start mysqld.service
systemctl enable mysqld.service
4.獲取臨時(shí)密碼
grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log |tail -1
輸出如下:
2018-09-01T17:13:30.385800Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: NqLQAN?(m1=Q
root@localhost:后邊的亂碼就是臨時(shí)密碼,登陸的時(shí)候要用到
5.更改密碼或使用安全安裝腳本
臨時(shí)密碼用一次之后最好修改掉称簿,修改密碼可以用mysqladmin或者安全安裝腳本進(jìn)行扣癣。
更改密碼:
mysqladmin -u root password [your_password_here]
使用安全安裝腳本:
/usr/bin/mysql_secure_installation
腳本設(shè)置內(nèi)容較長(zhǎng),如果沒(méi)有興趣可以跳過(guò)直接看添加遠(yuǎn)程賬戶那一節(jié)憨降。
Securing the MySQL server deployment.
Enter password for user root: # 在這里輸入臨時(shí)密碼
The existing password for the user account root has expired. Please set a new password.
New password: # 在這里輸入新密碼
Re-enter new password: # 重復(fù)新密碼
這段是設(shè)置新密碼父虑。
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y # 選擇是否設(shè)置密碼復(fù)雜度策略
There are three levels of password validation policy:
LOW Length >= 8 # 長(zhǎng)度大于8
MEDIUM Length >= 8, numeric, mixed case, and special characters # 長(zhǎng)度大于8,有數(shù)字授药,大小寫(xiě)混合士嚎,有特殊字符
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file # 在中間策略的基礎(chǔ)上增加弱密碼字典
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 # 設(shè)置密碼復(fù)雜度策略
Using existing password for root.
上邊這段我安裝的時(shí)候并沒(méi)有見(jiàn)到,在設(shè)置完之后可以更新設(shè)置策略來(lái)簡(jiǎn)化密碼悔叽,有關(guān)密碼策略的相關(guān)內(nèi)容可以看這篇文章:https://www.cnblogs.com/ivictor/p/5142809.html
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y # 搞不明白為什么這里還要重新設(shè)置一遍
New password: # 新密碼
Re-enter new password: # 重復(fù)新密碼
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
先檢查了密碼復(fù)雜度航邢,然后又重新設(shè)置了一遍密碼
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
這段比較長(zhǎng),但是都無(wú)關(guān)緊要骄蝇,一路按y就行
6. 添加遠(yuǎn)程賬戶
MySQL 8.0的安全策略比之前的版本要嚴(yán)格很多膳殷,如果想通過(guò)遠(yuǎn)程訪問(wèn)不能直接改my.conf了,需要到MySQL中去設(shè)置
登陸MySQL
mysql -u root -p
創(chuàng)建遠(yuǎn)程用戶
# 創(chuàng)建db
mysql> CREATE DATABASE webdb;
# 添加用戶和密碼到監(jiān)聽(tīng)ip九火,注意這里的ip應(yīng)該是本機(jī)與外界通信的物理ip
mysql> CREATE USER 'webdb_user'@'10.0.15.25' IDENTIFIED BY 'password123';
# 為用戶設(shè)置權(quán)限
mysql> GRANT ALL ON webdb.* TO 'webdb_user'@'10.0.15.25';
# 刷新策略
mysql> FLUSH PRIVILEGES;