天天看點

Linux 系統手動部署 MySQL 資料庫

Linux 系統手動部署 MySQL 資料庫

鏡像下載下傳、域名解析、時間同步請點選

阿裡巴巴開源鏡像站

一、前提條件

進行操作前,請確定您已經注冊了阿裡雲賬号。如還未注冊,請先完成

賬号注冊

您已在ECS執行個體所使用的安全組入方向添加規則并放行3306端口。具體步驟,請參見

添加安全組規則

二、背景資訊

本文在示例步驟中使用了以下版本軟體。實際操作時,請以您的軟體版本為準。

  • 作業系統:公共鏡像CentOS 7.2 64位
  • MySQL:5.7.26

本文在示例步驟中使用了以下配置的ECS執行個體。 實際操作時,請以您的執行個體配置為準。

  • CPU:2 vCPU
  • 記憶體:4 GiB
  • 網絡類型:專有網絡
  • IP位址:公網IP

三、安裝步驟

1. 準備環境

連接配接您的ECS執行個體。具體操作,請參見

使用SSH密鑰對連接配接Linux執行個體

使用使用者名密碼驗證連接配接Linux執行個體

2. 安裝MySQL

完成以下操作,安裝MySQL。

1)運作以下指令更新YUM源。

rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm           

2)運作以下指令安裝MySQL。

yum -y install mysql-community-server           

3)運作以下指令檢視MySQL版本号。

mysql -V           

4)傳回結果如下,表示MySQL安裝成功。

mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper           

3. 配置MySQL

完成以下操作,配置MySQL。

1)運作以下指令啟動MySQL服務。

systemctl start mysqld           

2)運作以下指令設定MySQL服務開機自啟動。

systemctl enable mysqld           

3)運作以下指令檢視/var/log/mysqld.log檔案,擷取并記錄root使用者的初始密碼。

# grep 'temporary password' /var/log/mysqld.log
2019-04-28T06:50:56.674085Z 1 [Note] A temporary password is generated for root@localhost: 3w)WqGlM7-o,           
說明 下一步重置root使用者密碼時,會使用該初始密碼。

4)運作下列指令對MySQL進行安全性配置。

mysql_secure_installation           

安全性的配置包含以下五個方面:

  • 重置root使用者的密碼。
Enter password for user root: #輸入上一步擷取的root使用者初始密碼
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.
Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root使用者密碼,輸入Y
New password: #輸入新密碼,長度為8至30個字元,必須同時包含大小寫英文字母、數字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
Re-enter new password: #再次輸入新密碼
Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y           
  • 輸入

    Y

    删除匿名使用者賬号。
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  #是否删除匿名使用者,輸入Y
Success.           
  • Y

    禁止root賬号遠端登入。
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root遠端登入,輸入Y
Success.           
  • Y

    删除test庫以及對test庫的通路權限。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test庫和對它的通路權限,輸入Y
- Dropping test database...
Success.           
  • Y

    重新加載授權表。
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加載授權表,輸入Y
Success.
All done!           

安全性配置的更多詳情,請參見

MySQL官方文檔

4. 遠端通路MySQL資料庫

您可以使用資料庫用戶端或阿裡雲提供的資料管理服務DMS(Data Management Service)來遠端通路MySQL資料庫。本節以DMS為例,介紹遠端通路MySQL資料庫的操作步驟。

1)在ECS執行個體上,建立遠端登入MySQL的賬号。

  • 運作以下指令後,輸入root使用者的密碼登入MySQL。
mysql -uroot -p           
  • 依次運作以下指令建立遠端登入MySQL的賬号。示例賬号為

    dms

    、密碼為

    123456

mysql> grant all on *.* to 'dms'@'%'IDENTIFIED BY '123456'; #使用root替換dms,可設定為允許root賬号遠端登入。
mysql> flush privileges;           
說明
  • 建議您使用非root賬号遠端登入MySQL資料庫。
  • 實際建立賬号時,需将

    123456

    更換為符合要求的密碼:長度為8至30個字元,必須同時包含大小寫英文字母、數字和特殊符号。特殊符号可以是

    ()

    ~!@#$%^&*-+=|{}[]:;‘<>,.?/`。

2)登入

資料管理控制台

3)在左側導航欄中,選擇自建庫(ECS、公網)。

4)單擊建立資料庫。

5)配置自建資料庫資訊。 配置詳情,請參見

配置自建資料庫

6)單擊登入。成功登入後,您可以使用DMS提供的菜單欄功能,建立資料庫、表、函數等,詳情請參見

管理ECS自建資料庫
提供全面,高效和穩定的系統鏡像、應用軟體下載下傳、域名解析和時間同步服務。”