0

I have a openshift app written in python and would like to use Flask-mysql

import os
from datetime import datetime
from flask import Flask, request, flash, url_for, redirect, \
     render_template, abort, send_from_directory, json


from flask.ext.mysql import MySQL


app = Flask(__name__)
app.config.from_pyfile('flaskapp.cfg')
mysql = MySQL()

# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = ' adminuWfyNxG'
app.config['MYSQL_DATABASE_PASSWORD'] = ' '
app.config['MYSQL_DATABASE_DB'] = ''
app.config['MYSQL_DATABASE_HOST'] = '127.4.253.2:3306'
mysql.init_app(app)

#conn = mysql.connect()
#cursor = conn.cursor()


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

@app.route('/showSignUp')
def showSignUp():
    return render_template('signup.html')

@app.route('/<path:resource>')
def serveStaticResource(resource):
    return send_from_directory('static/', resource)


@app.route('/signUp',methods=['POST','GET'])
def signUp():
    try:
        _name = request.form['inputName']
        _email = request.form['inputEmail']
        _password = request.form['inputPassword']

        # validate the received values
        if _name and _email and _password:

            # All Good, let's call MySQL

            conn = mysql.connect()
            cursor = conn.cursor()

            cursor.callproc('sp_createUser',(_name,_email,_password))
            data = cursor.fetchall()

            if len(data) is 0:
                conn.commit()
                return json.dumps({'message':'User created successfully !'})
            else:
                return json.dumps({'error':str(data[0])})
        else:
            return json.dumps({'html':'<span>Enter the required fields</span>'})

    except Exception as e:
        return json.dumps({'error':str(e)})
    finally:
        cursor.close()
        conn.close()

if __name__ == "__main__":
    app.run()

When I try to run it on my app I receive an error.

HTTP/1.1 503 Service Temporarily Unavailable Date: Wed, 02 Sep 2015 19:09:34 GMT Content-Length: 428 Connection: close Content-Type: text/html; charset=iso-8859-1

I use the database user, password, databasename and host as per the openshift config.

Can anyone assist me?

Log file output:

Starting WSGIServer type flask on 127.4.253.1:8080 ... * Running on http://127.4.253.1:8080/ 127.4.253.1 - - [03/Sep/2015 13:40:33] "GET /showSignUp HTTP/1.1" 500 - Error on request: Traceback (most recent call last): File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/serving.p y", line 159, in run_wsgi execute(app) File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/serving.p y", line 146, in execute application_iter = app(environ, start_response) File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1836, in call return self.wsgi_app(environ, start_response) File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1836, in call return self.wsgi_app(environ, start_response) File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1817, in wsgi_app response = self.full_dispatch_request() File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1473, in full_dispatch_request rv = self.preprocess_request() File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask-0.10.1-py2.7.egg/flask/app.py ", line 1666, in preprocess_request rv = func() File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask_MySQL-1.3-py2.7.egg/flaskext/ mysql.py", line 48, in before_request ctx.mysql_db = self.connect() File "/var/lib/openshift/55d41b2c0c1e66a0060000b8/app-root/runtime/dependencie s/python/virtenv/lib/python2.7/site-packages/Flask_MySQL-1.3-py2.7.egg/flaskext/ mysql.py", line 44, in connect return MySQLdb.connect(**self.connect_args) File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/MySQLdb/init .py", line 81, in Connect return Connection(*args, **kwargs) File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/MySQLdb/connecti ons.py", line 187, in init super(Connection, self).init(*args, **kwargs2) OperationalError: (2005, "Unknown MySQL server host '127.4.253.2:3306' (1)")

user
  • 1,220
  • 1
  • 12
  • 31
  • What error are you getting in your log files? https://developers.openshift.com/en/managing-log-files.html –  Sep 02 '15 at 19:57
  • I am receiving the following error: Unknown MySQL server host – user2459813 Sep 03 '15 at 05:54
  • Are you using the environment variables for your mysql connection? –  Sep 03 '15 at 15:04
  • The host is wrong, it should jsut be the ip address, don't include the port, and you should not post your passwords.... –  Sep 03 '15 at 18:54
  • Tried that, removed the port. still doesnt work. Can we maybe teamviewer? – user2459813 Sep 04 '15 at 18:15
  • Have you looked at [openshift's HOWTO add DB to your app](https://developers.openshift.com/en/managing-adding-a-database.html) As I don't see any of it in the output provided. At least the ENV variables are always in form OPENSHIFT__PORT etc ... –  Sep 07 '15 at 11:08

0 Answers0