sodcalculator

10 Tech How-To

Access SQL Database Without Password (Safe Ways)

Guide 10-4809 · Updated · Step by step

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 sqlcmd tool or SQL Server Management Studio (SSMS) installed.
  • For MySQL/MariaDB: the mysqld or mariadbd binary and mysql client.
  • For PostgreSQL: the postgres binary and psql client.
  • Disk space for a backup (optional but recommended).

Step-by-Step

For SQL Server (Windows)

  1. Open Command Prompt or PowerShell as Administrator.
  2. Stop the SQL Server service. Run: net stop MSSQLSERVER (or the actual service name, e.g., MSSQL$SQLEXPRESS for named instances).
  3. Start SQL Server in single-user mode. Run: net start MSSQLSERVER /f /m (the /f forces startup, /m allows only one connection).
  4. Open a new Command Prompt (still admin) and connect using sqlcmd: sqlcmd -S . -E (Windows authentication).
  5. 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];
  6. Exit sqlcmd (EXIT), stop SQL Server (net stop MSSQLSERVER), then start it normally (net start MSSQLSERVER).

For MySQL / MariaDB (Linux/Windows)

  1. Stop the MySQL service: sudo systemctl stop mysql (or mariadb, or net stop MySQL on Windows).
  2. Start MySQL in safe mode, skipping grant tables. Run: sudo mysqld_safe --skip-grant-tables --skip-networking & (on Windows, run mysqld --skip-grant-tables in a console).
  3. Connect without a password: mysql -u root.
  4. Flush privileges to enable changing passwords: FLUSH PRIVILEGES;
  5. 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;
  6. Exit mysql (EXIT), stop the safe-mode process (Ctrl+C or kill), then start MySQL normally: sudo systemctl start mysql.

For PostgreSQL (Linux)

  1. Stop PostgreSQL: sudo systemctl stop postgresql.
  2. Edit pg_hba.conf (usually in /etc/postgresql/<version>/main/ or /var/lib/pgsql/data/). Change the line for local connections to trust instead of md5 or scram-sha-256.
  3. Start PostgreSQL: sudo systemctl start postgresql.
  4. Connect as the postgres user: sudo -u postgres psql.
  5. Set a new password: ALTER USER postgres WITH PASSWORD 'NewPass123!';
  6. Exit psql (
    q
    ), stop PostgreSQL, revert pg_hba.conf back to the original settings, and start PostgreSQL again.

Common Mistakes

  • Forgetting to revert pg_hba.conf after resetting the password—leaves the database open to anyone with local access.
  • Using --skip-grant-tables without --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.msc or sc query to 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—try taskkill /F /PID on 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 run mysqld --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 PRIVILEGES again. 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

The whole Tech How-To bed →