当前位置:   article > 正文

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

peewee.operationalerror: could not connect to server: connection refused",

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

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?

解决方案

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/172738
推荐阅读
相关标签
  

闽ICP备14008679号