使用sqlalchemy读取sql数据库:OperationalError [Errno 111]连接被拒绝

问题描述

这很好用

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年,“无法连接到192.168.0.7上的MysqL服务器([Errno 111]连接被拒绝)”) (此错误的背景位于: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(此错误的背景位于: http://sqlalche.me/e/13/2j85

解决方法

可以在此处找到 InternalError: Packet sequence number wrong - got 1 expected 0 错误的解决方案:https://github.com/PyMySQL/PyMySQL/issues/971

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...