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

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

<p>I've tried also to connect throught MySQL, not working either</p>

<h2>Docker environment</h2>

<p>My Dockerfile:</p>

<pre><code>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"]
</code></pre>

<p>The <em>sql-scripts</em> folder content 2 files in it:</p>

<pre><code>CREATE USER 'newuser'@'%' IDENTIFIED BY 'newpassword';
GRANT ALL PRIVILEGES ON * to 'newuser'@'%';
</code></pre>

<p>and</p>

<pre><code>CREATE DATABASE test_db;
</code></pre>

<h2>Setting up GCP</h2>

<p>I launch the container with the following command:</p>

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

<p>on GCP, the container seems running properly:</p>

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

<h2>Connect with Python</h2>

<p>And the python file to connect to the MySQL:</p>

<pre><code>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()
</code></pre>

<h2>What I want to do</h2>

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

<p>Thanks for your help!</p>

<p>You have exposed port <strong>50050</strong> while MySQL server by default is listening port <strong>3306</strong></p>

<p><strong>Option I.</strong> Change default port in <code>my.cfg</code> and set <code>port=50050</code> </p>

<p><strong>Option II.</strong> Expose default MySQL port</p>

<p>Dockerfile:</p>

<pre><code>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"]
</code></pre>

<p>Start container:</p>

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