Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Jak połączyć się z bazą danych Cloud SQL MySQL z App Engine?

POŁĄCZENIE Z APP ENGINE (FLEX I STANDARD) DO CLOUD SQL PRZY UŻYCIU GNIAZD DOMENY TCP I UNIX 2020

1.Utwórz nowy projekt

gcloud projects create con-ae-to-sql
gcloud config set project con-ae-to-sql
gcloud projects describe con-ae-to-sql

2. Włącz płatności w swoim projekcie:https://cloud.google .com/billing/docs/how-to/modify-project

3. Uruchom następujące polecenie gcloud, aby włączyć App Engine i utworzyć powiązane zasoby aplikacji

gcloud app create -region europe-west2
gcloud app describe
#Remember the location of you App Engine aplication, because we will create all our resources on the same region

4. Ustaw metadane obliczeniowe informacji o projekcie:

 gcloud compute project-info describe --project con-ae-to-sql
 #Enable the Api, and you can check that default-region,google-compute-default-zone are not set. Set the metadata.
 gcloud compute project-info add-metadata --metadata google-compute-default-region=europe-west2,google-compute-default-zone=europe-west2-b

5. Włącz interfejs API sieci usług:

gcloud services list --available
gcloud services enable servicenetworking.googleapis.com

6. Utwórz 2 instancje cloud sql (jedną z wewnętrznym adresem IP i jedną z publicznym adresem IP)- https://cloud.google.com/sql/docs/mysql/create-instance :

6. Instancja Cloud Sql z zewnętrznym adresem IP:

#Create the sql instance in the same region as App Engine Application
gcloud --project=con-ae-to-sql beta sql instances create database-external --region=europe-west2
#Set the password for the "[email protected]%" MySQL user:
gcloud sql users set-password root --host=% --instance database-external --password root 
#Create a user
gcloud sql users create user_name --host=% --instance=database-external  --password=user_password
#Create a database
gcloud sql databases create user_database --instance=database-external
gcloud sql databases list --instance=database-external

6.b Instancja Cloud Sql z wewnętrznym adresem IP:

i.#Create a private connection to Google so that the VM instances in the default VPC network can use private services access to reach Google services that support it.

gcloud compute addresses create google-managed-services-my-network     --global  --purpose=VPC_PEERING --prefix-length=16  --description="peering range for Google"  --network=default --project=con-ae-to-sql
gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com --ranges=google-managed-services-my-network  --network=default  --project=con-ae-to-sql
#Check whether the operation was successful.
gcloud services vpc-peerings operations describe     --name=operations/pssn.dacc3510-ebc6-40bd-a07b-8c79c1f4fa9a
#Listing private connections
gcloud services vpc-peerings list --network=default --project=con-ae-to-sql
 
ii.Create the instance:

gcloud --project=con-ae-to-sql beta sql instances create database-ipinternal --network=default --no-assign-ip  --region=europe-west2
#Set the password for the "[email protected]%" MySQL user:
gcloud sql users set-password root --host=% --instance database-ipinternal --password root
#Create a user
gcloud sql users create user_name --host=% --instance=database-ipinternal  --password=user_password
#Create a database
gcloud sql databases create user_database --instance=database-ipinternal
gcloud sql databases list --instance=database-ipinternal 


gcloud sql instances list
gcloud sql instances describe database-external
gcloud sql instances describe database-ipinternal
#Remember the instances connectionName

OK, więc mamy dwie instancje mysql, połączymy się z App Engine Standard do wewnętrznej bazy danych za pomocą dostępu bezserwerowego i TCP, z App Engine Standard do zewnętrznej bazy danych za pomocą gniazda domeny unix, z App Engine Flex do wewnętrznej bazy danych za pomocą TCP, i z App Engine Flex do zewnętrznej bazy danych przy użyciu gniazda domeny unix.

7. Włącz interfejs Cloud SQL Admin API

gcloud services list --available
gcloud services enable sqladmin.googleapis.com

8. Obecnie standardowe środowiska App Engine nie obsługują łączenia się z instancją Cloud SQL przy użyciu protokołu TCP. Twój kod nie powinien próbować uzyskać dostępu do instancji przy użyciu adresu IP (takiego jak 127.0.0.1 lub 172.17.0.1), chyba że skonfigurowałeś bezserwerowy dostęp do VPC. Skonfigurujmy więc bezserwerowy dostęp do VPC.

8.a Upewnij się, że w Twoim projekcie włączono interfejs API bezserwerowego dostępu do VPC:

gcloud services enable vpcaccess.googleapis.com

8.b Utwórz złącze:

gcloud compute networks vpc-access connectors create serverless-connector --network default --region europe-west2 --range 10.10.0.0/28
#Verify that your connector is in the READY state before using it
gcloud compute networks vpc-access connectors describe serverless-connector --region europe-west2

9.App Engine używa konta usługi do autoryzacji połączeń z Cloud SQL. To konto usługi musi mieć odpowiednie uprawnienia, aby pomyślnie nawiązać połączenie. Unless otherwise configured, the default service account is in the format [email protected] nt.com. Upewnij się, że konto usługi dla Twojej usługi ma następujące role uprawnień:Klient Cloud SQL, a do łączenia się z App Engine Standard z Cloud Sql na wewnętrznym adresie IP potrzebujemy również roli Compute Network User.

gcloud iam service-accounts list
gcloud projects add-iam-policy-binding con-ae-to-sql --member serviceAccount:[email protected] --role roles/cloudsql.client
gcloud projects add-iam-policy-binding con-ae-to-sql --member serviceAccount:[email protected] --role roles/compute.networkUser

Teraz, kiedy skonfigurowałem konfigurację

1. Połącz z App Engine Standard z Cloud Sql za pomocą Tcp i gniazda domeny unix

cd app-engine-standard/
ls
#app.yaml  main.py requirements.txt

cat requirements.txt
Flask==1.1.1
sqlalchemy
pymysql
uwsgi==2.0.18

cat app.yaml
runtime: python37
entrypoint: uwsgi --http-socket :8080 --wsgi-file main.py --callable app --master --processes 1 --threads 2
vpc_access_connector:
    name: "projects/con-ae-to-sql/locations/europe-west2/connectors/serverless-connector" 



cat main.py

from flask import Flask
import pymysql
from sqlalchemy import create_engine

# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)

@app.route('/')
def hello():
    engine_tcp = create_engine('mysql+pymysql://user_name:[email protected]:3306')
    existing_databases_tcp = engine_tcp.execute("SHOW DATABASES;")
    con_tcp = "Connecting from APP Engine Standard to Cloud SQL using TCP: databases => " + str([d[0] for d in existing_databases_tcp]).strip('[]') + "\n"
    engine_unix_socket = create_engine('mysql+pymysql://user_name:[email protected]/user_database?unix_socket=/cloudsql/con-ae-to-sql:europe-west2:database-external')
    existing_databases_unix_socket = engine_unix_socket.execute("SHOW DATABASES;")
    con_unix_socket = "Connecting from APP Engine Standard to Cloud SQL using Unix Sockets: tables in sys database:  => " + str([d[0] for d in existing_databases_unix_socket]).strip('[]') + "\n"
    return con_tcp + con_unix_socket



gcloud app deploy -q
gcloud app browse
#Go to https://con-ae-to-sql.appspot.com
#Connecting from APP Engine Standard to Cloud SQL using TCP: databases => 'information_schema', 'user_database', 'mysql', 'performance_schema', 'sys' Connecting from APP Engine Standard to Cloud SQL using Unix Sockets: tables in sys database: => 'information_schema', 'user_database', 'mysql', 'performance_schema', 'sys'

SUKCES!

2.Połącz z App Engine Flex z Cloud Sql za pomocą Tcp i gniazda domeny unix

cd app-engine-flex/
ls
#app.yaml  main.py requirements.txt

cat requirements.txt
Flask==1.1.1
gunicorn==19.9.0
sqlalchemy
pymysql

cat app.yaml
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
  python_version: 3
#Using TCP and unix sockets domain
beta_settings:
 cloud_sql_instances: con-ae-to-sql:europe-west2:database-ipinternal=tcp:3306,con-ae-to-sql:europe-west2:database-external
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10


cat main.py

from flask import Flask
import pymysql
from sqlalchemy import create_engine

app = Flask(__name__)

@app.route('/')
def hello():
    engine_tcp = create_engine('mysql+pymysql://user_name:[email protected]:3306')
    existing_databases_tcp = engine_tcp.execute("SHOW DATABASES;")
    con_tcp = "Connecting from APP Engine Flex to Cloud SQL using TCP: databases => " + str([d[0] for d in existing_databases_tcp]).strip('[]') + "\n"

    engine_unix_socket = create_engine('mysql+pymysql://user_name:[email protected]/user_database?unix_socket=/cloudsql/con-ae-to-sql:europe-west2:database-external')
    existing_databases_unix_socket = engine_unix_socket.execute("SHOW DATABASES;")
    con_unix_socket = "Connecting from APP Engine Flex to Cloud SQL using Unix Sockets: tables in sys database:  => " + str([d[0] for d in existing_databases_unix_socket]).strip('[]') + "\n"
    return con_tcp + con_unix_socket


gcloud app deploy -q
gcloud app browse
#Go to https://con-ae-to-sql.appspot.com
#Connecting from APP Engine Flex to Cloud SQL using TCP: databases => 'information_schema', 'marian', 'mysql', 'performance_schema', 'sys' Connecting from APP Engine Flex to Cloud SQL using Unix Sockets: tables in sys database: => 'information_schema', 'marian', 'mysql', 'performance_schema', 'sys'

SUKCES!



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Jak zoptymalizować wyszukiwanie pełnotekstowe MySQL Boolean? (Lub czym go zastąpić?) - C#

  2. Jak uzyskać dostęp do internetowej bazy mysql w Androidzie?

  3. Jak mogę zrzucić bazę danych MySQL bez użycia mysqldump w Pythonie?

  4. RuntimeError z mysql2 i rails3 (bundler)

  5. Mysql ERROR 1005 (HY000):Nie można utworzyć tabeli 'tmp' (errno:13)