ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ํฌ๋กค๋งํ•œ ๋ฐ์ดํ„ฐ๋ฅผ MySQL Docker ์ปจํ…Œ์ด๋„ˆ์— ์ €์žฅํ•˜๊ธฐ
    Project/DEVOOK 2022. 1. 31. 23:52

    2022.01.31 - [Project/๐Ÿ“šDEVOOK] - Selenium์„ ์‚ฌ์šฉํ•œ ๋™์  ํŽ˜์ด์ง€ ํฌ๋กค๋Ÿฌ ๊ตฌํ˜„

    ์•ž์„œ ์ˆ˜์ง‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ „์ฒ˜๋ฆฌ ๋ฐ ๋ฒกํ„ฐํ™” ์ž‘์—…์„ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํฌ๋กค๋งํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜์˜€๋‹ค. 

    ์—ฌ๋Ÿฌ ์ปดํ“จํ„ฐ ํ™˜๊ฒฝ ๋ฐ AWS EC2 ์„œ๋ฒ„์—์„œ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Docker Compose๋กœ MySQL ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ์„ฑํ•˜์˜€๋‹ค.

     

    โœ”๏ธ๋ชจ๋ธ๋ง

     

     

     

     

     

    ๊ฐ„๋‹จํ•˜๊ฒŒ url, title, description, category ํ•„๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ํ…Œ์ด๋ธ” 1๊ฐœ๋กœ ๊ตฌ์„ฑํ•˜์˜€๋‹ค. 

     

     

    โœ”๏ธdocker-compose.yml ํŒŒ์ผ ๊ตฌ์„ฑ

    # docker-compose-dev.yml
    
    version: "3"
    
    services:
      db:
        image: mysql
        container_name: devook-db
        ports:
          - "3306:3306"
        volumes:
          - ./db/conf.d:/etc/mysql/conf.d
        command:
          --default-authentication-plugin=mysql_native_password
        restart: always
        environment:
          MYSQL_DATABASE: devook_db
          MYSQL_ROOT_PASSWORD: cool
          TZ: "Asia/Seoul"

    MySQL ๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ ์‹œ์ž‘๋  ๋•Œ, environment ์˜ต์…˜์œผ๋กœ MySQL ์ธ์Šคํ„ด์Šค์˜ ๊ตฌ์„ฑ์— ๋Œ€ํ•œ 1๊ฐœ ์ด์ƒ์˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    MYSQL_ROOT_PASSWORD : ํ•„์ˆ˜ ์„ค์ •์œผ๋กœ, root superuser ๊ณ„์ •์— ๋Œ€ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ root superuser๋Š” ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค. 

    MYSQL_DATABASE : ์„ ํƒ ์‚ฌํ•ญ์œผ๋กœ, ์ด๋ฏธ์ง€๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ๋ช…์‹œ๋œ ์ด๋ฆ„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. MYSQL_USER, MYSQL_PASSWORD ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•ด superuser ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ถ€์—ฌ๋ฐ›๋Š”๋‹ค. (GRANT ALL๊ณผ ๋™์ผ)

     

    mysql docker image - Environment Variables

     

    ๐Ÿ’ก์ด๋ชจ์ง€ ์ €์žฅ์„ ์œ„ํ•œ MySQL ์„ค์ • ์ถ”๊ฐ€

    ๋ธ”๋กœ๊ทธ ๊ธ€์˜ ํŠน์„ฑ์ƒ ์ด๋ชจ์ง€๊ฐ€ ๋นˆ๋ฒˆํžˆ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ์ด๋ฅผ MySQL์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ถ”๊ฐ€ ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

     

    #๏ธโƒฃ MySQL ์„ค์ • ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

    ๊ธฐ๋ณธ MySQL ์„ค์ •์€ /etc/mysql/my.cnf ํŒŒ์ผ์— ์ž‘์„ฑ๋˜์–ด ์žˆ๊ณ , ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    /etc/mysql/my.cnf

    ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” ๋‚ด์šฉ์„ ๋ณด๋ฉด, Custom config๋Š” /etc/mysql/conf.d/ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์— ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋‹ค.

    !includedir : ๋ช…์‹œ๋œ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์˜ ๋ชจ๋“  .cnf ํŒŒ์ผ๋“ค์„ ํฌํ•จํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

     

    ๋”ฐ๋ผ์„œ ๋กœ์ปฌ์—์„œ ์ถ”๊ฐ€๋กœ ์ปค์Šคํ…€ ์„ค์ •์ด ํฌํ•จ๋œ .cnf ํŒŒ์ผ์„ ๊ตฌ์„ฑํ•˜์—ฌ ํ•ด๋‹น ํŒŒ์ผ์„ docker ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ /etc/mysql/conf.d/์— ์ „๋‹ฌํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

    ์œ„์—์„œ ์ž‘์„ฑํ•œ docker-compose.yml์˜ volumes ์˜ต์…˜์„ ํ†ตํ•ด ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    MySQL ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ๊ธฐ์กด์˜ /etc/mysql/my.cnf์™€ /etc/mysql/conf.d/~~.cnf์˜ ์„ค์ •์ด ์ข…ํ•ฉ์ ์œผ๋กœ ๋ฐ˜์˜๋œ๋‹ค.

     

    #๏ธโƒฃ MySQL ์„ค์ • ์ถ”๊ฐ€

    MySQL์˜ UTF-8์€ 3byte๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฒ”์œ„๋‚ด์˜ Char๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋ชจ์ง€์ฒ˜๋Ÿผ 4byte๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฌธ์ž๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” MySQL ์„œ๋ฒ„์™€ DBMS์˜ ์ธ์ฝ”๋”ฉ์„ utf8mb4๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

    # /conf.d/my.cnf
    [client]
    default-character-set = utf8mb4
    
    [mysql]
    default-character-set = utf8mb4
    
    [mysqld]
    character-set-client-handshake = FALSE
    character-set-server           = utf8mb4
    collation-server               = utf8mb4_unicode_ci

    ์œ„์™€ ๊ฐ™์ด []์— ์ž‘์„ฑ๋œ ๊ทธ๋ฃน๋ณ„๋กœ ์˜ต์…˜์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    [client] : ๋ชจ๋“  ์—ฐ๊ฒฐ๋œ clients์—๊ฒŒ ๋ฐ˜์˜๋จ (mysql cli ํฌํ•จ)

    [mysql] : mysql command line client์— ๋ฐ˜์˜๋จ

    [mysqld] : mysql server์— ๋ฐ˜์˜๋จ

     

    stackoverflow - MySQL configuration file sections

    MySQL 8 - Option files

     

    โœ”๏ธMySQL ์—ฐ๊ฒฐ ๋ฐ ๋ฐ์ดํ„ฐ ์ €์žฅ 

    ๐Ÿ’กdocker ๊ด€๋ จ ๋ช…๋ น์–ด

    $ docker-compose -f docker-compose-dev.yml up -d
    $ docker-compose -f docker-compose-dev.yml down 
    
    # ์ปจํ…Œ์ด๋„ˆ ์ ‘์†
    $ docker exec -it devook-db /bin/bash
    # MySQL ์ ‘์† 
    $ mysql -u root -p cool

     

    mysqlclient ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ MySQLdb ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. 

    import MySQLdb
    
    def connect_db(user, passwd, host, db):
        return MySQLdb.connect(
            user=user,
            passwd=passwd,
            host=host,
            db=db
        )
    
    
    def disconnect_db(conn):
        conn.commit()
        conn.close()
    
    
    def insert_into_database(items, user, passwd, host, db):
        conn = connect_db(user=user, passwd=passwd, host=host, db=db)
        cursor = conn.cursor()
        cursor.execute("DROP TABLE IF EXISTS post")  # ํ•ด๋‹น ํ…Œ์ด๋ธ”์ด ์ด๋ฏธ ์กด์žฌํ•  ๊ฒฝ์šฐ ์‚ญ์ œ
        cursor.execute(
            "CREATE TABLE post "
            "(id            int AUTO_INCREMENT PRIMARY KEY, "
            "url            char, "
            "title          char, "
            "description    text, "
            "category       char)"
        )  # ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    
        sql = "INSERT INTO post(url, title, description, category) VALUES(%s, %s, %s, %s)"
        for item in items:
            values = (item[0], item[1], item[2], item[3])
            cursor.execute(sql, values)
    
        disconnect_db(conn=conn)

     

    ๋Œ“๊ธ€

Designed by Tistory.