0

I am new to using python to connect to a mysql DB and I am getting the following error:

OperationalError: (pymysql.err.OperationalError) (1045, u"Access denied for user 'xxxxxxadmin'@'xx.xx.xx.xx' (using password: YES)") (Background on this error at: http://sqlalche.me/e/e3q8)
xx.xxx.216.44 - - [02/Apr/2018 17:27:49] "GET /testconnect HTTP/1.1" 500 -

This is most of the connect script in my python file:

   #!/usr/bin/python3
    from flask import Flask, request
    from flask_restful import Resource, Api
    from sqlalchemy import create_engine
    from json import dumps
    from flask.ext.jsonpify import jsonify

    db_connect = create_engine("mysql+pymysql://xxxxxxxadmin:password@,mymaindb.xxxxxxxx.us-east-2.rds.amazonaws.com:3306/myDBname")

    app = Flask(__name__)
    api = Api(app)

    class TestConnect(Resource):
        def get(self):
            conn = db_connect.connect() # connect to database
            query = conn.execute("select * from Players") # This line performs query and returns json result
            return {'employees': [i[0] for i in query.cursor.fetchall()]} # Fetches first column that is Employee ID

api.add_resource(TestConnect, '/testconnect') # Route_1



if __name__ == '__main__':
        app.run(host='0.0.0.0', debug = False)

Other background:

But when I try to connect to the same mysql database using the exact same credentials via the command line on the server running the python script I am able to get in.

Not sure how to test more to get a better error result that will help me figure this issue out.

UPDATE So I was able to connect to my DB via mysql workbench with the connection strings and information I have in the python script. Does this mean my python script is doing something wrong?

4
  • 1
    Your MySQL Host is rejecting connection from this host or your credential are wrong. Step 1 check if username and password are correct Step 2 Check if user is allowed to connect from this IP. Commented Apr 2, 2018 at 17:46
  • Step 3: actually connect from a MySQL client (e.g. the mysql CLI command). Do not proceed with Python code until you've made a canonical, known-to-work MySQL tool connect with the correct credentials. Commented Apr 2, 2018 at 17:49
  • @9000 does connecting using: mysql -h mymaindb.xxxxxxx.us-east-2.rds.amazonaws.com -P 3306 -u myDBname -p count as connecting from the command line? Because that does work and I get the mysql bash prompt after entering my password. Commented Apr 2, 2018 at 17:53
  • 1
    So I was able to connect to my DB via mysql workbench with the connection strings and information I have in the python script. I will update my question with that info. Commented Apr 2, 2018 at 18:08

1 Answer 1

1

Why not use:

mysql+pymysql://xxxxxxxadmin:[email protected]:3306/myDBname

instead of

mysql+pymysql://xxxxxxxadmin:password@**,**mymaindb.xxxxxxxx.us-east-2.rds.amazonaws.com:3306/myDBname

Not sure why you're connection string has a comma. Might just be a typo?

On that note, I usually build the connection URL before passing it to create_engine just to make it easier to manage in the future incase I have to pull the actual values from the environmental variables:

HOST = "mymaindb.xxxxxxxx.us-east-2.rds.amazonaws.com"
PORT = 3306
USERNAME = "xxxxxxxadmin"
PASSWORD = "password"
DBNAME = "myDBname"

CONNECTION_URL = 'mysql+pymysql://%s:%s@%s:%s/%s' % (
    USERNAME,
    PASSWORD,
    HOST,
    PORT,
    DBNAME
)
Sign up to request clarification or add additional context in comments.

2 Comments

I will build out my connection string in the future def cleaner. That was a typo with the comma. I will edit my question so there is no confusion.
I think it had something to do with a special character or a line end but when I used your method and saved it worked first try. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.