当我尝试使用 python 连接到我的 MySQL 数据库时,由于未知原因显示错误:
dTraceback (most recent call last):
File "/usr/local/bin/flask", line 8, in <module>
sys.exit(main())
^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/cli.py", line 1105, in main
cli.main()
File "/usr/local/lib/python3.12/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/click/decorators.py", line 92, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/cli.py", line 953, in run_command
raise e from None
File "/usr/local/lib/python3.12/site-packages/flask/cli.py", line 937, in run_command
app: WSGIApplication = info.load_app()
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/cli.py", line 335, in load_app
app = locate_app(import_name, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/cli.py", line 245, in locate_app
__import__(module_name)
File "/app/main.py", line 2, in <module>
import sql_code
File "/app/sql_code.py", line 4, in <module>
mydb = mysql.connector.connect(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/mysql/connector/pooling.py", line 322, in connect
return CMySQLConnection(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/mysql/connector/connection_cext.py", line 151, in __init__
self.connect(**kwargs)
File "/usr/local/lib/python3.12/site-packages/mysql/connector/abstracts.py", line 1399, in connect
self._open_connection()
File "/usr/local/lib/python3.12/site-packages/mysql/connector/connection_cext.py", line 339, in _open_connection
raise get_mysql_exception(
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (99)
这些是 python 连接器的文件(针对敏感信息的审查进行了修改)
mydb = mysql.connector.connect(
host="localhost",
user="USERNAME_DATABASE",
password="root_pass",
database="mydatabase"
)
以及修改后的MYSQL文件(docker):
FROM mysql:latest
WORKDIR /app
ADD . /app
COPY ./ex_db.sql /docker-entyypoint-initdb.d/
ENV MYSQL_HOST=localhost
ENV MYSQL_USER=USERNAME_DATABASE
ENV MYSQL_ROOT_PASSWORD=root_pass
ENV MYSQL_PASSWORD=pass
ENV MYSQL_DATABASE=mydatabase
EXPOSE 3306
两者都在最新版本的 docker 中运行
上面的代码应该可以正确连接
更新:修复了它,必须在 python 代码中将
host=''
设置为 mysql 服务名称,而不是 localhost 或 ip 地址。感谢那些试图帮助我的人。