Cannot connect to MySql in Docker. Access Denied Error thrown. Flask-SqlAlchemy
<p>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 <code>Access denied...</code> error shown below.</p>

<pre><code>$ docker-compose up --build -d
$ docker exec -it flaskdocker_mysql_1 mysql -u root -p
mysql&gt; CREATE DATABASE flask_docker;
mysql&gt; CREATE USER `flask-docker`@`localhost` IDENTIFIED BY 'pass';
mysql&gt; GRANT ALL PRIVILEGES ON flask_docker.* TO 'flask-docker'@'localhost';
mysql&gt; 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)")
</code></pre>

<p>Here is the repo <a href="https://github.com/Amertz08/flask-docker" rel="nofollow">https://github.com/Amertz08/flask-docker</a></p>

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

<p>Maybe I am not understanding it right but I linked <code>mysql:mysql</code> in my <code>docker-compose.yml</code> file</p>

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

<p>Then in my config object in <code>config.py</code> I have the following...</p>

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

<p>Any help on this would be greatly appreciated.</p>

<p>It seems the problem there:</p>

<p><code>mysql&gt; GRANT ALL PRIVILEGES ON flask_docker.* TO 'flask-docker'@'localhost';</code>
You grant privileges for <strong>localhost</strong> user</p>

<p>But you use mysql as <code>flask-docker'@'flaskdocker_web_1.flaskdocker_default</code> user.</p>

<p>To solve this grant user privileges for flask-docker'@'<strong>flaskdocker_web_1.flaskdocker_default</strong> user</p>

python docker flask flask-sqlalchemy docker-compose