from sqlalchemy import create_engine, Table, MetaData, Column, BIGINT, String, Integer, FLOAT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
import pymysql
pymysql.install_as_MySQLdb()
engine = create_engine("mysql://fil:fil@db:3307/doc", encoding="utf8")
Base = declarative_base()
Session = sessionmaker(bind=engine)
class Doc(Base):
__tablename__ = 'Docker'
def __init__(self, text):
self.text = text
id = Column(BIGINT, primary_key=True, autoincrement=True)
text = Column(String(100))
Base.metadata.create_all(engine)
session = Session()
session.add(Doc('text'))
session.commit()
huston@huston:~/PycharmProjects/docker$ docker-compose up -d
Creating network "docker_default" with the default driver
Creating volume "docker_db_data" with default driver
Creating docker_db_1 ... done
Creating docker_python-test_1 ... done
huston@huston:~/PycharmProjects/docker$ docker-compose ps -a
Name Command State Ports
--------------------------------------------------------------------------------------------------
docker_db_1 docker-entrypoint.sh --def ... Up 0.0.0.0:3307->3306/tcp, 33060/tcp
docker_python-test_1 python main.py Exit 1
huston@huston:~/PycharmProjects/docker$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
babc7a3ac663 python:3 "python main.py" 35 seconds ago Exited (1) 34 seconds ago docker_python-test_1
4db8ecdee029 mysql:latest "docker-entrypoint.s…" 36 seconds ago Up 35 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp docker_db_1
huston@huston:~/PycharmProjects/docker$ docker logs babc7a3ac663
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/pymysql/connections.py", line 613, in connect
sock = socket.create_connection(
File "/usr/local/lib/python3.9/socket.py", line 843, in create_connection
raise err
File "/usr/local/lib/python3.9/socket.py", line 831, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2336, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 364, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 495, in checkout
rec = pool._do_get()
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get
self._dec_overflow()
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.raise_(
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
raise exception
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get
return self._create_connection()
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 440, in __init__
self.__connect(first_connect_check=True)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.raise_(
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
raise exception
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 509, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python3.9/site-packages/pymysql/connections.py", line 353, in __init__
self.connect()
File "/usr/local/lib/python3.9/site-packages/pymysql/connections.py", line 664, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/app/main.py", line 23, in <module>
Base.metadata.create_all(engine)
huston@huston:~/PycharmProjects/docker$ docker exec -it 30c6438df401 mysql -ufil -p -P3307
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
engine = create_engine("mysql://fil:fil@localhost:3307/doc", encoding="utf8")
переходим в папку проекта
source venv/bin/activate
python3 scrupt.py
Спасибо большое, очень помог!!!