sqlalchemy.exc.操作错误:(pymysql.err.操作错误)(2003,"无法连接到MySQL服务器上的'localhost'([错误2]没有这样的文件或目录)")(此错误的背景:http://sqlalche.me/e/e3q8)
它已经很长时间我被困在这个错误,我试图将我的应用程序引擎python代码连接到mysql实例的云sql。这是我第一次使用谷歌cloud.below是我写的代码1。在app.yaml
runtime:
python37
vpc_access_connector:
name: "projects/projectnameandcode/locations/location name/connectors/connector name"
2.要求。txt文件
sqlalchemy
pymysql
总的来说。py公司
导入pymysql
db=sqlalchemy。创建\u引擎(
sqlalchemy.engine.url.URL(
drivername="mysql+pymysql",
username="username",
password=password,
database="databasename",
query={"unix_socket": "/cloudsql/{}".format("instance name")},
),
)a=分贝。connect()
为什么我要面对这个问题?我的Iam角色是所有者或管理员。
快速查看从App Engine连接到云SQL留档,并确保您正确执行了所有步骤。具体来说,请确保以下内容:
应用引擎标准环境不支持使用TCP连接到云SQL实例。除非配置了无服务器VPC访问,否则代码不应尝试使用IP地址(例如127.0.0.1或172.17.0.1)访问实例。
从您的问题中,我了解到您正在使用vpc_access_connector
。因此,我假设您配置了无服务器VPC访问。
main.py中使用的代码用于使用unix域套接字而不是TCP连接到云SQL实例。
1.创建一个新项目
gcloud projects create con-ae-to-sql
gcloud config set project con-ae-to-sql
gcloud projects describe con-ae-to-sql
2、对您的项目启用计费:https://cloud.google.com/billing/docs/how-to/modify-project
3.运行以下gcloud命令启用App Engine并创建关联的应用程序资源
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、设置计算项目信息元数据:
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、启用服务网络Api:
gcloud services list --available
gcloud services enable servicenetworking.googleapis.com
6.创建2个云sql实例,(一个带有内部ip,一个带有公共ip)-https://cloud.google.com/sql/docs/mysql/create-instance:
6、具有外部ip的云Sql实例:
#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 "root@%" 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、内部ip的b云Sql实例:
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 "root@%" 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
好的,我们有两个mysql实例,我们将使用无服务器访问和TCP从App Engine Standard连接到数据库ipinternal,使用unix域套接字从App Engine Standard连接到数据库外部,使用TCP从App Engine Flex连接到数据库ipinternal,使用unix域套接字从App Engine Flex连接到数据库外部。
7.启用云SQL管理API
gcloud services list --available
gcloud services enable sqladmin.googleapis.com
8、目前App Engine标准环境不支持使用TCP连接到云SQL实例。除非配置了无服务器VPC访问,否则代码不应尝试使用IP地址(如127.0.0.1或172.17.0.1)访问实例。因此,让我们配置无服务器VPC访问。
8. a确保为您的项目启用无服务器VPC访问API:
gcloud services enable vpcaccess.googleapis.com
8.b创建连接器:
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使用服务号来授权您与CloudSQL的连接。此服务号必须具有正确的IAM权限才能成功连接。除非另有配置,否则默认服务号采用service-PROJECT_NUMBER@gae-api-prod.google.com.iam.gserviceaccount.com格式。确保服务的服务号具有以下IAM角色:CloudSQLClient,要在内部ip上从App Engine Standard连接到Cloud Sql,我们还需要角色Compute Network User。
gcloud iam service-accounts list
gcloud projects add-iam-policy-binding con-ae-to-sql --member serviceAccount:con-ae-to-sql@appspot.gserviceaccount.com --role roles/cloudsql.client
gcloud projects add-iam-policy-binding con-ae-to-sql --member serviceAccount:con-ae-to-sql@appspot.gserviceaccount.com --role roles/compute.networkUser
现在我配置了设置
1、使用Tcp和unix domanin socket从App Engine Standard连接到云Sql
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:user_password@internal-ip-of-database-ipinternal: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:user_password@/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'
成功!
2.使用Tcp和unix domanin套接字从App Engine Flex连接到Cloud Sql
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:user_password@internal-ip-of-database-ipinternal: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:user_password@/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'
成功!
我正在尝试将MySQL数据库连接到XAMPP。每当我尝试在XAMPP上按start MySQL时,它都不会运行,我会得到一个错误: 这是因为我的病毒防护软件或VPN吗?我将如何更改MySQL和控制面板中的端口?如果有人能给我一些建议或指导我回答与此类似的已回答问题,将不胜感激。谢谢你。
目标是使用云数据融合连接云SQL、mysql或postgreSQL实例。 null
我已经创建了AWS ec2实例并关联到一个安全组。在关联的安全组中,我已经向所有人开放了HTTP(HTTP, TCP,80,0.0.0.0/0)、SSH(SSH, TCP,22,0.0.0.0/0)和ICMP(All ICMP, All, N/A,0.0.0.0/0)。 但当我尝试ping到实例时,它失败了;给出请求超时。入站规则设置 此外,使用PuTTY或openssh连接到实例也会失败。它给出
我正在尝试连接到MySQL服务器,但出现无法处理的错误。 java.sql.SQLNonTransientConnectionException:无法创建到数据库服务器的连接。尝试重新连接3次。放弃。com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)com.mysql.cj.jdbc.excepti
问题内容: 我正在尝试从EC2实例i-78a8df00连接到RDS实例mysql。**。us- east-1.rds.amazonaws.com。他们俩都在美国东部地区。我将EC2实例的安全组(sg- **)添加到RDS安全组,但这无济于事。这似乎是防火墙/ DNS问题,因为运行此命令时超时: 错误2003(HY000):无法连接到“ mysql。**。us- east-1.rds.amazona
从EC2实例i-78a8df00中,我正在尝试连接到RDS实例他们都在美国东部地区。我将EC2实例的安全组(SG-*********)添加到RDS安全组中,但这没有帮助。运行此命令时超时,可能是防火墙/DNS问题: 错误2003(HY000):无法连接到“ 我可以从我的本地机器使用与上面相同的线路连接到RDS实例。我尝试了各种论坛解决方案,但那些都没有帮助。