brunch

You can make anything
by writing

C.S.Lewis

by Master Seo May 06. 2022

189. 테라폼- GCP-Cloud SQL 생성, 이론

<1> 테라폼으로 Cloud SQL 생성하는법을 설명하는 이론 과정이다.

<2> Cloud shell 환경 설정

<3> 테라폼 초기화 , 적용9

<4> Cloud SQL proxy 프로그램 설치와 테스트

<5> 콘솔에서 Cloud SQL 생성 확인




<1> 테라폼으로 Cloud SQL 생성하는법을 설명하는 이론 과정이다.


작업 순서에 대해 이해 시키는게 목적이다.

SQL 인스턴스 생성

Cloud SQL Proxy 설치

연결하기



<2> Cloud shell 환경 설정


1

gcloud auth list


2

gcloud config list project


3

mkdir sql-with-terraform

cd sql-with-terraform



4

main.tf 파일 내용


provider 설정

resource sql-database 설정


5

output.tf

output  인스턴스 이름


6

variables.tf

variable  프로젝트

variable  리전

variable  데이터베이스 버전

variable  데이터베이스 이름

variable  데이터베이스 문자셋

variable  사용자 이름

variable  암호

variable  디스크 사이즈




<3> 테라폼 초기화 , 적용


1

terraform init


2

terraform plan -out=tfplan


3

terraform apply tfplan


(10분)


Outputs:

generated_user_password = <sensitive>

instance_address = "34.70.230.23"

instance_address_time_to_retire = ""

instance_name = "example-mysql-ae75"

self_link = "https://www.googleapis.com/sql/v1beta4/projects/secret-zephyr-348001/instances/example-mysql-ae75"




<4> Cloud SQL proxy 프로그램 설치와 테스트


1

wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy


chmod +x cloud_sql_proxy


Cloud SQL Proxy 설명

https://cloud.google.com/sql/docs/mysql/configure-ip

https://cloud.google.com/sql/docs/mysql/configure-ssl-instance




2

test


export GOOGLE_PROJECT=$(gcloud config get-value project)


MYSQL_DB_NAME=$(terraform output -json | jq -r '.instance_name.value')

MYSQL_CONN_NAME="${GOOGLE_PROJECT}:us-central1:${MYSQL_DB_NAME}"


./cloud_sql_proxy -instances=${MYSQL_CONN_NAME}=tcp:3306



3

Cloud Shell tab 에서  plus (+) icon


cd ~/sql-with-terraform

echo MYSQL_PASSWORD=$(terraform output -json | jq -r '.generated_user_password.value')


mysql -udefault -p --host 127.0.0.1 default


MYSQL_PASSWORD=b125c2e91


Enter password:  b125c2e91

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 214

Server version: 5.6.51-google (Google)


Copyright (c) 2000, 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 '\h' for help. Type '\c' to clear the current input statement.


mysql>






<5> 콘솔에서 Cloud SQL 생성 확인






https://brunch.co.kr/@topasvga/2419



감사합니다.


매거진의 이전글 188. 테라폼- GCP-Regional LB, 이론

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari