5

I want to connect to online MySQL DB to the flask app I made, so I can get rid of localhost stuff.

I have this code

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import os
import uuid
from werkzeug.security import generate_password_hash , check_password_hash

app = Flask(__name__)

app.config['SECRET_KEY'] = 'thisissecret'
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['sqlite:////Users/confuapplication/PycharmProjects/untitled/.db']
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://saifaliunity:123123123!@saifaliunity.mysql.pythonanywhere-services.com/saifaliunity$todo';
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']

db = SQLAlchemy(app)

So well this is what localhost environment is, I have setup MySQL and python app online and want to set dbname, host, username, password to the flask app. But don't know how to do this.

After using the answer below this is the error log.

2019-02-26 10:16:54,896: Error running WSGI application
2019-02-26 10:16:54,900: ModuleNotFoundError: No module named 'flask_app'
2019-02-26 10:16:54,900:   File "/var/www/saifaliunity_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-02-26 10:16:54,900:     from flask_app import app as application  # noqa
2019-02-26 10:16:54,901: ***************************************************
2019-02-26 10:16:54,901: If you're seeing an import error and don't know why,
2019-02-26 10:16:54,901: we have a dedicated help page to help you debug: 
2019-02-26 10:16:54,901: https://help.pythonanywhere.com/pages/DebuggingImportError/
2019-02-26 10:16:54,902: ***************************************************
2019-02-26 12:58:20,238: Error running WSGI application
2019-02-26 12:58:20,278: ModuleNotFoundError: No module named 'flask_app'
2019-02-26 12:58:20,279:   File "/var/www/saifaliunity_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-02-26 12:58:20,279:     from flask_app import app as application  # noqa
2019-02-26 12:58:20,279: ***************************************************
2019-02-26 12:58:20,280: If you're seeing an import error and don't know why,
2019-02-26 12:58:20,280: we have a dedicated help page to help you debug: 
2019-02-26 12:58:20,280: https://help.pythonanywhere.com/pages/DebuggingImportError/
2019-02-26 12:58:20,280: ***************************************************
2019-02-26 12:58:21,842: Error running WSGI application
2019-02-26 12:58:21,843: ModuleNotFoundError: No module named 'flask_app'
2019-02-26 12:58:21,843:   File "/var/www/saifaliunity_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-02-26 12:58:21,843:     from flask_app import app as application  # noqa
2019-02-26 12:58:21,844: ***************************************************
2019-02-26 12:58:21,844: If you're seeing an import error and don't know why,
2019-02-26 12:58:21,844: we have a dedicated help page to help you debug: 
2019-02-26 12:58:21,844: https://help.pythonanywhere.com/pages/DebuggingImportError/
2019-02-26 12:58:21,844: ***************************************************

1 Answer 1

5

The SQLALCHEMY_DATABASE_URI should be mysql://username:password@server/db, replace username, password, server and db with your values.

See the documentation for more details: http://flask-sqlalchemy.pocoo.org/2.3/config/

6
  • So it should be in this function? os.path.expanduser() ? or passed without it?
    – Saif Ali
    Feb 26, 2019 at 10:09
  • 1
    You don't need expanduser() it is used to replace "~" with the fullpath of the user home directory.
    – luxcem
    Feb 26, 2019 at 10:11
  • but still its not connecting
    – Saif Ali
    Feb 26, 2019 at 10:41
  • @Debendra i have updated the question can you now help?
    – Saif Ali
    Feb 26, 2019 at 13:00
  • 1
    Activate your virtual environment first. Its not activated.
    – Debendra
    Feb 26, 2019 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.