Tiny Bunny

Rookies/인프라 활용을 위한 파이썬

[SK shieldus Rookies 19기] MySQL 실습 환경 구축

bento 2024. 3. 8. 13:43
[SK쉴더스 Rookies 19기] 클라우드 기반 스마트 융합보안 과정

01. MySQL

MySQL Installer 8.0.36 다운로드!

https://dev.mysql.com/downloads/installer/

 

MySQL :: Download MySQL Installer

Note: MySQL 8.0 is the final series with MySQL Installer. As of MySQL 8.1, use a MySQL product's MSI or Zip archive for installation. MySQL Server 8.1 and higher also bundle MySQL Configurator, a tool that helps configure MySQL Server.

dev.mysql.com

 

실습을 위한 데이터베이스 생성

create schema sampledb default character set utf8;

use sampledb;

create table members (
    member_id 		int(11) 	not null auto_increment 	comment '회원 아이디', 
    member_name	varchar(100)		not null 		   	comment	'회원 이름', 
    member_age		int(3)						comment '회원 나이', 
    member_email	varchar(100)					comment '회원 이메일', 
    primary key(member_id)
);

 

테스트 데이터 추가

use sampledb;

insert into members (member_name, member_age, member_email)
values ('홍길동', 23, 'hong@test.com');

 

pymysql 라이브러리를 설치(파이썬 - mysql 연동 목적)

>>> pip install pymysql

 

데이터를 조회해보기!

import pymysql


# 데이터베이스 연결
try:
    with pymysql.connect(host="localhost", port=3306, user="springboot", passwd="p@ssw0rd", db="sampledb") as conn:
        # 조회 쿼리
        query = "select member_id, member_name, member_age, member_email from members"


        # 커서 생성 후 조회 쿼리를 실행
        with conn.cursor() as curr:
            curr.execute(query)


            # 조회 결과 출력 
            for c in curr:
                print(c)


except pymysql.MySQLError as e:
    print(e)

 

02. Faker 라이브러리 활용

Faker 라이브러리 설치

 

from faker import Faker

faker = Faker('ko-KR')      # 한국 설정

for i in range(10):
    print(faker.name(), faker.address(), faker.email())

실행 결과

 

사용 가능한 fake 데이터들!

 

728x90