【教學】 如何在 Ubuntu 18.04 安裝 MySQL 8.0


【教學】 如何在 Ubuntu 18.04 安裝 MySQL 8.0

Leading Tides 在這個教學裡,我們教您如何在雲端的 Ubuntu 伺服器(版本 18.04 Bionic Beaver)上安裝 MySQL 8.0 的開放原始碼之關聯式資料庫。MySQL 8.0 GA 版本(General Availability)於今年四月釋出,General Availability 是屬於可以正式(Production)作為營運的釋出版本。

在您安裝 MySQL 8.0 之前,務必要注意如果您的伺服器本來就有安裝或是內建之 MySQL 8.0 以前之版本(如 MySQL 5.7 版),將需您原本公司的 MySQL 資料進行備份及匯出,關於備份資料和匯出作業,可以洽詢LT團隊關於進一步的資訊。另準備 Ubuntu (版本 18.04)伺服器,需一組非 root 的超級使用者帳號,root 在 Linux 的權限是非常高,不建議以該帳號進行操作。

前置作業

MySQL 提供開發者於 Ubuntu 中加入下載方式於 apt 軟體管理套件,以此教學為例,需先移至臨時資料夾:

$ cd /tmp

接下來,下載 MySQL 提供的套件管理設定檔至 /tmp 資料夾:

$ curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb

下載完成後,.deb 的軟體套件包需以 dpkg 指令安裝:

$ sudo dpkg -i mysql-apt-config*

安裝後,Config 設定選擇切換至 MySQL v8 的版本(另外有 v5.7等選項,須選擇 v8版本),再來以下列指令更新本機的軟體套件版本:

$ sudo apt-get update

上述步驟完成後,也可以選擇把剛下載的 .deb 檔案自 /tmp 資料夾刪除:

$ sudo rm mysql-apt-config*

安裝 MySQL 8.0

由於伺服器已經更新了最新的軟體套件版本,現在需以 apt-get 正式地安裝 MySQL 於 Ubuntu中:

$ sudo apt-get install mysql-server

安裝完成後輸入以下指令,查看是否 MySQL 已順利運行:

$ service mysql status

Console 回傳:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-12-05 11:37:47 UTC; 10s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 16032 (mysqld)
   Status: "SERVER_OPERATING"
    Tasks: 38 (limit: 1152)
   CGroup: /system.slice/mysql.service
           └─16032 /usr/sbin/mysqld

Dec 05 11:37:43 mysql-v8 systemd[1]: Starting MySQL Community Server...
Dec 05 11:37:47 mysql-v8 systemd[1]: Started MySQL Community Server.

初始化 MySQL 8.0 的安全設置

MySQL 從 5.7 版本後提供安全的初始化設定,如第一次處理密碼等可進行以下幾點安全設置。

  • 安裝密碼強度驗證插件 Plugin
  • 設置密碼強度安全等級
  • 修改 root 訪問權限
  • 移除匿名用戶
  • 移除測試數據庫
  • 設置完成重新載入安全表

進行安全設置,可透過以下指令:

$ mysql_secure_installation

初始化介面會進行一連串問題如下:

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?

選擇是否安裝 Validate Password Component 插件,如果是擇選擇三種安全性等級。

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Estimated strength of the password: 50 
Do you wish to continue with the password provided?

安全性等級低密碼要求密碼總長度8位數以上,中等除了8位數以上,還需要為數字、大和小寫英文字及特殊符號,強等級另外還要求符合字典字詞檢核。

接下來,以下選擇是否禁止 root 遠程登入,通常來說 root 只能准許從 localhost 連線,以防從主機外部的連線進來,但也須遵照IT的系統佈局意思,而選擇是否禁用遠程登入。

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.

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?

預設來說,MYSQL 在安裝時有開啟一個資料庫叫 test ,是可以移除的,可以利用這個時間把 test 給移除。

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?

最後,是否重新載入權限表,載入後即設定完成。

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now?

MySQL 8.0 用戶管理

重設 Root 密碼

遇到無法登入,可重設密碼。

Access denied for user 'root'@'localhost'

mysql -u root
mysql> use mysql;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password';

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

新增 MySQL 本機帳號和密碼

新增 newuser 及其密碼 12345678 (範例):

mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY '12345678';

視情況給予 newuser 所有的資料庫權限:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';

重新載入權限表:

mysql> FLUSH PRIVILEGES;

新增 MySQL 遠端帳號和密碼

新增遠端 remote newuser 及其密碼 12345678 (範例):

mysql> CREATE USER 'newuser'@'%' IDENTIFIED BY '12345678';

視情況給予 newuser 所有的資料庫權限:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'%';

重新載入權限表:

mysql> FLUSH PRIVILEGES;

另外 MySQL 預設是監聽在 127.0.0.1 上,如果要遠端訪問,需修改設置:

(常見的 MySQL 設置檔案位置如下)

/etc/my.cnf
/etc/mysql/my.cnf
$MYSQL_HOME/my.cnf
[datadir]/my.cnf
~/.my.cnf

使用虛擬編輯器如 nano 修改,將 bind-address 註解掉或改成 0.0.0.0,然後存檔。

[mysqld]
# Only allow connections from localhost
#bind-address = 127.0.0.1

重啟 MySQL 8.0,即可生效。

$ service mysql restart

 

以上是如何在 Ubuntu 18.04 安裝 MySQL 8.0 的教學,如果您對於安裝 MySQL 有什麼回饋,歡迎您透過以下表單聯繫我們。Happy Coding !