Sometimes you need to get into a SQL database but don’t have the password—maybe it was lost, an admin left, or you’re recovering an old system. This guide covers legitimate, safe methods to access SQL Server, MySQL, and PostgreSQL databases without the original password, using built-in tools and administrative privileges. You’ll need some level of system access (local admin or root) to succeed.
What You’ll Need
- Administrative access to the server or machine hosting the database (local admin on Windows, root on Linux).
- Ability to stop/start services (e.g., systemctl, services.msc, or Task Manager).
- Command-line access (Command Prompt, PowerShell, or terminal).
- For SQL Server: the
sqlcmdtool or SQL Server Management Studio (SSMS) installed. - For MySQL/MariaDB: the
mysqldormariadbdbinary andmysqlclient. - For PostgreSQL: the
postgresbinary andpsqlclient. - Disk space for a backup (optional but recommended).
Step-by-Step
For SQL Server (Windows)
- Open Command Prompt or PowerShell as Administrator.
- Stop the SQL Server service. Run:
net stop MSSQLSERVER(or the actual service name, e.g.,MSSQL$SQLEXPRESSfor named instances). - Start SQL Server in single-user mode. Run:
net start MSSQLSERVER /f /m(the/fforces startup,/mallows only one connection). - Open a new Command Prompt (still admin) and connect using
sqlcmd:sqlcmd -S . -E(Windows authentication). - Create a new login or reset an existing one. For example, to create a new sysadmin login:
CREATE LOGIN [NewAdmin] WITH PASSWORD = 'StrongPass!'; ALTER SERVER ROLE sysadmin ADD MEMBER [NewAdmin]; - Exit sqlcmd (
EXIT), stop SQL Server (net stop MSSQLSERVER), then start it normally (net start MSSQLSERVER).
For MySQL / MariaDB (Linux/Windows)
- Stop the MySQL service:
sudo systemctl stop mysql(ormariadb, ornet stop MySQLon Windows). - Start MySQL in safe mode, skipping grant tables. Run:
sudo mysqld_safe --skip-grant-tables --skip-networking &(on Windows, runmysqld --skip-grant-tablesin a console). - Connect without a password:
mysql -u root. - Flush privileges to enable changing passwords:
FLUSH PRIVILEGES; - Set a new root password. For MySQL 5.7+:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass123!';For older versions:UPDATE mysql.user SET Password=PASSWORD('NewPass123!') WHERE User='root'; FLUSH PRIVILEGES; - Exit mysql (
EXIT), stop the safe-mode process (Ctrl+C orkill), then start MySQL normally:sudo systemctl start mysql.
For PostgreSQL (Linux)
- Stop PostgreSQL:
sudo systemctl stop postgresql. - Edit
pg_hba.conf(usually in/etc/postgresql/<version>/main/or/var/lib/pgsql/data/). Change the line for local connections totrustinstead ofmd5orscram-sha-256. - Start PostgreSQL:
sudo systemctl start postgresql. - Connect as the postgres user:
sudo -u postgres psql. - Set a new password:
ALTER USER postgres WITH PASSWORD 'NewPass123!'; - Exit psql (
), stop PostgreSQL, revert
qpg_hba.confback to the original settings, and start PostgreSQL again.
Common Mistakes
- Forgetting to revert
pg_hba.confafter resetting the password—leaves the database open to anyone with local access. - Using
--skip-grant-tableswithout--skip-networking, exposing the database to remote connections with no password. - Not stopping the service before starting in single-user mode, causing a conflict.
- Typing the service name wrong—use
services.mscorsc queryto verify. - Changing the wrong user’s password (e.g., root instead of the app’s user).
- Forgetting to flush privileges after a direct UPDATE in MySQL.
Tips
- Always take a full backup before making any changes—this is a safety net if something goes wrong.
- Use a strong, unique password when you reset it, and store it in a password manager.
- If you can, create a new admin account instead of resetting the root password—this preserves the original account for auditing.
- Perform these steps during a maintenance window to avoid disrupting users.
- After regaining access, review all user accounts and permissions to ensure no unauthorized changes were made.
- Keep a record of the steps you took—useful for future incidents or for handing over to a new admin.
Troubleshooting
- Can’t stop the service (access denied): Ensure you’re running as Administrator (Windows) or using
sudo(Linux). Check if the service is hung—trytaskkill /F /PIDon the process. - sqlcmd says “Login failed”: Make sure SQL Server is in single-user mode and you’re using Windows authentication. If a client is already connected, disconnect it or wait.
- MySQL safe mode doesn’t start: Check the error log (e.g.,
/var/log/mysql/error.log). Ensure the data directory is writable by the mysql user. On some systems, you need to runmysqld --user=mysql --skip-grant-tables. - PostgreSQL won’t start after editing pg_hba.conf: Syntax error—verify the line format. Also check file permissions; the postgres user must own it.
- Password reset works, but you still can’t log in: Check that the new password meets the server’s complexity requirements. For MySQL, try
FLUSH PRIVILEGESagain. For PostgreSQL, ensure you’re connecting to the right database and user. - Service won’t start normally after the fix: Revert any changes you made to config files (like pg_hba.conf) and try again. Check the error logs for clues.
Frequently Asked Questions
Is it legal to access a SQL database without a password?
Only if you own the system or have explicit permission from the owner. Unauthorized access is illegal and unethical. This guide is for recovering your own databases or those you’re authorized to manage.
Can I recover the original password instead of resetting it?
No—SQL databases store password hashes, not plaintext. You can only reset the password, not retrieve the original.
Will these methods work on a cloud-hosted database (like AWS RDS)?
No. Cloud providers don’t give you OS-level access, so you can’t stop services or edit config files. You’d need to use the provider’s built-in password reset feature (e.g., AWS RDS console’s “Reset master password”).
What if I don’t have admin rights on the server?
You can’t use these methods. You’ll need to contact the system administrator or the database owner to get access.
Can I use these steps to hack into someone else’s database?
No—these methods require local OS access and are for legitimate recovery. Attempting unauthorized access is a crime.
Will resetting the password affect my data?
No—these methods only change authentication. Your tables, views, and stored procedures remain intact. However, always back up before making changes to be safe.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Find Leaked Passwords in Chrome, Edge, Wallet & iCloud Keychain
- Fingerprint Limit Reached on MacBook Pro: Fix It
- WhatsApp Call Errors: Meanings & Fixes
- Error 0x8007025d: Fix Windows Install Fails
- Net Framework 3.5 Installation Error 0x800f0950
- How to Organize Google Photos with Face Groups & Labels