Django.Db.Utils.Operationalerror: (1045, Access Denied for User '<User>'@'Localhost'

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



Leave a reply



Submit