Python Can';t连接到托管在GCP Kubernetes引擎上的MySQL Docker

Python Can';t连接到托管在GCP Kubernetes引擎上的MySQL Docker,python,mysql,docker,kubernetes,google-cloud-platform,Python,Mysql,Docker,Kubernetes,Google Cloud Platform,我想通过Python连接到托管在GCP Kubernetes上的MySQL docker以编辑数据库。 我遇到了错误: 2003, "Can't connect to MySQL server on '35.200.250.69' ([Errno 61] Connection refused)" 我也尝试过通过MySQL连接,但也不起作用 Docker环境 我的Dockerfile: FROM mysql:latest ENV MYSQL_ROOT_PASSWORD password #

我想通过Python连接到托管在GCP Kubernetes上的MySQL docker以编辑数据库。 我遇到了错误:

2003, "Can't connect to MySQL server on '35.200.250.69' ([Errno 61] Connection refused)"
我也尝试过通过MySQL连接,但也不起作用

Docker环境 我的Dockerfile:

FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD password


# Derived from official mysql image (our base image)
FROM mysql
# Add a database
ENV MYSQL_DATABASE test-db
ENV MYSQL_USER=dbuser
ENV MYSQL_PASSWORD=dbpassword

# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/
EXPOSE 50050
CMD echo "This is a test." | wc -
CMD ["mysqld"]
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD password


# Derived from official mysql image (our base image)
FROM mysql
# Add a database
ENV MYSQL_DATABASE test-db
ENV MYSQL_USER=dbuser
ENV MYSQL_PASSWORD=dbpassword

# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/
EXPOSE 3306
CMD echo "This is a test." | wc -
CMD ["mysqld"]
sql脚本文件夹中包含2个内容文件:

CREATE USER 'newuser'@'%' IDENTIFIED BY 'newpassword';
GRANT ALL PRIVILEGES ON * to 'newuser'@'%';

建立GCP 我使用以下命令启动容器:

kubectl run test-mysql --image=gcr.io/data-sandbox-196216/test-mysql:latest --port=50050 --env="MYSQL_ROOT_PASSWORD=root_password"
在GCP上,容器似乎运行正常:

NAME         TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)           AGE
test-mysql   LoadBalancer   10.19.249.10   35.200.250.69   50050:30626/TCP   2m
与Python连接 以及连接到MySQL的python文件:

import sqlalchemy as db

# specify database configurations
config = {
    'host': '35.200.250.69',
    'port': 50050,
    'user': 'root',
    'password': 'root_password',
    'database': 'test_db'
}
db_user = config.get('user')
db_pwd = config.get('password')
db_host = config.get('host')
db_port = config.get('port')
db_name = config.get('database')
# specify connection string
connection_str = f'mysql+pymysql://{db_user}:{db_pwd}@{db_host}:{db_port}/{db_name}'
# connect to database
engine = db.create_engine(connection_str)
connection = engine.connect()
我想做什么 我希望能够用Python编写这个MySQL数据库,并在PowerBI上阅读它


谢谢你的帮助

您已经公开了端口50050,而MySQL服务器默认为侦听端口3306

选项I.更改
my.cfg
中的默认端口,并设置
port=50050

选项二。公开默认MySQL端口

Dockerfile:

FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD password


# Derived from official mysql image (our base image)
FROM mysql
# Add a database
ENV MYSQL_DATABASE test-db
ENV MYSQL_USER=dbuser
ENV MYSQL_PASSWORD=dbpassword

# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/
EXPOSE 50050
CMD echo "This is a test." | wc -
CMD ["mysqld"]
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD password


# Derived from official mysql image (our base image)
FROM mysql
# Add a database
ENV MYSQL_DATABASE test-db
ENV MYSQL_USER=dbuser
ENV MYSQL_PASSWORD=dbpassword

# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/
EXPOSE 3306
CMD echo "This is a test." | wc -
CMD ["mysqld"]
启动容器:

kubectl run test-mysql --image=gcr.io/data-sandbox-196216/test-mysql:latest --port=3306 --env="MYSQL_ROOT_PASSWORD=root_password"

是否已启用防火墙以允许来自30626端口的tcp流量谢谢您的回答!如何检查防火墙?它是在GCP端还是在我的本地计算机上?(我在Mac OS btw上)很抱歉这个愚蠢的问题,我在GCP上,对于IP范围,我应该选择哪一个?外部IP?我第一次用这个