11
Июн
2021

db.create_all() – не выполняется SQL Ubuntu

Создал тестовую базу. Создал пользователя mysql со всеми правами для всех баз. Начал писать приложение, но не могу выполнить создание постов в этой базе данных, появляется странная ошибка. Перед самой ошибкой скажу такие факты: Приложение, для которой создается база - работает. Возможно проблема с версиями mysql и mysql-connector, однако устанавливал я всё последних версий. Недавно я уже делал это и такой проблемы не было, поэтому я и встал в тупик. Благодарен буду любой наводке.

Файл config.py

class Configuration(object):
    DEBUG = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_DATABASE_URI = 'mysql+mysqlconnector://admin:[email protected]/test1'

Файл app.py

from flask import Flask
from config import Configuration
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object(Configuration)

db = SQLAlchemy(app)

Файл models.py

from app import db
from datetime import datetime
import re

def slugify(s):
    pattern = r'[^\w+]'
    return re.sub(pattern, '-', s)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(140))
    slug = db.Column(db.String(140), unique=True)
    body = db.Column(db.Text)
    created = db.Column(db.DateTime, default=datetime.now())

    def __init__(self, *args, **kwargs):
        super(Post, self).__init__(*args, **kwargs)
        self.generate_slug()

    def generate_slug(self):
        if self.title:
            self.slug = slugify(self.title)

    def __repr__(self):
        return '<Post id: {}, title: {}>'.format(self.id, self.title)

Файл main.py

from app import app 
from app import db
from posts.blueprint import posts
import view

app.register_blueprint(posts, url_prefix='/blog')

if __name__ == '__main__':
    app.run()

Остальные вроде не так важны файлы, но буду готов докинуть.

Команды, которые выполняю.

  1. Захожу в виртуальную среду venv

  2. В ней запускаю python, далее команды

    import models

    from app import db

    db.create_all()

Но после крайней получаю ошибку длинной в жизнь:

Traceback (most recent call last):

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/network.py", line 426, in switch_to_ssl

    self.sock.do_handshake()

  File "/usr/lib/python3.9/ssl.py", line 1309, in do_handshake

    self._sslobj.do_handshake()

ssl.SSLError: [SSL] internal error (_ssl.c:1129)



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3211, in _wrap_pool_connect

    return fn()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 307, in connect

    return _ConnectionFairy._checkout(self)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout

    fairy = _ConnectionRecord.checkout(pool)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 425, in checkout

    rec = pool._do_get()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get

    self._dec_overflow()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__

    compat.raise_(

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_

    raise exception

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get

    return self._create_connection()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection

    return _ConnectionRecord(self)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 368, in __init__

    self.__connect()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 611, in __connect

    pool.logger.debug("Error on connect(): %s", e)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__

    compat.raise_(

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_

    raise exception

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 605, in __connect

    connection = pool._invoke_creator(self)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 578, in connect

    return dialect.connect(*cargs, **cparams)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 584, in connect

    return self.dbapi.connect(*cargs, **cparams)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/__init__.py", line 183, in connect

    return MySQLConnection(*args, **kwargs)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/connection.py", line 100, in __init__

    self.connect(**kwargs)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/abstracts.py", line 736, in connect

    self._open_connection()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/connection.py", line 244, in _open_connection

    self._do_auth(self._user, self._password,

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/connection.py", line 152, in _do_auth

    self._socket.switch_to_ssl(ssl_options.get('ca'),

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/network.py", line 431, in switch_to_ssl

    raise errors.InterfaceError(

mysql.connector.errors.InterfaceError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 1 [SSL] internal error (_ssl.c:1129)



The above exception was the direct cause of the following exception:



Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", line 1094, in create_all

    self._execute_for_all_tables(app, bind, 'create_all')

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", line 1086, in _execute_for_all_tables

    op(bind=self.get_engine(app, bind), **extra)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 4739, in create_all

    bind._run_ddl_visitor(

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3077, in _run_ddl_visitor

    with self.begin() as conn:

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2993, in begin

    conn = self.connect(close_with_result=close_with_result)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3165, in connect

    return self._connection_cls(self, close_with_result=close_with_result)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 96, in __init__

    else engine.raw_connection()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3244, in raw_connection

    return self._wrap_pool_connect(self.pool.connect, _connection)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3214, in _wrap_pool_connect

    Connection._handle_dbapi_exception_noconnection(

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2068, in _handle_dbapi_exception_noconnection

    util.raise_(

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_

    raise exception

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3211, in _wrap_pool_connect

    return fn()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 307, in connect

    return _ConnectionFairy._checkout(self)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout

    fairy = _ConnectionRecord.checkout(pool)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 425, in checkout

    rec = pool._do_get()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get

    self._dec_overflow()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__

    compat.raise_(

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_

    raise exception

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get

    return self._create_connection()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection

    return _ConnectionRecord(self)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 368, in __init__

    self.__connect()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 611, in __connect

    pool.logger.debug("Error on connect(): %s", e)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__

    compat.raise_(

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_

    raise exception

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 605, in __connect

    connection = pool._invoke_creator(self)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 578, in connect

    return dialect.connect(*cargs, **cparams)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 584, in connect

    return self.dbapi.connect(*cargs, **cparams)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/__init__.py", line 183, in connect

    return MySQLConnection(*args, **kwargs)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/connection.py", line 100, in __init__

    self.connect(**kwargs)

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/abstracts.py", line 736, in connect

    self._open_connection()

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/connection.py", line 244, in _open_connection

    self._do_auth(self._user, self._password,

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/connection.py", line 152, in _do_auth

    self._socket.switch_to_ssl(ssl_options.get('ca'),

  File "/home/roman/Desktop/Coursework/env/lib/python3.9/site-packages/mysql/connector/network.py", line 431, in switch_to_ssl

    raise errors.InterfaceError(

sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) 2055: Lost connection to MySQL server at 'localhost:3306', system error: 1 [SSL] internal error (_ssl.c:1129)

(Background on this error at: http://sqlalche.me/e/14/rvf5)

Источник: https://ru.stackoverflow.com/questions/1294264/db-create-all-%D0%BD%D0%B5-%D0%B2%D1%8B%D0%BF%D0%BE%D0%BB%D0%BD%D1%8F%D0%B5%D1%82%D1%81%D1%8F-sql-ubuntu

Тебе может это понравится...

Добавить комментарий