Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get forgotten MySQL password from MySQL Workbench on Windows 10?

This question is essentially the same question as this one, but for Windows 10.

enter image description here

I have checked the documentation and still have no clear answer. A step by step for viewing the password would be perfect and truly appreciated!

like image 432
bjones01001101 Avatar asked Dec 09 '25 18:12

bjones01001101


2 Answers

First go check where your encrypted file is. Usually it is stored at %AppData%\MySQL\Workbench\workbench_user_data.dat

Now that you have the file location, open up powershell and copy & paste the following sequence of commands (order is important):

$cipher = Get-Content $env:APPDATA\MySQL\Workbench\workbench_user_data.dat -AsByteStream -Raw

Hit Enter

$scope = [System.Security.Cryptography.DataProtectionScope]::CurrentUser

Hit Enter

$mysqlpwd = [System.Security.Cryptography.ProtectedData]::Unprotect(
    $cipher, $null, $scope )

Hit Enter

[System.Text.UTF8Encoding]::UTF8.GetString($mysqlpwd)

Hit Enter

Now you can see the following output:

Mysql@X:Y@Z#DBUSER#DBPASSWORD
...
ssh@Z:22#SSHUSER#SSHPASSWORD
...

If you receive a TypeNotFound error:

Unable to find type [System.Security...].

you may also need to run the following to get access to "System.Security"

Add-Type -AssemblyName System.Security

References:

  • https://github.com/mysql/mysql-workbench/blob/5e10b42aa539155128bd97d54ed2cdd7ca283b34/library/forms/winforms/src/wf_utilities.cpp#L857
  • https://dev.mysql.com/doc/workbench/en/wb-mysql-connections-vault.html
like image 129
L. John Avatar answered Dec 14 '25 20:12

L. John


I made a quick Python solution adapted from the link in Mike's answer, since the PowerShell answer didn't work for me.

import os
import re
import win32crypt  # Requires pywin32 package

_group = '(?P<{}>{}+)'
WORKBENCH_PATTERN = '{dbm}@{host}:{port}\2{user}\3{password}'.format(
    dbm=_group.format('dbm', r'\w'),
    host=_group.format('host', r'[\w\.-]'),
    port=_group.format('port', r'\d'),
    user=_group.format('user', r'[\w\.-]'),
    password=_group.format('password', '.'),
)

def read_workbench_user_data(file_path=None):
    if file_path is None:
        file_path = os.path.join(os.getenv('APPDATA'), 'MySQL/Workbench/workbench_user_data.dat')

    with open(file_path, 'rb') as file:
        encrypted_data = file.read()
    decrypted_data = win32crypt.CryptUnprotectData(encrypted_data, None, None, None, 0)
    decrypted_content = decrypted_data[1].decode('utf-8')
    return decrypted_content.split('\n')[:-1]

if __name__ == '__main__':
    for item in read_workbench_user_data():
        match = re.match(WORKBENCH_PATTERN, item)

        print(f'{match.group("dbm")}:')
        print(f'\tHost: {match.group("host")}:{match.group("port")}')
        print(f'\tUsername: {match.group("user")}')
        print(f'\tPassword: {match.group("password")}')

Here's how the output looks:

enter image description here

like image 38
Peter Avatar answered Dec 14 '25 20:12

Peter