Jump to content
Sign in to follow this  
support

Reset MySQL Password (by Click Studios)

Recommended Posts

Script Name
Reset MySQL Password
 
Description
Script to reset a MySQL Account Password
 
System Requirements

  • Firewall to allow access

Successfully Tested Against

  • MySQL Server 5.6.16

Failed Testing Against

  • None Reported

PowerShell Script

<#
.SYNOPSIS
Connect to a MySQL server using the supplied Privileged Account Credentials, and change the password for a local account.
.NOTES
Requires database connections on in-use Port to be allowed through Firewall
#>
function Set-MySQLPassword
{
	[CmdletBinding()]
	param (
		[String]$HostName,
		[String]$SQLPort,
		[String]$UserName,
		[String]$NewPassword,
		[String]$PrivilegedAccountUserName,
		[String]$PrivilegedAccountPassword
	)
	
	#$SQLScript to be called once a database connection has been established. Add one command per line.
	$SQLScript = @"
	UPDATE mysql.user SET Password=PASSWORD('$NewPassword') WHERE User='$UserName';
	Flush Privileges;
"@
	
	try
	{
		#Declare some connection string variables
		[String]$MySQLPortString = ''
		
		#Construct the Port Number section of the connection string if required
		if ($SQLPort -ne '') {
			$MySQLPortString = ';port=' + $SQLPort
		}
		
		[void][System.Reflection.Assembly]::LoadFrom("[PasswordstateBinFolderPath]\MySQL.Data.dll")
		$MySQLConnection = New-Object MySql.Data.MySqlClient.MySqlConnection
		$MySQLConnection.ConnectionString = "server=" + $HostName + $SQLPortString + ";uid=" + $PrivilegedAccountUserName + ";pwd=" + $PrivilegedAccountPassword
		$MySQLConnection.Open()
		$MySQLCommand = New-Object MySql.Data.MySqlClient.MySqlCommand($SQLScript, $MySQLConnection)
		$results = $MySQLCommand.ExecuteNonQuery()
		$MySQLConnection.Close()
		switch -wildcard ($results.ToString().ToLower())
		{
			"*0*" { Write-Output "Failed to reset the password for the account '$UserName' on Host '$HostName' as the account could not be found."; break }
			#Add other wildcard matches here as required
			default { Write-Output "Success" }
		}
	}
	catch
	{
		switch -wildcard ($error[0].Exception.ToString().ToLower())
		{
			"*Unable to connect*" { Write-Output "Failed to connect to the Host '$HostName' to reset the password for the account '$UserName'. Please check the Host is online, or if a Firewall is blocking access - default Port is 3306."; break }
			"*Access denied for user*" { Write-Output "Failed to connect to the Host '$HostName' to reset the password for the account '$UserName'. Please check the Privileged Account Credentials associated with the Password Reset script are correct."; break }
			#Add other wildcard matches here as required
			default { Write-Output "Failed to reset the password for the MySQL account '$UserName' on Host '$HostName'. Error = " + $error[0].Exception }
		}		
	}
}

#Make a call to the Set-MySQLPassword function
Set-MySQLPassword -HostName '[HostName]' -SQLPort '[DatabasePort]' -Username '[UserName]' -NewPassword '[NewPassword]' -PrivilegedAccountUserName '[PrivilegedAccountUserName]' -PrivilegedAccountPassword '[PrivilegedAccountPassword]'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×