简体   繁体   中英

Can't connect to MySQL Docker hosted on GCP Kubernetes Engine

I want to connect to MySQL docker hosted on GCP Kubernetes through Python to edit a database. I encounter the error:

2003, "Can't connect to MySQL server on '35.200.250.69' ([Errno 61] Connection refused)"

I've tried also to connect throught MySQL, not working either

Docker environment

My 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"]

The sql-scripts folder content 2 files in it:

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

and

CREATE DATABASE test_db;

Setting up GCP

I launch the container with the following command:

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

on GCP, the container seems running properly:

NAME         TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)           AGE
test-mysql   LoadBalancer   10.19.249.10   35.200.250.69   50050:30626/TCP   2m

Connect with Python

And the python file to connect to the MySQL:

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()

What I want to do

I would like to be able to write this MySQL database with Python, and read it on PowerBI.

Thanks for your help!

You have exposed port 50050 while MySQL server by default is listening port 3306

Option I. Change default port in my.cfg and set port=50050

Option II. Expose default MySQL port

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 3306
CMD echo "This is a test." | wc -
CMD ["mysqld"]

Start container:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM