无法将 flask sqlAlchemy 连接到 mysql 数据库

Can't connect flask sqlAlchemy to the mysql database

我正在尝试将我的 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.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

这里是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://root@127.0.0.1:3360/alchemy'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

和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)

当我尝试使用

初始化数据库时
flask db init
flask 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?

连接到 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

我很抱歉提供了很多选项,但我希望其中一个可行:)

如果 none 有效则问题不是与数据库的连接