3

Alright so been trying for a while to get this to work. Here is the line of commands I run from start to finish and I get an Access denied... error shown below.

$ docker-compose up --build -d $ docker exec -it flaskdocker_mysql_1 mysql -u root -p mysql> CREATE DATABASE flask_docker; mysql> CREATE USER `flask-docker`@`localhost` IDENTIFIED BY 'pass'; mysql> GRANT ALL PRIVILEGES ON flask_docker.* TO 'flask-docker'@'localhost'; mysql> exit Bye $ docker exec -it flaskdocker_web_1 python /usr/src/app/manage.py createdb # rest of traceback sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, u"Access denied for user 'flask-docker'@'flaskdocker_web_1.flaskdocker_default' (using password: YES)") 

Here is the repo https://github.com/Amertz08/flask-docker

It looks like the SqlAlchemy engine is not looking at the right container. 'flask-docker'@'flaskdocker_web_1.flaskdocker_default' is what shows up in the error.

Maybe I am not understanding it right but I linked mysql:mysql in my docker-compose.yml file

web: restart: always build: ./app volumes: - /usr/src/app/static expose: - "5000" environment: FLASK_CONFIG: 'production' links: - mysql:mysql command: /usr/local/bin/uwsgi --ini uwsgi.ini 

Then in my config object in config.py I have the following...

MYSQL_USER = 'flask-docker' MYSQL_PASS = 'pass' MYSQL_HOST = 'mysql' MYSQL_DB = 'flask_docker' # Database info SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{usr}:{passwd}@{host}/{db}'.format( usr=MYSQL_USER, passwd=MYSQL_PASS, host=MYSQL_HOST, db=MYSQL_DB ) 

Any help on this would be greatly appreciated.

2
  • You've linked the container with the name mysql. You don't need to refer to it as flask_docker. Also, you can just say mysql instead of mysql:mysql. The latter form is only needed when you map a container under a different name (e.g., mysql:flask_docker). Commented Aug 26, 2016 at 18:22
  • valid point. Changed thank you! Commented Aug 26, 2016 at 20:36

2 Answers 2

3

It seems the problem there:

mysql> GRANT ALL PRIVILEGES ON flask_docker.* TO 'flask-docker'@'localhost'; You grant privileges for localhost user

But you use mysql as flask-docker'@'flaskdocker_web_1.flaskdocker_default user.

To solve this grant user privileges for flask-docker'@'flaskdocker_web_1.flaskdocker_default user

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

1 Comment

This appears to fix the issue. Thanks.
0

I'll add another solution here as I had a difficult time with this problem. In order for me to get access to the database from Flask I used mysql-connector (can be installed via pip install mysql-connector), entered the mysql shell and ran the following code ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';

I then connected to mysql using the following config:

from flask import Flask from flask_sqlalchemy import SQLAlchemy from .db import db app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root:[email protected]:3306/bar_db' db = SQLAlchemy(app) 

I'm still not quite sure why this work around was needed as I've never knowingly used it before in any other Python application however it does appear to work with libmysqlclient.

src: https://dev.mysql.com/doc/refman/8.0/en/native-pluggable-authentication.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.