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

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

本文介绍了Peewee-无法连接到主机上的MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

问题

我已经设置了可以远程访问数据库的IP地址,但是在尝试连接数据库时出现以下错误:

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

我也曾尝试连接到"localhost"作为主机,但在这里似乎不起作用,我应该连接到其他主机吗?

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?

推荐答案

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

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)

但默认端口为3306

but defaul port is 3306

这篇关于Peewee-无法连接到主机上的MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:01