Ubuntu安裝Mysql並開啟遠程訪問
Created by 夏夜香頌
一、下載Mysql的apt配置包
-
在Mysql官網找到apt配置包下載鏈接 https://dev.mysql.com/get/mysql-apt-config_0.8.16-1_all.deb
-
在Ubuntu的終端輸入 wget https://dev.mysql.com/get/mysql-apt-config_0.8.16-1_all.deb
- 下載完成后會在當前路徑下產生一個.deb文件
mysql-apt-config_0.8.16-1_all.deb
二、安裝並配置MySQL APT配置包
- 在終端輸入
sudo dpkg -i mysql-apt-config_0.8.16-1_all.deb
-
- 第一個選項是選擇安裝版本,默認是8.0
- 第二個選項是選擇是否安裝MySQL套件,包括MySQL connectors, MySQL Workbench, MySQL Utilities 和 MySQL router,默認安裝
- 第三個選項是選擇是否啟用預覽版,默認不開啟,建議不開啟
-
下一步點擊OK即可,點擊后會返回正常終端,可能會出現Warning,可暫時忽略。想了解的同學可以深入研究一下。
三、安裝Mysql Server
- 更新apt
- 安裝后輸入密碼,自建密碼,
- 輸入,確定並再次輸入密碼后,會提示
│ MySQL 8 uses a new authentication based on improved SHA256-based
│ password methods. It is recommended that all new MySQL Server
│ installations use this method going forward. This new authentication
│ plugin requires new versions of connectors and clients, with support for
│ this new authentication method (caching_sha2_password). Currently MySQL
│ 8 Connectors and community drivers built with libmysqlclient21 support
│ this new method. Clients built with older versions of libmysqlclient may
│ not be able to connect to the new server.
│
│ To retain compatibility with older client software, the default
│ authentication plugin can be set to the legacy value
│ (mysql_native_password) This should only be done if required third-party
│ software has not been updated to work with the new authentication
│ method. The change will be written to the file
│ /etc/mysql/mysql.conf.d/default-auth-override.cnf
│
│ After installation, the default can be changed by setting the
│ default_authentication_plugin server setting.
譯文如下:MySQL 8使用了一種基於改進的SHA256密碼方法的新身份驗證。建議以后所有新的MySQL服務器安裝都使用這種方法。這個新的身份驗證插件需要新版本的連接器和客戶端,並支持這種新的身份驗證方法。目前,使用libmysqlclient21構建的MySQL 8連接器和社區驅動程序支持這種新方法。使用舊版本的libmysqlclient構建的客戶端可能無法連接到新服務器。
為了保持與舊版客戶端軟件的兼容性,可以將默認身份驗證插件設置為舊版值。只有在所需的第三方軟件尚未更新以使用新的身份驗證方法時,才應這樣做。更改將會被寫入以下文件/etc/mysql/mysql.conf.d/default-auth-override.cnf
安裝后,可以通過設置 default_authentication_plugin 服務器設置來更改默認值。
- 繼續OK,進入選擇驗證方式和加密方式,默認選擇即可。安裝完成后就done了。
四、開啟遠程訪問
-
使用
mysql -u root -p
命令並輸入你的密碼登錄 -
輸入
use mysql;
指定數據庫 -
輸入
select Host,User from user;
進行查詢,結果如下圖 -
root用戶的 Host為 localhost,此時只允許本地訪問。未開啟遠程訪問權限。
-
輸入
update user set host = '%' where user = 'root';
來修改,%代表允許所有的遠程主機進行連接。 -
授權
grant ALL on *.* to `root`@`%` with grant option;
-
刷新權限
flush privileges;