python - 使用 sqlalchemy 读取 sql 数据库:OperationalError [Errno 111] Connection denied
问题描述
这工作正常
from sqlalchemy import create_engine
import pandas as pd
db_connection_str = 'mysql+pymysql://User:PW@localhost/DBName'
db_connection = create_engine(db_connection_str)
df = pd.read_sql('SELECT * FROM tablename', con=db_connection)
如果我localhost
用IP替换它不会
from sqlalchemy import create_engine
import pandas as pd
db_connection_str = 'mysql+pymysql://User:PW@192.168.0.7/DBName'
db_connection = create_engine(db_connection_str)
df = pd.read_sql('SELECT * FROM tablename', con=db_connection)
OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '192.168.0.7' ([Errno 111] Connection refused)") (此错误的背景:http ://sqlalche.me /e/13/e3q8 )
但是有必要用IP替换localhost,因为我想从客户端执行脚本。
编辑:在我/etc/mysql/my.cnf
那里没有bind-adddress:
- 所以我添加了最后一行:
# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
bind-address = 0.0.0.0
并执行sudo systemctl restart mariadb
现在我有一个新的错误信息:
InternalError:(pymysql.err.InternalError)数据包序列号错误 - 得到 1 预期 0(此错误的背景: http ://sqlalche.me/e/13/2j85 )
解决方案
InternalError: Packet sequence number wrong - got 1 expected 0
可以在这里找到错误的解决方案: https ://github.com/PyMySQL/PyMySQL/issues/971
推荐阅读
- excel - 如何使用范围内的下一个可用行
- reactjs - 如何为使用 Table 组件材质 UI 的组件编写测试?
- python - 寻找用于捕获短语直到点的正则表达式模式
- java - Visual Studio 代码调试器 (Java)
- python-3.x - 在 python 3 中无法捕获异常
- reactjs - 为什么我的主要内容离左边那么远?
- unit-testing - 模拟聚合类
- python - 如何在一个查询中使用多个数据库?
- wordpress - 如何设置 docker-compose 以使用应用程序引擎和 wordpress?
- angular - 角度 formControl 值更改不起作用([formControl].valueChanges)不起作用