1

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!

7
  • is firewall enabled to allow tcp traffic from 30626 port Commented Apr 4, 2019 at 6:59
  • Thanks for your answer! How do I check the firewall? Is it on GCP side or on my local computer? (I am on Mac OS btw) Commented Apr 4, 2019 at 8:14
  • console.cloud.google.com/networking/firewalls/list Commented Apr 4, 2019 at 8:23
  • GCP screenshot Sorry for the dumb question, I am on GCP, and for the IP range, which one should I choose? External IP? First time I use this Commented Apr 4, 2019 at 9:17
  • 1
    stackoverflow.com/questions/21065922/… Commented Apr 4, 2019 at 9:21

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.