Я пытаюсь использовать MySQL с Flask -SQLAlchemy. Когда я пытаюсь создать таблицу, она не создается.
Вниз мой код и возникает ошибка:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import pymysql
pymysql.install_as_MySQLdb()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:''@localhost/alchemy'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(80), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
Traceback (most recent call last):
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\pymysql\connections.py", line 581, in connect
sock = socket.create_connection(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 808, in create_connection
raise err
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Nie można nawiązać połączenia, ponieważ komputer docelowy aktywnie go odmawia
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\base.py", line 2285, in _wrap_pool_connect
return fn()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 773, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.raise_(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 657, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.raise_(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 652, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\default.py", line 488, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\pymysql\connections.py", line 325, in __init__
self.connect()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\pymysql\connections.py", line 630, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([WinError 10061] Nie można nawiązać połączenia, ponieważ komputer docelowy aktywnie go odmawia)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\flask_sqlalchemy\__init__.py", line 1033, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\flask_sqlalchemy\__init__.py", line 1025, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\sql\schema.py", line 4320, in create_all
bind._run_visitor(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\base.py", line 2057, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\base.py", line 2049, in _optional_conn_ctx_manager
with self._contextual_connect() as conn:
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\base.py", line 2251, in _contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\base.py", line 2288, in _wrap_pool_connect
Connection._handle_dbapi_exception_noconnection(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\base.py", line 1554, in _handle_dbapi_exception_noconnection
util.raise_(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\base.py", line 2285, in _wrap_pool_connect
return fn()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 773, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.raise_(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 657, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.raise_(
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\pool\base.py", line 652, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\sqlalchemy\engine\default.py", line 488, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\pymysql\connections.py", line 325, in __init__
self.connect()
File "C:\Users\Ivan\AppData\Local\Programs\Python\Python38-32\lib\pymysql\connections.py", line 630, in connect
raise exc
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([WinError 10061] Nie można nawiązać połączenia, ponieważ komputer docelowy aktywnie go odmawia)")
(Background on this error at: http://sqlalche.me/e/e3q8)