Ubuntu20.0安裝MySQL8並設置遠程訪問,Navicat連接測試成功

步驟
雲服務器安全組設置
安裝
檢查MySQL是否正常啓動
初始化
進入MySQL,修改一些配置
修改配置文件,以監聽所有IP
本機使用 Navicat 連接
雲服務器安全組設置
參考 騰訊雲服務器Ubuntu20.0安裝miniconda、jupyter,notebook設置中文和遠程訪問 中的第一步

安裝
(base) ubuntu@VM-0-3-ubuntu:~$ sudo apt-get update
(base) ubuntu@VM-0-3-ubuntu:~$ sudo apt-get install mysql-server
… 輸出了一大堆東西
下列【新】軟件包將被安裝:
libcgi-fast-perl libcgi-pm-perl libfcgi-perl libhtml-template-perl libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0
mysql-common mysql-server mysql-server-8.0 mysql-server-core-8.0
升級了 0 個軟件包,新安裝了 14 個軟件包,要卸載 0 個軟件包,有 0 個軟件包未被升級。
需要下載 30.9 MB 的歸檔。
解壓縮後會消耗 258 MB 的額外空間。
您希望繼續執行嗎? [Y/n]
Y

檢查MySQL是否正常啓動
(base) ubuntu@VM-0-3-ubuntu:~$ systemctl status mysql
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-06-09 16:17:18 CST; 1min 51s ago
Main PID: 2886 (mysqld)
Status: “Server is operational”
Tasks: 37 (limit: 2328)
Memory: 332.4M
CGroup: /system.slice/mysql.service
└─2886 /usr/sbin/mysqld

6月 09 16:17:17 VM-0-3-ubuntu systemd[1]: Starting MySQL Community Server…
6月 09 16:17:18 VM-0-3-ubuntu systemd[1]: Started MySQL Community Server.

初始化
(base) ubuntu@VM-0-3-ubuntu:~$ sudo mysql_secure_installation

Securing the MySQL server deployment. 保護MySQL服務器部署。

Connecting to MySQL using a blank password. 使用空密碼連接到MySQL。

VALIDATE PASSWORD COMPONENT 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 component? 驗證密碼組件可以用來測試密碼和提高安全性。它檢查密碼的強度,並允許用戶只設置那些足夠安全的密碼。是否要設置驗證密碼組件?
Press y|Y for Yes, any other key for No: 按y | y表示是,按其他鍵表示否:
N
Please set the password for root here. 請在此處設置root的密碼。
New password: 新密碼:
輸入密碼
Re-enter new password: 重新輸入新密碼:
再次輸入
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. 默認情況下,MySQL安裝有一個匿名用戶,允許任何人登錄MySQL,而不必爲他們創建用戶帳戶。這僅用於測試,並使安裝更加順利。您應該在進入生產環境之前刪除它們。
Remove anonymous users? (Press y|Y for Yes, any other key for No) : 刪除匿名用戶(按y | y表示是,按其他鍵表示否):
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. 通常,只允許root用戶從“localhost”連接。這確保了有人無法從網絡中猜到根密碼。

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 不允許根用戶遠程登錄(按y | y表示是,按其他鍵表示否):
N
… skipping. … 跳過。 這裏有點問題,稍後再整
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. 默認情況下,MySQL附帶一個名爲“test”的數據庫,任何人都可以訪問它。這也僅用於測試,應該在移入生產環境之前刪除。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 刪除測試數據庫並訪問它(按y | y表示是,按其他鍵表示否):
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 | y表示是,按其他鍵表示否):
Y
Success. 成功。
All done! 一切就緒!

進入MySQL,修改一些配置
(base) ubuntu@VM-0-3-ubuntu:~$ sudo mysql

如果出現錯誤,也可以試試:
(base) ubuntu@VM-0-3-ubuntu:~$ sudo mysql -u root -p
輸入密碼

選擇use數據庫
mysql> use mysql

查詢user信息
mysql> select host,user,plugin from user;
±----------±--------------------------±--------------------------------+
| host | user | plugin |
±----------±--------------------------±---------------------------------+
| localhost | debian-sys-maint | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | auth_socket |
±-----------±-------------------------±---------------------------------+

設置root可遠程訪問(注意標點符號
mysql> update user set host=’%’ where user=‘root’;

修改root的認證方式和密碼
mysql> ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;

刷新
mysql> FLUSH PRIVILEGES;

再次查詢
mysql> select host,user,plugin from user;

退出
mysql> exit

修改配置文件,以監聽所有IP
配置文件也有可能是別的,但都在 /etc/mysql 目錄下
(base) ubuntu@VM-0-3-ubuntu:~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
按 i 鍵,進入編輯模式
找到這兩個,將其改爲 0.0.0.0

保存並退出
按ESC鍵,再輸入 :wq

重新啓動MySQL服務
(base) ubuntu@VM-0-3-ubuntu:~$ sudo service mysql restart

本機使用 Navicat 連接

————————————————
版權聲明:本文爲CSDN博主「黑白吾嘗」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_17517409/article/details/117746615

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章