Peewee - Can't connect to MySQL server on host

4.4k views Asked by At

I'm developing a Flask based python app using the peewee ORM. I was initially connecting to the database that was being stored locally on my machine and I'm now trying to transition to connecting to the db remotely. I've set up the database in phpmyadmin via my server's cpanel section.

The Issue

I've set up my IP address to be able to remotely access my databases but I am getting the following error when I attempt to connect to the database:

Traceback (most recent call last):
  File "app.py", line 294, in <module>
    models.initialize()
  File "/Users/wyssuser/Desktop/dscraper/models.py", line 145, in initialize
    DATABASE.connect()
  File "/Library/Python/2.7/site-packages/peewee.py", line 2767, in connect
    self.__local.closed = False
  File "/Library/Python/2.7/site-packages/peewee.py", line 2688, in __exit__
    reraise(new_type, new_type(*exc_value.args), traceback)
  File "/Library/Python/2.7/site-packages/peewee.py", line 2766, in connect
    **self.connect_kwargs)
  File "/Library/Python/2.7/site-packages/peewee.py", line 3209, in _connect
    return mysql.connect(db=database, **conn_kwargs)
  File "/Library/Python/2.7/site-packages/pymysql/__init__.py", line 88, in Connect
    return Connection(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/pymysql/connections.py", line 644, in __init__
    self._connect()
  File "/Library/Python/2.7/site-packages/pymysql/connections.py", line 869, in _connect
    raise exc
peewee.OperationalError: (2003, "Can't connect to MySQL server on '142.157.25.22' ([Errno 61] Connection refused)")

This is the portion of my code that references the database connection:

app.py

if __name__ == '__main__':
  models.initialize()
  app.run(debug=DEBUG, port=PORT, host=HOST)

config.py

DATABASE = {
  'db': 'my_dbname',
  'host': '142.157.25.22',
  'port': 3306,
  'user': 'my_username',
  'passwd': 'my_pswd',
}

models.py

from peewee import *
import config

DATABASE = MySQLDatabase(config.DATABASE['db'], host=config.DATABASE['host'], port=config.DATABASE['port'], user=config.DATABASE['user'], passwd=config.DATABASE['passwd'])

...all of my models related code

def initialize():
  print 'starting db connection'
  DATABASE.connect()
  print 'connected'
  DATABASE.create_tables([Batch, Company, User, Post],safe=True)
  DATABASE.close()

I've also tried connecting to 'localhost' as the host but that doesn't seem to work here, is there a different host I should be connecting to?

1

There are 1 answers

0
Honza Pilař On

Solution is bad default port: the example from peewee doc is

# Connect to a MySQL database on network.
mysql_db = MySQLDatabase('my_app', user='app', password='db_password',
                         host='10.1.0.8', port=3316)

but defaul port is 3306