Reputation: 61
I'm trying to connect my flask app with the MySQL database. here's MySQL data
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| alchemy |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.01 sec)
here's init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models
here's config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or 'test-key-motherfucker'
SQLALCHEMY_DATABASE_URI = 'mysql://[email protected]:3360/alchemy'
SQLALCHEMY_TRACK_MODIFICATIONS = False
and models.py
from app import db
class Test(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), index=True, unique=True)
def __repr__(self):
return '<Test {}>'.format(self.name)
when I try to init db with
flask db init
flask db upgrade
I receive
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:mysql
I tried to use pymysql SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://[email protected]:3360/alchemy'
how to connect mysql with sqlalchemy?
Upvotes: 2
Views: 5509
Reputation: 101
the right why to connect to mysql database is
mysql://username:password@server/db
try this first
SQLALCHEMY_DATABASE_URI = 'mysql://root:@127.0.0.1/alchemy'
if it did not work try this
SQLALCHEMY_DATABASE_URI = 'mysql://root:@127.0.0.1:3306/alchemy'
if it did not work try this
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@127.0.0.1:3306/alchemy
i am sorry for putting many options but i hope one of them work :)
if none worked then the problem not the connection with the database
Upvotes: 6