kantancoding / microservices-python

256 stars 157 forks source link

Cast MySQL port to integer #4

Closed blakejoy closed 1 year ago

blakejoy commented 1 year ago

It may be a Python 3.11 thing ... but it looks like an error gets thrown which in turns returns a 500 internal server error when calling http://mp3converter.com/login .... I'm assuming its the port here https://github.com/selikapro/microservices-python/blob/4382751315adadbeabd32a69ba9eb8472f37683c/src/auth/server.py#L13. Casting it as an int should solve your problem.

server.config["MYSQL_PORT"] = int(os.environ.get("MYSQL_PORT"))

Auth pod logs:

image
blakejoy commented 1 year ago

If this doesn't make sense to anyone, we can sync up and debug.

berain-uvic commented 1 year ago

I tried that but now I can't connect to the database:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/app/server.py", line 24, in login
    cur = mysql.connection.cursor()
  File "/usr/local/lib/python3.10/site-packages/flask_mysqldb/__init__.py", line 101, in connection
    ctx.mysql_db = self.connect
  File "/usr/local/lib/python3.10/site-packages/flask_mysqldb/__init__.py", line 88, in connect
    return MySQLdb.connect(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/__init__.py", line 123, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
MySQLdb.OperationalError: (2002, "Can't connect to MySQL server on 'host.minikube.internal' (115)")
172.17.0.1 - - [21/Nov/2022 02:51:16] "POST /login HTTP/1.1" 500 -
blakejoy commented 1 year ago

Can you verify that mysql is running in the Docker container?

berain-uvic commented 1 year ago

I will look into it! Also, I am using WSL Ubuntu 20.04

donwany commented 1 year ago

Should the mysql be running outside the container or inside the container? I have mine running on my local computer (Mac)

blakejoy commented 1 year ago

It runs in the docker container iirc

donwany commented 1 year ago

It runs in the docker container iirc

I think I fixed it using single quotes '3306' instead of "3306", and then cast it this way: int(os.environ.get("MYSQL_PORT"))

apiVersion: v1 kind: ConfigMap metadata: name: auth-configmap data: MYSQL_HOST: host.minikube.internal MYSQL_USER: root MYSQL_DB: auth MYSQL_PORT: '3306'

berain-uvic commented 1 year ago

Ok, I will give that a try!

blakejoy commented 1 year ago

It runs in the docker container iirc

I think I fixed it using single quotes '3306' instead of "3306", and then cast it this way: int(os.environ.get("MYSQL_PORT"))

apiVersion: v1 kind: ConfigMap metadata: name: auth-configmap data: MYSQL_HOST: host.minikube.internal MYSQL_USER: root MYSQL_DB: auth MYSQL_PORT: '3306'

Single and double quotes are the same in Python so it was probably the type casting

donwany commented 1 year ago

You are right but in this case the double quote didn't work for me, single worked.

berain-uvic commented 1 year ago

It runs in the docker container iirc

I think I fixed it using single quotes '3306' instead of "3306", and then cast it this way: int(os.environ.get("MYSQL_PORT"))

apiVersion: v1 kind: ConfigMap metadata: name: auth-configmap data: MYSQL_HOST: host.minikube.internal MYSQL_USER: root MYSQL_DB: auth MYSQL_PORT: '3306'

Didn't work for me. I wonder if it has something to do with mysql running locally in WSL...

donwany commented 1 year ago

MySQL is running locally

berain-uvic commented 1 year ago

Also, it should work if we omit the db port number, as 3306 is the default.

berain-uvic commented 1 year ago

Oh well, it was a great tutorial otherwise :) I learned a lot about Kubernetes!

donwany commented 1 year ago

well if working it can be closed