簡體   English   中英

為什么在使用MySQLdb連接時,燒瓶中出現500個內部服務器錯誤?

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

我是Flask的新手,我從一個小應用程序開始。 現在,我正在嘗試連接到mysql db。 我已經安裝了mysql工作台,使數據庫添加了表和數據。 我可以使用mysql cmd客戶端查詢數據庫。

我嘗試使用MySQLdb進行連接,但是在cmd中出現錯誤2005,在瀏覽器中出現內部服務器錯誤。 這是我轉到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 -

我的__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)

我的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

和我的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 %}

我通過在數據庫連接文件,視圖文件等中進行小的更改來盡我最大的努力。但是我僅收到500 error

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

是錯的,應該是

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

主機名和端口需要單獨提供。

另外,這些是默認值,您也可以忽略它們。 僅當您使用其他端口/主機時,才需要提供它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM