การ Import ข้อมูลจาก MySQL Workbench บน Windows ไปยัง RDS Aurora

การ Import ข้อมูลไปยัง Database RDS Aurora สามารถใช้ MySQL Workbench ในการ Run ข้อมูลลง Database ได้ ซึ่งเป็นวิธีที่สะดวกในการ Import ข้อมูลจำนวนมาก ดังนั้นครั้งนี้จะมาแนะนำการ Import ข้อมูลจาก MySQL Workbench บน Windows ไปยัง RDS Aurora ในบทความนี้ครับ

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

POP จากบริษัท Classmethod (Thailand) ครับ

ครั้งนี้ผมจะมาแนะนำการ Import ข้อมูลจาก MySQL Workbench บน Windows ไปยัง RDS Aurora

เป้าหมาย

สร้าง EC2 Windows และสร้าง RDS Aurora จากนั้นเข้าไปที่ EC2 Windows แล้วติดตั้ง MySQL Workbench แล้วเชื่อมต่อกับ RDS Aurora และ Import ข้อมูลไปยัง RDS Aurora

การดำเนินการในบทความนี้จะทำบน OS: Windows 11 และบน Remote Desktop Connection (Windows ใน EC2)

การสร้าง Microsoft Windows Server ใน Amazon EC2

ดูตัวอย่างการเริ่มต้น Windows ได้ที่ลิงก์ด้านล่างนี้ครับ

ตัวอย่างตั้งค่าการสร้าง EC2 Instance ในบทความนี้

※Create Key pairs

Key pair

Key pairs:tinnakorn-import-win
Key pair type:RSA
Private key file format: ◉pem

※Launch instances

Name and tags

Name:tinnakorn-import-win

Application and OS Images (Amazon Machine Image)

Amazon Machine Image (AMI):Microsoft Windows Server 2022 Base

Instance type

Instance type:t3a.medium

Key pair (login)

Key pair name - required:tinnakorn-import-win

Network settings

Firewall (security groups)

Security group name - required:tinnakorn-import-win
Description - required:tinnakorn-import-win

Inbound security groups rules

Security group rule 1 (TCP, 3389, xxx.xxx.xxx.xxx/32)

Type:rdp ▼| Source type:My IP ▼| Source:xxx.xxx.xxx.xxx/32 ✕

Configure storage

1x:30GiBgp2 ▼Root volume (Default)

การสร้าง Aurora Serverless

ตัวอย่างนี้ผมใช้ Aurora Serverless V1 ซึ่งเราสามารถใช้ RDS (MySQL) ที่ต้องการหรือทำการติดตั้ง MySQL ใน EC2 ก็ได้

ดูตัวอย่างการเริ่มต้น Aurora Serverless ได้ที่ลิงก์ด้านล่างนี้ครับ

ตัวอย่างตั้งค่าการสร้าง RDS Aurora ในบทความนี้

※Create Security Group สำหรับ RDS Aurora

Basic details

Security group name:tinnakorn-import-db
Description:tinnakorn-import-db

Inbound rules
Type Source
MYSQL/Aurora Custom ▼ ?︎ tinnakorn-import-win (พิมพ์ค้นหาชื่อ EC2 ที่สร้างเมื่อสักครู่นี้แล้วเลือก)
sg-ab12cd34xxxx

※Create RDS Aurora

Engine options

Engine type:Amazon Aurora
Edition:Amazon Aurora MySQL-Compatible Edition
Available versions:Aurora (MySQL 5.7) 2.07.1

Templates

⦿Production

Settings

DB instance identifier:tinnakorn-import-db

Credentials Settings

Master username:admin
Master password:PassW0rd (รหัสผ่านนี้เป็นแค่ตัวอย่าง ให้ตั้งรหัสผ่านที่ต้องการ)
Confirm password:PassW0rd

Instance configuration

DB instance class
◉ Serverless
Serverless v1 ▼
Capacity range
Minimum ACUs:1 ACU
Maximum ACUs:1 ACU

Additional scaling configuration

Autoscaling timeout and action:00:05:00
Pause after inactivity:✅ Scale the capacity to 0 ACUs when cluster is idle
00:05:00

Connectivity

Existing VPC security groups:tinnakorn-import-db

Additional configuration

Web Service Data API:✅ Data API

การติดตั้ง MySQL Workbench

ขั้นตอนนี้ดำเนินการใน Remote Desktop Connection

เข้ามาที่ Remote Desktop Connection ของเรา แล้วเปิดเบราว์เซอร์Microsoft Edgeขึ้นมา
แล้วค้นหาคำว่าMySQL WorkbenchและคลิกGo to download
หรือจะคัดลอกลิงก์ด้านล่างนี้ไปเปิดใน Remote Desktop Connection ก็ได้

https://www.mysql.com/products/workbench/

คลิกDownload Now »

เลือก Select Operating System:Microsoft WindowsและคลิกDownload

คลิกNo thanks, just start my download.แล้วจะเบราว์เซอร์จะดำเนินการดาวน์โหลดไฟล์ mysql-workbench-community-8.0.31-winx64 จากนั้นคลิกที่Open fileได้เลย

แล้วเริ่มติดตั้ง MySQL Workbench ได้เลยครับ

การเชื่อมต่อ Database RDS Aurora

ขั้นตอนนี้ดำเนินการใน Remote Desktop Connection

ค้นหา ?︎MySQL Workbenchแล้วเลือกMySQL Workbench 8.0 CE

เมื่อเปิดขึ้นมาแล้วจะแสดงหน้าจอแบบนี้ จากนั้นคลิกแท็บDatabaseและคลิกConnect to Database...

ไปที่หน้า AWS Console Management บน OS (Windows 11) แล้วคัดลอก Endpoint ใน Service [RDS > Database ของเรา] เตรียมไว้

กลับมาที่ Remote Desktop Connection แล้วป้อนข้อมูลตามนี้
» Hostname: วาง Endpoint ที่คัดลอกมาเมื่อสักครู่นี้
» Username: admin (ป้อนตามที่สร้างใน RDS Aurora)
» Password: คลิกStore in Vault...

ป้อน Password ที่สร้างใน RDS Aurora แล้วคลิกOKและคลิกOKอีกครั้งเพื่อเชื่อมต่อไปยัง RDS Aurora

รอการเชื่อมต่อสักครู่

เมื่อเชื่อมต่อเสร็จแล้วจะแสดงหน้าจอแบบนี้

การ Import ข้อมูลจาก MySQL Workbench บน Windows ไปยัง RDS Aurora

ขั้นตอนนี้ดำเนินการใน Remote Desktop Connection

การตรวจสอบ Administration และ Schemas เบื้องต้น

คลิกAdministrationและคลิกServer Statusก็จะเห็น Status ของ Database RDS Aurora (สามารถคลิกปิดหน้าต่างล่างและขวาได้ที่มุมขวาบน)

คลิกSchemasก็จะเห็น Database ของระบบแสดงอยู่ ถ้าเรา Import ข้อมูลเข้ามาแล้ว ข้อมูลก็จะแสดงในนี้ ซึ่งจะอธิบายภายหลัง

การ Download Example Databases

คัดลอกลิงก์ด้านล่างนี้ไปเปิดที่เบราว์เซอร์ใน Remote Desktop Connection

https://dev.mysql.com/doc/index-other.html

แล้วเลื่อนลงมาที่หัวข้อ Example Databases แล้วคลิกZipที่แถวของ world database จากนั้นเบราว์เซอร์จะเริ่มโหลดไฟล์ เมื่อเสร็จแล้วคลิกที่ไอคอนโฟลเดอร์เพื่อไปที่อยู่ของไฟล์

แล้วให้แตกไฟล์ที่ดาวน์โหลดมา (ในตัวอย่างนี้แตกไฟล์ไว้ที่หน้า Desktop) แล้วเปิดไฟล์ดูก็จะแสดงหน้าจอแบบนี้

การ Import ข้อมูลไปยัง RDS Aurora

กลับมาที่โปรแกรม MySQL Workbench แล้วคลิกFileและเลือกOpen SQL Script...

แล้วเลือกไฟล์ SQL ที่แตกออกมาเมื่อสักครู่นี้

เมื่อ Open ไฟล์เสร็จแล้วจะแสดงหน้าจอแบบนี้ จากนั้นให้คลิกไอคอน⚡️Execute the selected portion of the script or everything, if there is no selection เพื่อดำเนินการ Run ข้อมูลลง Database RDS Aurora

ระหว่างที่ระบบกำลัง Run ข้อมูล เราสามารถคลิกที่มุมขวาบนเพื่อเปิดหน้าต่างล่างดูสถานะ Action Output ได้

คลิกSchemasแล้วคลิกปุ่มReloadก็จะเห็น Database: world แสดงขึ้นมา ทีนี้เรามาตรวจสอบเนื้อหาข้างในกันครับ

คลิกTables แล้วคลิกขวา ▶countryและเลือกSelect Rows - Limit 1000

แล้วจะเห็นข้อมูลต่างๆใน Table: country นี้ (คลิกปิดหน้าต่างล่างที่มุมขวาบน)

ต่อไปคลิกขวา ▶cityและเลือกSelect Rows - Limit 1000แล้วจะเห็นข้อมูลต่างๆใน Table: city นี้

เพียงเท่านี้การ Import ข้อมูลก็เสร็จเรียบร้อย

สรุป

จากที่ผมได้ลองใช้ MySQL Workbench ทำการ Import ข้อมูลไปยัง RDS Aurora แล้ว ผมคิดว่าเป็นอีกช่องทางนึงที่สามารถสร้างฐานข้อมูลขนาดใหญ่ได้อย่างรวดเร็วมากๆ ไม่ว่าจะเป็นการสร้างฐานข้อมูลจากไฟล์ SQL ของฐานข้อมูลเก่า หรือจะสร้างฐานข้อมูลขึ้นมาใหม่ ก็สามารถทำได้ง่ายๆ ผ่าน MySQL Workbench นี้ได้เลยครับ

ผมหวังว่าบทความนี้จะเป็นประโยชน์ให้กับผู้อ่านได้นะครับ

POP จากบริษัท Classmethod (Thailand) ครับ !

Link อ้างอิง