[教學][Ubuntu 架站] 在 Ubuntu 20.04 上安裝 MySQL Server

在本指南中,我們將在 Ubuntu 20.04 伺服器上安裝和配置  MySQL 。

前言

在本指南中,我們將在 Ubuntu 20.04 上安裝和配置 MySQL。 MySQL在過去由於效能高、成本低、可靠性好,已經成為最流行的開源資料庫,因此被廣泛地應用在 Internet 上的中小型網站中。隨著 MySQL 的不斷成熟,它也逐漸用於更多大規模網站和應用,比如維基百科、Google 和 Facebook 等網站。非常流行的開源軟體組合 LAMP 中的「M」指的就是MySQL。


預先準備

在開始操作此篇文章前,需要先準備好 Ubuntu 20.04 的主機,如果尚未準備好,可以參考此篇文章([教學][Ubuntu 架站] 如何在 Google Cloud Platform 架設 Ubuntu 伺服器)


1. 安裝MySQL

先從更新軟件包(Package )並在 Ubuntu 20.04 上安裝 MySQL 開始。下面我們有兩個由 && 分隔的命令。第一個命令將更新軟件包列表以確保可獲得 MySQL 的最新版本和依賴項。然後第二個命令將下載並安裝 MySQL。

$ sudo apt update && sudo apt install mysql-server

當提示安裝 MySQL 軟件包時,按 y 和 ENTER。

軟件包安裝 MySQL 完成後,我們可以檢查 MySQL 服務是否正在運行。

$ sudo service mysql status

如果正在運行,您將看到如下所示的綠色 Active 狀態。

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-09-08 07:16:49 UTC; 18s ago
   Main PID: 32713 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4705)
     Memory: 356.6M
     CGroup: /system.slice/mysql.service
             └─32713 /usr/sbin/mysqld
Sep 08 07:16:48 ubuntu-20 systemd[1]: Starting MySQL Community Server...
Sep 08 07:16:49 ubuntu-20 systemd[1]: Started MySQL Community Server.

2. 設定安全性(Security)

您現在應該運行 mysql_secure_installation 為 MySQL Server 配置安全性。

$ sudo mysql_secure_installation

如果在第 1 步中創建了 root 密碼,系統可能會提示你在此處輸入密碼。否則,您將被要求創建一個新的。 

系統會詢問您是否要設定驗證密碼插件(VALIDATE PASSWORD PLUGIN)。除非想要強制執行嚴格的密碼規則,不然這並不是真正需要的。


Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN 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 plugin?
Press y|Y for Yes, any other key for No: 

如果不想設置驗證密碼插件(VALIDATE PASSWORD PLUGIN),請在此處直接按 ENTER。

Please set the password for root here.

New password: 

Re-enter new password: 

如果沒有在第 1 步中創建 root 密碼,現在必須在這邊建立一個。

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

按 y 和 ENTER 刪除匿名用戶。

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

按 y 和 ENTER 禁止遠端 root 登錄。這將防止機器人和駭客嘗試猜測 root 密碼。

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

按 y 和 ENTER 刪除測試資料庫(Database)。

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 和 ENTER 重新載入權限表(Privilege Tables)。

以上步驟完成後,即可測試 MySQL 是否正常運作。以下登錄 MySQL Server 並運行 version 命令。

$ sudo mysqladmin -p -u root version

輸入前面建立的 MySQL root 密碼,應該會看到以下內容:

mysqladmin  Ver 8.0.26-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version          8.0.26-0ubuntu0.20.04.2
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 6 min 23 sec
Threads: 2  Questions: 11  Slow queries: 0  Opens: 130  Flush tables: 3  Open tables: 49  Queries per second avg: 0
.028

現在已經成功地在 Ubuntu 20.04 上安裝和配置了 MySQL。


相關文章

Leave a Reply

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *