首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法连接到托管在引擎上的MySQL码头

无法连接到托管在引擎上的MySQL码头
EN

Stack Overflow用户
提问于 2019-04-04 06:39:08
回答 1查看 522关注 0票数 1

我想通过Python连接到上托管的MySQL码头来编辑数据库。我遇到了错误:

代码语言:javascript
复制
2003, "Can't connect to MySQL server on '35.200.250.69' ([Errno 61] Connection refused)"

我也试过通过MySQL连接,也没有工作

码头环境

我的Dockerfile:

代码语言:javascript
复制
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"]

其中的sql-脚本文件夹content 2文件:

代码语言:javascript
复制
CREATE USER 'newuser'@'%' IDENTIFIED BY 'newpassword';
GRANT ALL PRIVILEGES ON * to 'newuser'@'%';

代码语言:javascript
复制
CREATE DATABASE test_db;

建立GCP

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

代码语言:javascript
复制
kubectl run test-mysql --image=gcr.io/data-sandbox-196216/test-mysql:latest --port=50050 --env="MYSQL_ROOT_PASSWORD=root_password"

在GCP上,容器似乎运行正常:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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上读取它。

谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-04 10:17:00

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

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

选项II.公开默认的MySQL端口

Dockerfile:

代码语言:javascript
复制
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"]

启动容器:

代码语言:javascript
复制
kubectl run test-mysql --image=gcr.io/data-sandbox-196216/test-mysql:latest --port=3306 --env="MYSQL_ROOT_PASSWORD=root_password"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55509333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档