简体   繁体   English

为什么在使用MySQLdb连接时,烧瓶中出现500个内部服务器错误?

[英]Why am I Getting 500 internal server error in flask while connecting using MySQLdb?

I am a newbie to flask and I started with a small app. 我是Flask的新手,我从一个小应用程序开始。 Now I am trying to connect to mysql db. 现在,我正在尝试连接到mysql db。 I have installed mysql workbench,made database added table and data to it. 我已经安装了mysql工作台,使数据库添加了表和数据。 I can query the db using mysql cmd client. 我可以使用mysql cmd客户端查询数据库。

I tried to connect it using MySQLdb, but i am getting error 2005 in cmd and internal server error in browser. 我尝试使用MySQLdb进行连接,但是在cmd中出现错误2005,在浏览器中出现内部服务器错误。 This is what i get when i goto 127.0.0.1:5000/dashboard 这是我转到127.0.0.1:5000/dashboard时得到的

(env) C:\Users\415663\Documents\phatomation\project>set FLASK_CONIG=developer

(env) C:\Users\415663\Documents\phatomation\project>set FLASK_APP=run.py

(env) C:\Users\415663\Documents\phatomation\project>flask run
 * Serving Flask app "run"
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2018-01-17 13:51:51,367] ERROR in app: Exception on /dashboard [GET]
Traceback (most recent call last):
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\_com
pat.py", line 33, in reraise
    raise value
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\flask\app.
py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\415663\Documents\phatomation\project\app\views.py", line 18,
in dashboard
    c, conn = connection()
  File "C:\Users\415663\Documents\phatomation\project\database.py", line 8, in
 connection
    db = "pha_std")
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\MySQLdb\__
init__.py", line 86, in Connect
    return Connection(*args, **kwargs)
  File "c:\users\415663\documents\phatomation\env\lib\site-packages\MySQLdb\co
nnections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host '127.0.0.1
:3306' (0)")
127.0.0.1 - - [17/Jan/2018 13:51:51] "GET /dashboard HTTP/1.1" 500 -

my __init__.py 我的__init__.py

from flask import render_template

from app import app

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/about')
def about():
    return render_template("about.html")


from database import connection

@app.route('/dashboard')       
def dashboard():
        c, conn = connection()
        query = "SELECT * from level_1"
        c.execute(query)
        data = c.fetchall()
        conn.close()
        return render_template("index.html", data=data)

my database.py 我的database.py

import MySQLdb

def connection():
    # Edited out actual values
    conn = MySQLdb.connect(host="127.0.0.1:3306",
                           user="root",
                           passwd="1234",
                           db = "pha_std")
    c = conn.cursor()

    return c, conn

and my index.html 和我的index.html

<!-- index.html-->

{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block body %}
<div class="jumbotron">
  <h1>Flask Is Awesome</h1>
  <p class="lead">And I'm glad to be learning so much about it!</p>
</div>


<table border="1" cellpadding="5" cellspacing="5">
    {% for row in data %}
        <tr>
        {% for id in row %}
            <td>{{ id }}</td>
        {% endfor %}
        </tr>
    {% endfor %}
</table>


{% endblock %}

I tried my level best by making small changes in db connection file,views file etc..But i am only getting 500 error . 我通过在数据库连接文件,视图文件等中进行小的更改来尽我最大的努力。但是我仅收到500 error

MySQLdb.connect(host="127.0.0.1:3306", ...)

is wrong, it should be be 是错的,应该是

MySQLdb.connect(host="127.0.0.1", port=3306, ...)

Hostename and port need to be supplied separately. 主机名和端口需要单独提供。

Also, these are the default values, you can also omit them. 另外,这些是默认值,您也可以忽略它们。 Only if you use a different port/host you have to supply them. 仅当您使用其他端口/主机时,才需要提供它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到 500 内部服务器错误消息? - Why am I getting the 500 Internal Server Error message? 为什么在导入本地 python 文件时出现 500 内部服务器错误? - Why am I getting a 500 Internal Server Error when importing local python file? 为什么通过python请求调用rest api时出现500 Internal Server Error? - Why am I getting 500 Internal Server Error when calling post rest api via python request? 为什么我收到内部服务器错误 - Why am I getting an Internal Server error 使用Flask连接到MySQL时出现500内部服务器错误 - 500 Internal Server Error when connecting to MySQL with Flask 使用 Flask JWT 时出错,AttributeError: &#39;list&#39; 对象没有属性 &#39;id&#39; 并显示 500 内部服务器错误 - getting error while using Flask JWT, AttributeError: 'list' object has no attribute 'id' and shows 500 Internal server error 使用 flask 时出现 500 内部服务器错误 - 500 internal server error when using flask 在python中使用fileConfig时出现500个内部服务器错误 - Getting 500 internal server error while using fileConfig in python 在AWS EBS上部署Flask App时出现500个内部服务器错误 - Getting 500 internal server error while Deploying Flask App on AWS EBS 我不断收到 500 内部服务器错误 - I keep getting a 500 Internal server error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM