reading sql database with sqlalchemy: OperationalError [Errno 111] Connection refused

1k views Asked by At

This is working fine

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)

If I replace localhost with the IP it doesn't

from sqlalchemy import create_engine
import pandas as pd

db_connection_str = 'mysql+pymysql://User:[email protected]/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)") (Background on this error at: http://sqlalche.me/e/13/e3q8)

But it's necessary to replace localhost with the IP because I want to execute the script from a client.

EDIT: In my /etc/mysql/my.cnf there was no bind-adddress: - so I added the last line:

# 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

and executed sudo systemctl restart mariadb

Now I have a new error message:

InternalError: (pymysql.err.InternalError) Packet sequence number wrong - got 1 expected 0 (Background on this error at: http://sqlalche.me/e/13/2j85)

1

There are 1 answers

0
masterofpuppets On BEST ANSWER

A solution for the InternalError: Packet sequence number wrong - got 1 expected 0 error can be found here: https://github.com/PyMySQL/PyMySQL/issues/971