user9578771
user9578771

Reputation: 19

pymysql.err.OperationalError: (1045, "Access denied for user 'MYID'@'localhost' (using password: NO)")

I am running a flask web server on server A, and I need to access a SQL database in remote server B.

I am getting this error.

pymysql.err.OperationalError: (1045, "Access denied for user 'MYID'@'localhost' (using password: NO)")

Can someone help me??

Below is my code:

from flask import Flask, render_template, request
from flask.ext.mysql import MySQL

app = Flask(__name__)

app.config['MYSQL_HOST']='IP OF SERVER B'
app.config['MYSQL_PORT']='SERVER B PORT NUMBER'
app.config['MYSQL_USER']='MYID'
app.config['MYSQL_PASSWORD']='MYPW'
app.config['MYSQL_DB']='DBNAME'
mysql=MySQL(app)
mysql.init_app(app)
@app.route('/')
def index():
    cur = mysql.get_db().cursor()
    cur.execute('''SQLQUERY;''')
    rv=cur.fetchall()
    return str(rv)

Upvotes: 2

Views: 21510

Answers (2)

Christopher
Christopher

Reputation: 37

try changing your

app.config['MYSQL_HOST'] = ...

to

app.config['MYSQL_DATABASE_HOST'] = ...

this applies to the rest as well:

MYSQL_DATABASE_HOST default is ‘localhost’
MYSQL_DATABASE_PORT default is 3306
MYSQL_DATABASE_USER default is None
MYSQL_DATABASE_PASSWORD default is None
MYSQL_DATABASE_DB   default is None
MYSQL_DATABASE_CHARSET  default is ‘utf-8’

I had the same problem but this fixed it for me. Hope this helps you as well. Look at the Flask MySQL reference here

Upvotes: 0

Marinos An
Marinos An

Reputation: 10816

Using a mysql client or console you should execute something like this:

grant all privileges on DBNAME.* to MYID@localhost identified by 'MYPW';

using a user with grant privilege (usually root).

Of course you can narrow down the privileges that you grant from: all privileges to let's say: select,insert,update,delete, depending on the case.

To access the console with root open a terminal window and write:

mysql -uroot -p

and provide the password or for passwordless root:

mysql -uroot

In case you do not know the root password follow this guide: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

In case you are on a company/university workstation with no privileges to perform the above operation, ask from the administrator to grant the privileges.

Upvotes: 1

Related Questions