Updating mysql password hashes

From Wiki
Jump to: navigation, search

MySQL upgrades may require resetting user passwords after the upgrade is done due to out of date hashes.

This script makes a few assumptions, be careful.

#!/usr/bin/perl

@users = split('\n', `mysql -Bse 'select DISTINCT(user) from mysql.user where length(Password) < 41'`);

foreach $user (@users) {
  chomp $user;
  my @cpuser = split('_', $user);
  my $cpuser = shift(@cpuser);
  my $path = "/home/$cpuser/public_html";
  my $output = `find $path -name wp-config.php -exec grep 'DB_PASS' {} \\\;`;
  chomp $output;
  if (length($output) != 0) {
    #adjust length of string as needed
    my @pass = split("'", substr($string, 23));
    my $pass = shift(@pass);
    my $query = "UPDATE mysql.user SET Password = PASSWORD('$pass'), plugin = 'mysql_native_password' WHERE User = '$user';";
    print "$query\n";
  }
  else {
    #do nothing
    next;
    #print "$user is not a wordpress user.\n";
  } 
}