- 安裝 mysql
sudo apt install mysql-server -y
- 運(yùn)行安全設(shè)置腳本
sudo mysql_secure_installation
因此包括步驟:
- 安裝驗(yàn)證密碼插件
- 設(shè)置root密碼
- 刪除匿名賬戶
- 禁止root從遠(yuǎn)程登錄
- 刪除test數(shù)據(jù)庫
- 刷新權(quán)限列表显设,讓設(shè)置的規(guī)則立刻生效
- 更改用戶認(rèn)證方式
出于對數(shù)據(jù)庫的安全性考慮纵装,Mysql的root用戶默認(rèn)是通過auth_socket插件認(rèn)證的,使用root用戶登錄訪問厘托,需要更改用戶認(rèn)證的方式拴疤,本機(jī)進(jìn)入mysql :
sudo mysql
檢查mysql中的用戶認(rèn)證方式:
SELECT user, authentication_string, plugin, host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *09EBD618590C1E2D294EEEC8B05E0C4E06DDC069 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
將root用戶的auth_socket修改為mysql_native_password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'tesT_12345';
使用命令mysql -u root -p
华畏,輸入密碼就可以正常登錄mysql了缭付。
- 遠(yuǎn)程訪問mysql
默認(rèn)情況下资溃,mysql不能遠(yuǎn)程訪問,要啟用遠(yuǎn)程訪問鳖悠,需要修改mysql的配置:
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
注釋掉:
bind-address = 127.0.0.1
先建用戶并授權(quán)該用戶訪問的權(quán)限:
CREATE USER 'test'@'%' IDENTIFIED BY 'tesT_123456';
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;