django.db.utils.OperationalError: (1045, Access denied for user 'root'@'localhost' (using password: YES) )
You need to make changes in project settings.py. Provide USER
and PASSWORD
for your database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myproject',
'USER': 'root',
'PASSWORD': 'rootpassword',
'HOST': 'localhost',
'PORT': '',
}
}
django.db.utils.OperationalError: (1045:Access denied for user 'root'@'localhost' (using password: NO)
Run the following in mysql console, to change the password encryption method to the old version in Mysql(it is changed to use cha2 in Mysql 8.0)
mysql -u root -p
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
FLUSH PRIVILEGES;
Then you should be free to run python manage.py migrate
.
mysql_exceptions.OperationalError: (1045, Access denied for user 'root'@'localhost' (using password: YES) )
Run in console
mysql> grant all privileges on *.* to root@localhost identified by 'password' with grant option;
Django : mysql : 1045, Access denied for user
I do it like this for a database named foo_db:
create database foo_db;
create user foo_user identified by 'foo_password';
grant all on foo_db.* to 'foo_user'@'%';
flush privileges;
django.db.utils.OperationalError: (1045, Access denied for user '<user>'@'localhost'
This is what I use to re-create MySQL databases for Django projects:
tmp="/tmp/mysql-tools.sh.tmp"
setup-db ( ) {
cat <<EOF > $tmp
DROP DATABASE $DB;
CREATE DATABASE $DB;
GRANT USAGE on *.* to '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
GRANT ALL PRIVILEGES ON ${DB}.* to '${DB_USER}'@'localhost';
EOF
cat $tmp
mysql -f -u ${MYSQL_ROOTUSR} -p${MYSQL_ROOTPWD} < $tmp
rm $tmp
}
Warning: this drops and re-creates!
Where:
- DB: the database name
- DB_USER: the django database user
- DB_PASS: the password for the mysql connection for the Django database user
- MYSQL_ROOTUSR: the mysql root user (must have permissions to create databases)
- MYSQL_ROOTPWD: the mysql root password
Export those in your environment
#1045 - Access denied for user 'root'@'localhost' (using password: YES)
The problem was I have 2 instances of Mysql installed and I didn't know the password for both instances.Just check if port 80 is used by any of the programs.
This is what I did
1.Quit Skype because it was using port 80.(Please check if port 80 is used by any other program).
2.Search for Mysql services in task manager and stop it.
3.Now delete all the related mysql files.Make sure you delete all the files.
4.Reinstall
Django - MySQL: 1045 Access Denied for 'user'@'localhost'
The problem was an extra newline character at the end of the password being read from file.
The solution was to change
db_pass = dbpf.read()
to
db_pass = dbpf.read().rstrip()
Related Topics
Python: Requests.Exceptions.Connectionerror. Max Retries Exceeded With Url
Pythonically Add Header to a CSV File
Get Current Url from Browser Using Python
Python Read File Determined by Separator \R\N
Permissionerror: [Errno 13] Permission Denied Flask.Run()
How to Smooth a Curve in the Right Way
Best Way to Identify and Extract Dates from Text Python
Create New Column Based on String
Most Efficient Way to Find Mode in Numpy Array
Replacing Special Characters in a List in Python
How to Repeat Each Test Multiple Times in a Py.Test Run
Convert Tensorflow String to Python String
Tensorflow:Attributeerror: 'Module' Object Has No Attribute 'Mul'
Python - Using Regex to Find Multiple Matches and Print Them Out
How to Use Ffmpeg in a Python Function
How to Create a Common Function to Execute a Python Script in Jenkins