kantancoding / microservices-python

256 stars 157 forks source link

Mysql cannot be connected using minikube's host.minikube.internal #16

Closed kshitizJ closed 1 year ago

kshitizJ commented 1 year ago

Error Image:

error

My server file image:

pythonfile

My auth-deployment file image:

auth-deployment-file

My auth-service file image:

auth-service

My auth-configmap file image:

auth-configmap

My Docker file for auth:

auth-docker-file

My all pods are running successfully:

pods running

I don't know what is the error. When id do systemctl status mysql it shows that my mysql is running successfully in my local device. My operating system is Ubuntu 22.04 LTS.

Please help me to resolve the issue.

kantancoding commented 1 year ago

My guess is that the mysql credentials aren't configured correctly.

kshitizJ commented 1 year ago

Thank you for replying.

My mysql credentials are correct. I am able to login using mysql credentials. mysql

When I print the env variable:

printing

When I check the logs of the pod, my env variables are getting printed:

inside log

kantancoding commented 1 year ago

I'm talking about the env vars that are being used by the application. can you check or show me the output of env | grep MYSQL in the container that is connecting to mysql?

kantancoding commented 1 year ago

Ok, so you are sure user root and password Kshitiz@123 are the correct credentials?

kantancoding commented 1 year ago

Can you show your init.sql

kshitizJ commented 1 year ago

My Env vars used by the application in the container that is connecting to mysql:

env vars

Ok, so you are sure user root and password Kshitiz@123 are the correct credentials?

Yes

My init.sql file:

sql file

kantancoding commented 1 year ago

seems you removed the line CREATE USER 'auth_user'@'localhost' IDENTIFIED BY 'Auth123';

You shouldn't remove this you should just replace the values with your creds. Actually can you drop the db and then create it again using the init.sql from this repo.. you can change the insert line but leave the password and user.. then use those creds in your secrets file and see if it will connect?

kshitizJ commented 1 year ago

Sure I will give this a try.

I have a doubt. This new user which we are creating, is being created in our local mysql right? My local mysql already have a user name - 'root' and password - 'Kshitiz@123' then does it make any difference if we use the existing user i.e 'root' than creating a new user?

kantancoding commented 1 year ago

I have no way of knowing how your user is configured and what dbs it has access to. I do know that in that init.sql I explicitly give access to the user being created in the script. So if we can't connect even when using that user, we know that the problem is something else. Basically, the fastest way to figure out the problem is through process of elimination.

kantancoding commented 1 year ago

You also removed the line GRANT ALL PRIVILEGES ON auth.* TO 'auth_user'@'localhost';

These are important

kshitizJ commented 1 year ago

I have no way of knowing how your user is configured and what dbs it has access to. I do know that in that init.sql I explicitly give access to the user being created in the script. So if we can't connect even when using that user, we know that the problem is something else. Basically, the fastest way to figure out the problem is through process of elimination.

Ok, Thank you. I will create a new user and give you an update.

kshitizJ commented 1 year ago

Update:

I am getting an error

Init.sql file: sql file

So I added '@' in the password and the user got created.

Still i get the same error: error 2

kshitizJ commented 1 year ago

I use this command to run the application:

command

My host file:

host file

kantancoding commented 1 year ago

can you docker exec into the container and try to manually connect to mysql from within the container's shell?

kshitizJ commented 1 year ago

Will you please assist me on this using google meet as I am new to docker and kubernetes.

I can share you the meeting link.

kantancoding commented 1 year ago

for example sudo docker exec –it container-name /bin/bash

kantancoding commented 1 year ago

that command will get you into the container. Then once in the container try to connect using your usual mysql command that you use on your local but this time use the host host.minikube.internal

kshitizJ commented 1 year ago

Do I need to do this eval $(minikube docker-env) before getting into the container?

kantancoding commented 1 year ago

huh why?

kantancoding commented 1 year ago

Nope just do what I mentioned above. Also I can't get on a call. It's late for me and I have work tomorrow

kantancoding commented 1 year ago

also, you will need to install a mysql client in the container

kantancoding commented 1 year ago

I will check on this tomorrow. Let me know the results.

kshitizJ commented 1 year ago

Ok I will do as you said and I will update you.

How to install mysql client inside the container?

kantancoding commented 1 year ago

I think its apt-get install mysql-client. This is the underlying os of the container. https://github.com/docker-library/python/blob/7ee840478eed1fd8d36dbe9ab7bd4706085468e5/3.12-rc/slim-bullseye/Dockerfile

You can see how they are installing things in the script

kshitizJ commented 1 year ago

Ok got it. I will update you by tomorrow. Thank you so much for the help.

kshitizJ commented 1 year ago

can you docker exec into the container and try to manually connect to mysql from within the container's shell?

From inside the container I got this error:

inside container

kantancoding commented 1 year ago

Are you sure you have mysql server installed on your local? What os are you using?

kshitizJ commented 1 year ago

Yes, I am sure.

OS: Ubuntu 22 LTS

Mysql version: mysql-version

kantancoding commented 1 year ago

run this and show me the output please sudo apt-get install mysql-server

kshitizJ commented 1 year ago

on my local machine?

kantancoding commented 1 year ago

yes

kshitizJ commented 1 year ago

This is the output:

Screenshot from 2022-11-27 23-47-33

kshitizJ commented 1 year ago

Hello sir, will you please suggest me next steps so that I can complete this tutorial. Your teaching is very good. I got to know each and every concepts clearly. I just got stuck because of mysql.

This is my minikube's host file:

minikube-host-file

donwany commented 1 year ago

did you change the port from "3306" to '3306' in the yaml file?

donwany commented 1 year ago

If its still not working just connect to a remote mysql database either on aws or azure. That should work fine

donwany commented 1 year ago

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

kubectl port-forward auth-67f87b9dc6-s7jbg 5000:5000 &

curl -X POST 'http://127.0.0.1:5000/login' -u "trump@gmail.com:admin123"

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRoZW9kb25kcmVAZ21haWwuY29tIiwiZXhwIjoxNjY5ODUxOTY2LCJpYXQiOjE2Njk3NjU1NjYsImFkbWluIjp0cnVlfQ.pOSQEACZrnni4NVP9WDr6pO1o6iK0ZWRPPYQWPdsQiI%

donwany commented 1 year ago

Screenshot 2022-11-29 at 5 52 11 PM

Screenshot 2022-11-29 at 5 52 20 PM

kantancoding commented 1 year ago

You can also try to spin up a mysql docker container and just connect to that although the data will be wiped once you stop the container. But still you will be able to finish the tutorial. I suspect there is some issue with your local mysql setup but I really have no way of knowing.

kshitizJ commented 1 year ago

did you change the port from "3306" to '3306' in the yaml file?

No luck on this.

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

kubectl port-forward auth-67f87b9dc6-s7jbg 5000:5000 &

curl -X POST 'http://127.0.0.1:5000/login' -u "trump@gmail.com:admin123"

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRoZW9kb25kcmVAZ21haWwuY29tIiwiZXhwIjoxNjY5ODUxOTY2LCJpYXQiOjE2Njk3NjU1NjYsImFkbWluIjp0cnVlfQ.pOSQEACZrnni4NVP9WDr6pO1o6iK0ZWRPPYQWPdsQiI%

No luck on this as well.

I got this while hitting the URL through postman:

postman error

Error:

MySQLdb.OperationalError: (2002, "Can't connect to MySQL server on 'host.minikube.internal' (115)")

I think my pods inside the minikube node doesnot have the access of host.minikube.internal.

Any idea on this?

kshitizJ commented 1 year ago

Also when I do minikube tunnel, I get stuck on this:

Screenshot from 2022-12-01 00-51-32

kantancoding commented 1 year ago

Let's focus on one issue at a time. If you are having issues with minikube tunnel, it's better to create another issue.

I think my pods inside the minikube node doesnot have the access of host.minikube.internal

For this, please run the following commands and show me the output:

minikube ssh

nc -vz host.minikube.internal 3306

kshitizJ commented 1 year ago

Let's focus on one issue at a time. If you are having issues with minikube tunnel, it's better to create another issue.

I think my pods inside the minikube node doesnot have the access of host.minikube.internal

For this, please run the following commands and show me the output:

minikube ssh

nc -vz host.minikube.internal 3306

output:

Screenshot from 2022-12-01 21-29-38

kantancoding commented 1 year ago

can you show me the output of ping host.minikube.internals as well... while in minikube ssh

kantancoding commented 1 year ago

Looking at this, it's possible there was a degradation introduced into the minikube code? I'm not sure I didn't read through it entirely, but what minikube version are you using?

minikube version

Might be worth it to downgrade to the version I used in the tutorial to see if that fixes the issue. In the tutorial I use v1.25.0 Let me know how it goes

kshitizJ commented 1 year ago

can you show me the output of ping host.minikube.internals as well... while in minikube ssh

host.minikube.internals didn't work so I used host.minikube.internal:

Screenshot from 2022-12-01 22-33-04

minikube version: Screenshot from 2022-12-01 22-35-38

kshitizJ commented 1 year ago

Looking at this, it's possible there was a degradation introduced into the minikube code? I'm not sure I didn't read through it entirely, but what minikube version are you using?

minikube version

Might be worth it to downgrade to the version I used in the tutorial to see if that fixes the issue. In the tutorial I use v1.25.0 Let me know how it goes

I read some the documentation where it says that if you are using minikube version greater than 1.10 than you can use host.minikube.internal to access the service on your local machine.

kantancoding commented 1 year ago

Yeah I know that but look at the thread I linked. People with minikube v1.28.x seem to be experiencing the issue you are experiencing. So I'm guessing a bug was introduced at some point between v1.25 and v1.28.. So that's why I said you should try downgrading minikube to the version I used in the tutorial which is v1.25.0

kshitizJ commented 1 year ago

Ok, I will downgrade and let you know.

Thank you!

kshitizJ commented 1 year ago

Yeah I know that but look at the thread I linked. People with minikube v1.28.x seem to be experiencing the issue you are experiencing. So I'm guessing a bug was introduced at some point between v1.25 and v1.28.. So that's why I said you should try downgrading minikube to the version I used in the tutorial which is v1.25.0

I downgraded the minikube version and the problem still exists.

It says that MySQLdb.OperationalError: (2002, "Can't connect to MySQL server on 'host.minikube.internal' (115)")

Screenshot from 2022-12-02 11-49-16

kantancoding commented 1 year ago

Ok, so at this point I can't tell if this is an issue with your mysql or what so we are just going to have to do this a different way. Please do the following:

  1. Create a directory structure in thesrc dir that looks like this: Screen Shot 2022-12-02 at 16 39 55

  2. In the pod.yaml file insert this:

    apiVersion: v1
    kind: Pod
    metadata:
    name: mysql
    spec:
    containers:
    - name: mysql
    image: mysql        
    ports:
      - containerPort: 3306
    volumeMounts:
      - name: mysql-initdb
        mountPath: /docker-entrypoint-initdb.d
    volumes:
    - name: mysql-initdb
      configMap:
        name: mysql-initdb-config
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: mysql-initdb-config
    data:
    initdb.sql: |
    CREATE USER 'auth_user'@'localhost' IDENTIFIED BY 'Aauth123';
    CREATE DATABASE auth;
    GRANT ALL PRIVILEGES ON auth.* TO 'auth_user'@'localhost';
    USE auth;
    CREATE TABLE user (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL);
    INSERT INTO user (email, password) VALUES ('georgio@email.com', 'Admin123');
  3. In auth/manifests/configmap.yaml replace the contents with this:

    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: auth-configmap
    data:
    MYSQL_HOST: mysql
    MYSQL_USER: auth_user
    MYSQL_DB: auth
    MYSQL_PORT: "3306"
  4. kubectl delete EVERYTHING...

  5. kubectl apply EVERYTHING