我正在尝试将我的 Flask 应用程序与 MySQL 数据库连接。这是 MySQL 数据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)这是init.pyfrom flask import Flaskfrom config import Configfrom flask_sqlalchemy import SQLAlchemyfrom flask_migrate import Migrateapp = Flask(__name__)app.config.from_object(Config)db = SQLAlchemy(app)migrate = Migrate(app, db)from app import routes, models这是config.pyimport osbasedir = 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://root@127.0.0.1:3360/alchemy' SQLALCHEMY_TRACK_MODIFICATIONS = False和 models.pyfrom app import dbclass 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)当我尝试初始化数据库时flask db initflask db upgrade我收到sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:mysql我尝试使用 pymysql SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://rootl@127.0.0.1:3360/alchemy'如何将mysql与sqlalchemy连接?
1 回答
aluckdog
TA贡献1847条经验 获得超7个赞
连接到 mysql 数据库的正确原因是
mysql://username:password@server/db
先试试这个
SQLALCHEMY_DATABASE_URI = 'mysql://root:@127.0.0.1/alchemy'
如果它不起作用尝试这个
SQLALCHEMY_DATABASE_URI = 'mysql://root:@127.0.0.1:3306/alchemy'
如果它不起作用尝试这个
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@127.0.0.1:3306/alchemy
我很抱歉提出了很多选择,但我希望其中之一有效:)
如果都不起作用,那么问题不是与数据库的连接
添加回答
举报
0/150
提交
取消