Search

RDS

서브넷 그룹 생성 - data subnet 선택

Aurora MySQL 선택

프로덕션 선택

클러스터 식별자 : wsi-rds-instance

t3.medium 선택

연결 옵션 세팅

VPC 선택 및 서브넷 그룹 선택

퍼블릭 액세스 비활성화 선택 및 보안 그룹 선택

로그 활성화

초기 DB Name : dev

데이터 삽입 코드

pip3 install pymysql
Shell
복사
import pymysql import time # RDS 연결 정보 connection = pymysql.connect( host='<RDS ENDPOINT>', user='admin', password='admin123!!', database='dev' ) def clean_value(value): # 값에서 불필요한 공백과 따옴표 처리 return value.strip().strip("'").strip('"') try: with connection.cursor() as cursor: # SQL 파일 읽기 with open('insert.sql', 'r', encoding='utf-8') as file: content = file.read() # VALUES 부분만 추출 if 'VALUES' in content: values_part = content.split('VALUES')[1].strip() # 괄호 쌍을 기준으로 값들을 분리 values_list = [] current_value = "" parentheses_count = 0 for char in values_part: if char == '(': parentheses_count += 1 elif char == ')': parentheses_count -= 1 current_value += char if parentheses_count == 0 and current_value.strip(): # 값 쌍 하나가 완성됨 values_list.append(current_value.strip().strip(',')) current_value = "" # 배치 처리 batch_size = 1000 # 배치 크기를 줄임 total = len(values_list) for i in range(0, total, batch_size): batch = values_list[i:i + batch_size] # 배치 쿼리 생성 batch_query = "INSERT INTO dev.product (id, name) VALUES " + \ ','.join(item for item in batch if item) try: cursor.execute(batch_query) connection.commit() print(f"Successfully processed {min(i + batch_size, total)}/{total} records") time.sleep(1) # 서버 부하 방지를 위한 대기 except Exception as e: print(f"Error at batch {i//batch_size + 1}: {str(e)}") connection.rollback() continue except Exception as e: print(f"An error occurred: {str(e)}") finally: connection.close() print("Connection closed.")
Python
복사