title: Why am I Getting 500 internal server error in flask while connecting using MySQLdb?
body: <p>I am a newbie to flask and I started with a small app. Now I am trying to connect to mysql db. I have installed mysql workbench,made database added table and data to it. I can query the db using mysql cmd client.</p>
  
  <p>I tried to connect it using MySQLdb, but i am getting error 2005 in cmd and internal server error in browser.
  This is what i get when i goto <code>127.0.0.1:5000/dashboard</code></p>
  
  <pre><code>(env) C:\Users\415663\Documents\phatomation\project&gt;set FLASK_CONIG=developer
  
  (env) C:\Users\415663\Documents\phatomation\project&gt;set FLASK_APP=run.py
  
  (env) C:\Users\415663\Documents\phatomation\project&gt;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 -
  </code></pre>
  
  <p><strong>my __init__.py</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p><strong>my database.py</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>and my index.html</strong></p>
  
  <pre><code>&lt;!-- index.html--&gt;
  
  {% extends "base.html" %}
  {% block title %}Home{% endblock %}
  {% block body %}
  &lt;div class="jumbotron"&gt;
    &lt;h1&gt;Flask Is Awesome&lt;/h1&gt;
    &lt;p class="lead"&gt;And I'm glad to be learning so much about it!&lt;/p&gt;
  &lt;/div&gt;
  
  
  &lt;table border="1" cellpadding="5" cellspacing="5"&gt;
      {% for row in data %}
          &lt;tr&gt;
          {% for id in row %}
              &lt;td&gt;{{ id }}&lt;/td&gt;
          {% endfor %}
          &lt;/tr&gt;
      {% endfor %}
  &lt;/table&gt;
  
  
  {% endblock %}
  </code></pre>
  
  <p>I tried my level best by making small changes in db connection file,views file etc..But i am only getting <code>500 error</code>.</p>