開發與維運

CentOS 系統手動部署 MySQL 數據庫

85.jpeg
鏡像下載、域名解析、時間同步請點擊 阿里巴巴開源鏡像站

一、前提條件

  • 已註冊阿里雲賬號。
  • 如果在中國內地地域中使用雲服務器ECS,請確保賬號已完成實名認證。
  • 已創建一臺ECS實例。詳細步驟請參見使用嚮導創建實例

二、背景信息

本教程在示例步驟中使用了以下實例規格和版本軟件。實際操作時,請以您的軟件版本為準。

  • 實例規格:ecs.c6.large(2 vCPU,4 GiB內存)
  • 操作系統:公共鏡像CentOS 7.2 64位
  • MySQL:5.7.26
  • 數據庫端口:3306

說明 您需要在ECS實例所使用的安全組入方向添加規則並放行3306端口。具體步驟,請參見添加安全組規則

三、操作步驟

步驟一:準備環境

遠程連接您的ECS實例。具體操作,請參見使用SSH密鑰對連接Linux實例使用用戶名密碼驗證連接Linux實例

步驟二:安裝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

返回結果如下,表示MySQL安裝成功。

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

步驟三:配置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,

說明 下一步對MySQL進行安全性配置時,會使用該初始密碼。

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.
  • 禁止root賬號遠程登錄。
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root遠程登錄,輸入Y
Success.
  • 刪除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.
  • 重新加載授權表。
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加載授權表,輸入Y
Success.
All done!

安全性配置的更多詳情,請參見MySQL官方文檔

步驟四:遠程訪問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個字符,必須同時包含大小寫英文字母、數字和特殊符號。特殊符號可以是() ~!@#$%^&*-+=|{}[]:;‘<>,.?/`。
  1. 登錄數據管理控制檯
  2. 在左側導航欄中,選擇自建庫(ECS、公網)
  3. 單擊新增數據庫
  4. 配置自建數據庫信息。 配置詳情,請參見管理ECS實例自建數據庫
  5. 單擊登錄。成功登錄後,您可以使用DMS提供的菜單欄功能,創建數據庫、表、函數等。

阿里巴巴開源鏡像站 提供全面,高效和穩定的鏡像下載服務。釘釘搜索 ' 21746399 ‘ 加入鏡像站官方用戶交流群。”

Leave a Reply

Your email address will not be published. Required fields are marked *