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)
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.