Peewee - Can't connect to MySQL server on host

蓝咒 提交于 2019-12-07 13:04:36

问题


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?

来源:https://stackoverflow.com/questions/30939688/peewee-cant-connect-to-mysql-server-on-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!