I am trying to connect to MySQL database in Flask as follows:
mysql = MySQL()
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DATABASE_DB'] = 'myDB'
mysql.init_app(app)
I am getting the following error:
pymysql.err.OperationalError: (1045, "Access denied for user 'myusername'#'localhost' (using password: NO)")
I tried reinstalling XAMPP but to no avail.
I have 2 issues:
Why am I getting the error for 'myusername' instead of 'root'?
How can I solve this issue?
SOLUTION:
I needed to grant the correct privilege to the specific user#localhost in MySQL.
By default in XAMPP root user doesn't have password, you can create a new one doing the following steps:
Click Admin next to MySQL on the XAMPP Control Panel to bring up the phpMyAdmin.
Choose the mysql database on the left.
Then, choose the SQL tab on the top and run the following statement:
SET Password=PASSWORD('password');
But, instead I highly recommend to create a new user:
CREATE USER 'myuser'#'%' IDENTIFIED BY 'password';
CREATE DATABASE myDB;
GRANT ALL PRIVILEGES ON myDB.* TO 'myuser'#'localhost';
Related
i have a flask python web site, im trying to connecto to mysql server but get
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied
for user 'root'#'xxx.xxx.251.67' (using password: YES)
i can reach the database thru mysql workbench using the data that im using in flask.
Iv tryed to flush previlegues
update root password
iv granted all permission on server to user root on %
but still get this error.
Anyone can help?
my code is
mydb = mysql.connector.connect(
host="mysql.server.com",
user="root",
password="passs123",
database="mydb"
)
select user,host from mysql.user ;
show grants for root#%;
show grant for root;
check the privileges for root#'%' on mydb;
I am hosting my flask website on ubuntu 18.04 server but I am using Flask SQLAlchemy to connect to my database previously (30-45 days ago) I connected it successfully but now I am getting this error -
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1045, "Access denied for user 'root'#'localhost' (using password: NO)")
I am using the following code to connect to the database from my flask website -
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:helloworld#localhost/vit"
app.config['MYSQL_HOST'] = "localhost"
app.config['MYSQL_USER'] = "root"
app.config['MYSQL_PASSWORD'] = "helloworld"
app.config['MYSQL_DB'] = "vit"
app.config['MYSQL_CURSORCLASS'] = "DictCursor"
I tried to create a new user in MySQL by native password, I tried to grant all privileges to root, I tried to also change the password of the root, I also tried to use mysql+pymysql in SQLALchemy URI. But no one method is working.
Kindly suggest to me any solution to this problem.
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
I have just installed mysql on Mac OSX and can easily open it in a shell and gain access to the databases and tables that come with it.
Now I want to create a database in Python using mysql-connector/python
Before doing that I figured it would be wise to set a user name and password in mysql.
I have tried using the following but get error messages.
My GOAL is to set a user name and a password so that I may simply provide it to mysql-connector for python, and ultimately execute sql queries from the python environment. Here is what I tried at the mysql command prompt:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('newpwd');
And I get the error message:
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'mysql'
So I tried the UPDATE METHOD:
UPDATE mysql.user SET Password = PASSWORD('newpwd')
WHERE User = 'root';
and I get the error message:
ERROR 1142 (42000): UPDATE command denied to user ''#'localhost' for table 'user'
So does anyone know why I get denied and how I can make the changes to create a user and a password that I may ultimately use in mysql connect in the python environment?
Sounds like you are not properly authenticated. Make sure you are authenticated with root user
Access denied for user ''#'localhost' to database 'mysql'
should look like
Access denied for user 'root'#'localhost' to database 'mysql'
Otherwise you are not logged in as root, but rather as an anonymous user.
Ofc, when you are logged in with root, you won'T get that message, but you would get it for any username that does not have the proper rights.
start your query with authentication
mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME
example:
mysql --port=3307 --host=127.0.0.1 -u root --password=thisismypass test -e "LOAD DATA LOW_PRIORITY LOCAL INFILE '%pathuser%' INTO TABLE `user_log`...."
in your case probably
mysql -u root "SET PASSWORD FOR 'root'#'localhost' = PASSWORD('newpwd');"
I'm trying to connect python with MySQL, but I get this error:
OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: NO)")
I'm trying to solve it for 6 hours and I fail. Can any one please help me ?
it means that you forgot the password parameter, which in mysql is not required.
from a unix command line:
$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
$ mysql -u root -p
Enter password:
<typing password>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.1.37-1ubuntu5.5 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
not exactly the same as logging from python,
but it's the same error, and most likely means that in your case the password argument did not make it to mysql at all.
when I type the wrong password I get a very different error on the command line:
$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
You didn't specified your root password.
If you are accessing mysql from remote computer... root user is by default limited to localhost connections only. Try running this to enable access from anywhere:
GRANT ALL PRIVILEGES ON *.* TO root#'%' IDENTIFIED BY "YOUR_ROOT_PASSWORD";
FLUSH PRIVILEGES;
Also check your firewall settings.
import MySQLdb
cxn = MySQLdb.connect(host = 'localhost',user = 'root',passwd = 'password', db = 'yourdatabase')
So you need to know what your password is and you need to have created a database with:
CREATE DATABASE yourdatabase;
from the mysql command line as described above.
I was getting this error because I had quotes in my config file.
[mysql]
username: 'uuuu'
password: 'pppp'
host: 'localhost'
database: 'dddd'
should have been
[mysql]
username: uuuu
password: pppp
host: localhost
database: dddd
If you don't see any value in front of the password column when you do
SELECT user, host, password FROM mysql.user;
within your mysql
In your python script don't specify the password
for example
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="", # your password
db="dynamo",
port = 3306) # name of the data base
# you must create a Cursor object. It will let
# you execute all the queries you need
cur = db.cursor()
# Use all the SQL you like
cur.execute("select * from SomeTable limit 10")
# print all the first cell of all the rows
for row in cur.fetchall():
print(row[0])
db.close()