Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) code example

Example 1: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

# Stop Your Server First
sudo service mysql stop

# Make MySQL service directory.
sudo mkdir /var/run/mysqld

# Give MySQL permission to work with the created directory
sudo chown mysql: /var/run/mysqld

# Start MySQL, without permission and network checking
sudo mysqld_safe --skip-grant-tables --skip-networking &

# Log in to your server without any password.
mysql -u root mysql


# Update the password for the root user:
UPDATE mysql.user SET authentication_string=PASSWORD('YourNewPasswordBuddy'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';

#if you omit (AND Host='localhost') section, it updates the root pass regardless of its host

FLUSH PRIVILEGES;
EXIT;

#kill mysqld_safe process
sudo service mysql restart

#Now you can use your new password to login to your Server
mysql -u root -p

#take note for remote access you should create a remote user and then grant all privileges to that remote user

Example 2: windows error 1045 (28000): access denied for user 'root'@'localhost' (using password: yes)

update user set authentication_string=password('my_password') where user='root';

Example 3: mysqli::real_connect(): (HY000/1045): Access denied for user 'pma'@'localhost' (using password: NO)

$cfg['Servers'][$i]['port'] = '3307';

Example 4: mysqli_real_connect(): (HY000/1698): Access denied for user 'root'@'localhost'

ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql';

Example 5: php catch mysqli_connect(): (HY000/1045): Access denied

$conn = mysqli_connect( '<server>', '<username>', '<password>',  '<database-name>');
if (!$conn) {
  // whatever processing you want to do upon error in connection
  // like log error or send an email to admin
  // I am just printing the error at the moment.
  echo mysqli_connect_errno() . ":" . mysqli_connect_error();
  exit;
}

Tags:

Sql Example