1

I am giving it a last try before I give up, I have googling everywhere but still havent found a solution to my error.

Just so clarify some things:

  • MySql server IS UP and running
  • I am running and PHP admin panel together with mysql in the same server(confirming once again that the mysql server works)
  • The connection to the server is TCP
  • When i run this python script locally on my windows machine with XAMPP, it works as it should.

When i create docker image and run it, i get the following error:

sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2002, "Can't con                    nect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

So here is my "main.py" file:

    from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_restful import Api, Resource
from flask_mysqldb import MySQL

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://myUser:myPassword@MyIpAdress/just_a_test'



db = SQLAlchemy(app)
ma = Marshmallow(app)
api = Api(app)




#------------------------------------------------------------------------------------------------------
class exercise(db.Model):
    exerciseId = db.Column(db.Integer(), primary_key=True)
    exerciseName = db.Column(db.String(50))
    repetitions = db.Column(db.Integer())
    timeTakenToCompleteRepetitions = db.Column(db.Integer())



class exerciseSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = exercise


exercise_schema = exerciseSchema()
exercises_schema = exerciseSchema(many=True)


class exerciseListResource(Resource):

    def get(self):
        exercise = exercise.query.all()
        return exercises_schema.dump(exercise)

    def post(self):
        new_exercise = exercise(
            exerciseId=request.json['exerciseId'],
            exerciseName=request.json['exerciseName'],
            repetitions=request.json['repetitions'],
            timeTakenToCompleteRepetitions=request.json['timeTakenToCompleteRepetitions']
        )
        db.session.add(new_exercise)
        db.session.commit()
        return_data = exercise_schema.dump(new_exercise)
        return return_data, 201



class exerciseResource(Resource):
    def get(self, exercise_exerciseId):
        exercise = exercise.query.get_or_404(exercise_exerciseId)
        return exercise_schema.dump(exercise)

    def patch(self, exercise_exerciseId):
        exercise = exercise.query.get_or_404(exercise_exerciseId)

        if request.json.get('exerciseName'):
            exercise.exerciseName = request.json['exerciseName']
        if request.json.get('repetitions'):
            exercise.repetitions = request.json['repetitions']
        if request.json.get('timeTakenToCompleteRepetitions'):
            exercise.timeTakenToCompleteRepetitions = request.json['timeTakenToCompleteRepetitions']

        db.session.commit()
        return exercise_schema.dump(exercise)

    def delete(self, exercise_exerciseId):
        exercise = exercise.query.get_or_404(exercise_exerciseId)
        db.session.delete(exercise)
        db.session.commit()
        return '', 204


api.add_resource(exerciseListResource, '/exercise')
api.add_resource(exerciseResource, '/exercise/<int:exercise_exerciseId>')
#---------------------------------------------------------------------------------------------------



if __name__ == '__main__':
    app.run(debug=True)

What could the problem be?

1 Answer 1

1

Need to expose the Mysql port via Docker's -p option like so:

docker run -p 3306:3306 --name mysql-server -e MYSQL_ROOT_PASSWORD=password -d mysql:latest

Also, if connecting to local, use 127.0.0.1 instead of localhost so the connection is made via tcp instead of local socket file.

Sign up to request clarification or add additional context in comments.

4 Comments

so i should add your line in addition to this: -docker run -d -p 5000:8000 --restart always --name my_first_docker_running the_docker_project ? And yes, I also tried with 127.0.0.1 instead of localhost but that didnt work either.
Anyways, if i type this: docker run -p 3306:3306 --name mysql-server -e MYSQL_ROOT_PASSWORD=password -d mysql:latest I get error message: docker: Error response from daemon: Conflict. The container name "/mysql-server" is already in use by container "1870f47bdccdaa67ae2858e3181b45ed0071edc979ee11caeb378a113006bfbc". You have to remove (or rename) that container to be able to reuse that name. See 'docker run --help'.
You'll need to remove the old container named mysql-server if you use the same name. Where are you running the Python code and MySQL?
Both the python code and the MySQL are running on the same server.

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.