在Ubuntu 20.04系統中安裝MySQL 8和MySQL Workbench


Install MySQL 8 & MySQL Workbench on Ubuntu 20.04

MySQL是一款廣受歡迎的數據庫,在MySQL 8當中,一些新特性的增加也使其功能更加強大。由於筆者日常科研需要,在筆記本電腦上安裝了Ubuntu 20.04單系統,對MySQL 8進行安裝與配置時遇到了一些小問題。經過查閱其他貢獻者發布的排障教程,以及自己的反復摸索操作,問題已基本解決。現將過程記錄如下。

1. 安裝MySQL Server

在終端中執行下列命令。

sudo apt update
sudo apt install mysql-server

安裝完成后MySQL服務將會自動啟動,在終端中執行以下命令以驗證運行狀態。

sudo systemctl status mysql

2. 初始化設置

在終端中輸入啟動初始化設置命令,然后通過幾個“是”“否”選項逐步進行配置。

(1)Would you like to setup VALIDATE PASSWORD component?**

—— n

然后設置root賬戶密碼,並重復輸入一遍。兩遍密碼輸入都不會在終端中顯示。

(2)Remove anonymous users?

—— n

(3)Disallow root login remotely?

—— n

(4)Remove test database and access to it?

—— n

(5)Reload privilege tables now?

—— y

當顯示“All done!”時,表示初始化完成。終端里的具體顯示內容如下。

sudo mysql_secure_installation

# 返回結果應為
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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: n
Please set the password for root here.

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.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : n

 ... skipping.


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) : 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.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
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! 

3. 安裝MySQL Workbench

通過sudo apt-get install mysql-workbench命令進行安裝,據說安裝不了,所以先不嘗試了,直接從官網下載。

下載地址:MySQL :: Download MySQL Workbench

當前最新版本是# MySQL Workbench 8.0.27,官方根據不同系統給出了很多版本。基於我的電腦的情況,在“Select Operating System”中選擇“Ubuntu Linux”,在“Select OS Version”中選擇“Ubuntu Linux 20.04 (x86, 64-bit)”。選擇完以后,列表里就只剩下2個了,我下載的是第一個,稍微小一點的安裝包。

接下來會跳轉到下載頁面,這里提示登陸Oracle賬號,但其實不登陸也行,於是點下面的小字“No thanks, just start my download.”,然后瀏覽器就會開始下載。

下載完畢后,找到下載的.deb文件,雙擊打開,在安裝器界面點擊“Install”按鈕。按下去以后會提示輸入用戶密碼,正常流程,輸入就好。緊接着就會開始安裝。

4. 連接MySQL Workbench和MySQL Server

打開安裝好的MySQL Workbench發現已經有一個root賬戶在了,於是點擊登錄,若登錄成功,則正常使用即可;若登錄失敗,報錯“Cannot Connect to Database Server”,則可按照以下步驟修復。

所報錯誤大致意思是連接root賬戶失敗,讓用戶檢查MySQL服務是否在運行、檢查端口號等等。以下步驟是參考了一些排障教程外加親手操作以后總結而成。

(1)刪除MySQL Workbench已經存在的root賬戶

(2)以root身份從終端中登陸MySQL

sudo mysql -uroot -p

(3)檢查端口號3306

通過以下命令檢查端口是否啟用,如果返回結果列表中有端口號3306,則說明端口沒問題。從網上大量的教程來看,大多數用戶的端口沒問題。

show global variables like 'port';

# 返回結果應為
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

(4)修改賬戶host

初始化的時候實際上就已經把root的host設置成了“localhost”,這里再做一次相當於二次確認。

use mysql;
update user set host = 'localhost' where user = 'root';

# 返回結果應為
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

驗證一下修改是否成功。

select user, host from user;

# 返回結果應為 
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

(5)授權

MySQL 8版本中新增了一個“SYSTEM_USER“帳戶類型,因為root用戶沒有SYSTEM_USER權限,所以如果不授權就執行修改密碼命令,就會報error。

grant all privileges on *.* to 'root'@'localhost';

# 返回結果應為
Query OK, 0 rows affected (0.01 sec)

# 若不執行這一步,便會報錯
ERROR 1227 (42000): Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this operation

(6)修改密碼

以往MySQL Workbench支持的是“mysql_native_password“這種加密方式,但MySQL 8引入了新特性“caching_sha2_password”,這種密碼加密方式MySQL Workbench有可能不支持。

所以這一步要用“mysql_native_password”的方式將密碼改成MySQL Workbench支持的樣子。

alter user 'root'@'localhost' identified with mysql_native_password by '(your password)';

# 返回結果應為
Query OK, 0 rows affected (0.01 sec)

(7)刷新權限

“flush privileges”的作用是:將當前user和privilige表中的用戶信息/權限設置從mysql庫(MySQL數據庫的內置庫)中提取到內存里。MySQL用戶數據和權限有修改后,希望在"不重啟MySQL服務"的情況下直接生效,那么就需要執行這個命令。通常是在修改ROOT帳號的設置后,怕重啟后無法再登錄進來,那么直接flush之后就可以看權限設置是否生效。而不必冒太大風險。

flush privileges;

# 返回結果應為
Query OK, 0 rows affected (0.01 sec)

(8)再次打開MySQL Workbench

這次再打開MySQL Workbench,會發現一開始刪除的root賬號又回來了。再次點擊、登錄,應該就能正常使用了。

References

[1] ubuntu20 安裝mysql和workbench

[2] 如何在 Ubuntu 20.04 上安裝 MySQLhttps://zhuanlan.zhihu.com/p/137339787)

[3] Ubuntu20.04安裝Mysql(親測有效,一定要按步驟來)

[4] Ubuntu20.04安裝MySQL8.0

[5] Ubuntu下mysql-workbench連接mysql報“access denied for user root@localhost”

[6] mysql 解決 ERROR 1227 (42000): Access denied

[7] 成功解決:Workbench等客戶端連接不上服務器的MySQL問題

[8] 安裝 mysql 8.0后;root用戶在客戶端連接不上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM