Peewee - 无法连接到主机上的MySQL服务器

时间:2015-06-19 13:41:46

标签: python mysql flask peewee

我正在使用peewee ORM开发基于Flask的python应用程序。我最初连接到本地存储在我的计算机上的数据库,现在我正在尝试过渡到远程连接数据库。我已经通过我的服务器的cpanel部分在phpmyadmin中设置了数据库。

问题

我已设置我的IP地址以便能够远程访问我的数据库但是当我尝试连接到数据库时出现以下错误:

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)")

这是我的代码中引用数据库连接的部分:

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()

我也尝试连接'localhost'作为主机,但这似乎不起作用,我应该连接到不同的主机吗?

1 个答案:

答案 0 :(得分:0)

解决方案是默认端口错误: peewee doc的示例是

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

但默认端口为3306