작업 순서에 대해 이해 시키는게 목적이다.
SQL 인스턴스 생성
Cloud SQL Proxy 설치
연결하기
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 인스턴스 이름
6
variable 프로젝트
variable 리전
variable 데이터베이스 버전
variable 데이터베이스 이름
variable 데이터베이스 문자셋
variable 사용자 이름
variable 암호
variable 디스크 사이즈
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"
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>
https://brunch.co.kr/@topasvga/2419
감사합니다.