본문 바로가기

카테고리 없음

OCI Kubernetes와 MySQL 서비스를 활용한 어플리케이션 배포 데모 - #2. MySQL 서비스 배포

글 순서

#1. OKE 클러스터 설치

#2. MySQL 서비스 배포

#3. 쿠버네티스 접속 환경 구성

#4. OCI 레지스트리 설정, 도커 이미지 빌드

#5. 도커 이미지 OCIR에 등록, OKE 클러스터에 컨테이너 배포

 

MySQL 서비스 배포

Databases > MySQL > DB Systems 화면에서 Create DB System를 클릭합니다.

데이터베이스 시스템의 이름을 “demo-mysql”로 입력했고, Standalone으로 생성했습니다. 관리자 계정은 “root”로 입력했습니다.

 

아래쪽으로 내려와서 네트웍 설정을 합니다. 앞서 생성한 데이터베이스용 서브넷으로 설정하고 Create을 클릭합니다.

 

MySQL 서비스 접속 설정 (on Bastion Host)

Bastion 호스트에서 MySQL 데이터베이스로의 접속을 테스트하기 위해 MySQL 클라이언트와 MySQL Shell을 설치했습니다. 설치 후, 아래와 같이 데이터베이스에 접속해서 DB 정보를 확인해 봅니다.

[opc@bastion ~]$ sudo yum install mysql-community-client -y
...
Installed:
  mysql-community-client.x86_64 0:8.0.29-1.el7

Complete!
[opc@bastion ~]$ sudo yum install mysql-shell -y
...
Installed:
  mysql-shell.x86_64 0:8.0.29-1.el7

Complete!
[opc@bastion ~]$ mysqlsh -uroot -p -h 10.0.30.52
Please provide the password for 'root@10.0.30.52': ************
Save password for 'root@10.0.30.52'? [Y]es/[N]o/Ne[v]er (default No):
MySQL Shell 8.0.29

Copyright (c) 2016, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\\help' or '\\?' for help; '\\quit' to exit.
Creating a session to 'root@10.0.30.52'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 24 (X protocol)
Server version: 8.0.29-cloud MySQL Enterprise - Cloud
No default schema selected; type \\use <schema> to set one.
 MySQL  10.0.30.52:33060+ ssl  JS > \\sql
Switching to SQL mode... Commands end with ;
 MySQL  10.0.30.52:33060+ ssl  SQL > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.0011 sec)
 MySQL  10.0.30.52:33060+ ssl  SQL > select version(), current_date, user();
+--------------+--------------+---------------+
| version()    | current_date | user()        |
+--------------+--------------+---------------+
| 8.0.29-cloud | 2022-05-26   | root@10.0.0.3 |
+--------------+--------------+---------------+
1 row in set (0.0022 sec)
 MySQL  10.0.30.52:33060+ ssl  SQL > select user, host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| administrator    | %         |
| ocirpl           | %         |
| root             | %         |
| ociadmin         | 127.0.0.1 |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
+------------------+-----------+
7 rows in set (0.0010 sec)
 MySQL  10.0.30.52:33060+ ssl  SQL > \\exit
Bye!

 

파이썬 샘플 코드 DB 연결 테스트

다음과 같이 데이터베이스에 연결해서 버전, 현재일자, 접속 사용자 정보를 가져오는 샘플 파이썬 코드를 dbconn.py 라는 이름으로 저장했습니다.

[opc@bastion ~]$ cat dbconn.py

import logging
import pymysql
import json

dbhost = '10.0.30.52'
dbuser = 'root'
dbpassword = 'Welcome123!@'
dbname = 'mysql'

logger = logging.getLogger()
logger.setLevel(logging.INFO)

conn = pymysql.connect(host=dbhost, user=dbuser, password=dbpassword, db=dbname, connect_timeout=5)

def list_data():
    try:
        cursor = conn.cursor()
        sql = "select version(), current_date, user()"
        cursor.execute(sql)
    except Exception as ex:
        logger.error("ERROR: Could not fetch data.")
        logger.error(e)

    logger.info("SUCCESS: Fetching data succeeded")

    results = cursor.fetchall()

    json_data = json.dumps(results, default=str)

    cursor.close()

    return json_data

print(list_data())

 

Bastion 호스트 로컬에서 코드 작동 여부를 확인하기 위해 위 파이썬 코드를 실행하는데 필요한 PyMySQL 모듈을 설치합니다.

[opc@bastion ~]$ sudo pip3 install pymysql
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pymysql
  Downloading <https://files.pythonhosted.org/packages/4f/52/a115fe175028b058df353c5a3d5290b71514a83f67078a6482cff24d6137/PyMySQL-1.0.2-py3-none-any.whl> (43kB)
    100% |████████████████████████████████| 51kB 10.3MB/s
Installing collected packages: pymysql
Successfully installed pymysql-1.0.2
[opc@bastion ~]$ python dbconn.py
[["8.0.29-cloud", "2022-05-26", "root@10.0.0.3"]]

 

MySQL 서비스 배포

Databases > MySQL > DB Systems 화면에서 Create DB System를 클릭합니다.

데이터베이스 시스템의 이름을 “demo-mysql”로 입력했고, Standalone으로 생성했습니다. 관리자 계정은 “root”로 입력했습니다.

아래쪽으로 내려와서 네트웍 설정을 합니다. 앞서 생성한 데이터베이스용 서브넷으로 설정하고 Create을 클릭합니다.

 

MySQL 서비스 접속 설정 (on Bastion Host)

Bastion 호스트에서 MySQL 데이터베이스로의 접속을 테스트하기 위해 MySQL 클라이언트와 MySQL Shell을 설치했습니다. 설치 후, 아래와 같이 데이터베이스에 접속해서 DB 정보를 확인해 봅니다.

[opc@bastion ~]$ sudo yum install mysql-community-client -y
...
Installed:
  mysql-community-client.x86_64 0:8.0.29-1.el7

Complete!
[opc@bastion ~]$ sudo yum install mysql-shell -y
...
Installed:
  mysql-shell.x86_64 0:8.0.29-1.el7

Complete!
[opc@bastion ~]$ mysqlsh -uroot -p -h 10.0.30.52
Please provide the password for 'root@10.0.30.52': ************
Save password for 'root@10.0.30.52'? [Y]es/[N]o/Ne[v]er (default No):
MySQL Shell 8.0.29

Copyright (c) 2016, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\\help' or '\\?' for help; '\\quit' to exit.
Creating a session to 'root@10.0.30.52'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 24 (X protocol)
Server version: 8.0.29-cloud MySQL Enterprise - Cloud
No default schema selected; type \\use <schema> to set one.
 MySQL  10.0.30.52:33060+ ssl  JS > \\sql
Switching to SQL mode... Commands end with ;
 MySQL  10.0.30.52:33060+ ssl  SQL > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.0011 sec)
 MySQL  10.0.30.52:33060+ ssl  SQL > select version(), current_date, user();
+--------------+--------------+---------------+
| version()    | current_date | user()        |
+--------------+--------------+---------------+
| 8.0.29-cloud | 2022-05-26   | root@10.0.0.3 |
+--------------+--------------+---------------+
1 row in set (0.0022 sec)
 MySQL  10.0.30.52:33060+ ssl  SQL > select user, host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| administrator    | %         |
| ocirpl           | %         |
| root             | %         |
| ociadmin         | 127.0.0.1 |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
+------------------+-----------+
7 rows in set (0.0010 sec)
 MySQL  10.0.30.52:33060+ ssl  SQL > \\exit
Bye!

 

파이썬 샘플 코드 DB 연결 테스트

다음과 같이 데이터베이스에 연결해서 버전, 현재일자, 접속 사용자 정보를 가져오는 샘플 파이썬 코드를 dbconn.py 라는 이름으로 저장했습니다.

[opc@bastion ~]$ cat dbconn.py

import logging
import pymysql
import json

dbhost = '10.0.30.52'
dbuser = 'root'
dbpassword = 'Welcome123!@'
dbname = 'mysql'

logger = logging.getLogger()
logger.setLevel(logging.INFO)

conn = pymysql.connect(host=dbhost, user=dbuser, password=dbpassword, db=dbname, connect_timeout=5)

def list_data():
    try:
        cursor = conn.cursor()
        sql = "select version(), current_date, user()"
        cursor.execute(sql)
    except Exception as ex:
        logger.error("ERROR: Could not fetch data.")
        logger.error(e)

    logger.info("SUCCESS: Fetching data succeeded")

    results = cursor.fetchall()

    json_data = json.dumps(results, default=str)

    cursor.close()

    return json_data

print(list_data())

 

Bastion 호스트 로컬에서 코드 작동 여부를 확인하기 위해 위 파이썬 코드를 실행하는데 필요한 PyMySQL 모듈을 설치합니다.

[opc@bastion ~]$ sudo pip3 install pymysql
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pymysql
  Downloading <https://files.pythonhosted.org/packages/4f/52/a115fe175028b058df353c5a3d5290b71514a83f67078a6482cff24d6137/PyMySQL-1.0.2-py3-none-any.whl> (43kB)
    100% |████████████████████████████████| 51kB 10.3MB/s
Installing collected packages: pymysql
Successfully installed pymysql-1.0.2
[opc@bastion ~]$ python dbconn.py
[["8.0.29-cloud", "2022-05-26", "root@10.0.0.3"]]

 

<END>