Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

SQL Server error 3417: "Cannot recover the master database. SQL Server is unable to run." and what to do next

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

The SQL Server service will not start, and the error log ends with error 3417. This is a serious start-up error: Microsoft says that if the master or tempdb databases cannot be brought online, SQL Server cannot run.

The wording of Microsoft's page helps: the error "is typically preceded by other errors", and those earlier errors point to the real cause. Below are how to read them, what Microsoft documents for a failed upgrade, and the order of the repair options.

What the error means

Microsoft gives the message text for error 3417 as: "Cannot recover the master database. SQL Server is unable to run. Restore master from a full backup, repair it, or rebuild it. For more information about how to rebuild the master database, see SQL Server Books Online." The symbolic name is REC_BADMASTER.

The explanation on the page: "SQL Server can't start the master database. If the master or tempdb databases can't be brought online, SQL Server can't run. This error is typically preceded by other errors. Review error logs for those errors to find the root cause."

A second route to the same error: "This error may be encountered during an upgrade of SQL Server to a Cumulative Update or Service Pack. The upgrade scripts may fail due to an underlying issue and may cause the master database to not recover. Again the underlying cause is to be found in the latest SQL Server error log prior to this message."

Likely causes

Microsoft's two pages describe these situations:

Situation What the log shows
A failed upgrade or Cumulative Update script Error 912, "Script level upgrade for database 'master' failed", followed by 3417
Insufficient disk space for tempdb Error 1101 "Could not allocate a new page for database 'tempdb' because of insufficient disk space in filegroup 'PRIMARY'", then 912, then 3417 (an example Microsoft gives)
master or tempdb cannot be brought online for another reason Other errors before 3417; Microsoft's page does not list them

For the upgrade case, Microsoft explains the sequence. When SQL Server is upgraded or a Cumulative Update is applied, only the binaries are upgraded at first. "Once the binaries are replaced with new versions and the service restarts for the first time, a database upgrade is started." If a script fails (error 912), the database is taken offline, and for master that "will prevent the entire SQL Server instance from starting".

Prove the cause before changing anything

All of these only read.

  1. Open the SQL Server error log and find error 3417. Scroll up and read the errors immediately before it. Microsoft says: "Examine the log for errors that occurred immediately before error 912 and focus on troubleshooting the error referenced in the messaging of Error 912."
  2. Note the exact first error: number, state, severity and any file name. If error 912 is present, note the upgrade step it names. Microsoft says the upgrade scripts are located under C:\Program Files\Microsoft SQL Server\MSSQLXX.YYYY\MSSQL\Install.
  3. If a setup wizard reported "Wait on the Database Engine recovery handle failed. Check the SQL Server error log for potential causes.", Microsoft says this may be raised "with a wide variety of installation issues" and always directs you to the error log.
  4. Ask what changed last: a Cumulative Update, a disk that filled, a moved or restored file.
  5. Check free space on the drives holding the instance's data files if the log shows error 1101.

Example (illustrative): at contoso.com.au the SQL Server service on SQL01 does not start after a Cumulative Update. The log shows error 1101 about tempdb running out of space in filegroup PRIMARY, then error 912 for upgrade step xxx.sql, then 3417 with state 3. The data drive was full when the upgrade ran. The cause is the earlier disk space error, not damage to master.

Fix

Take the options in this order. The first only helps when the cause is a failed upgrade script.

If the cause is a failed upgrade script: start with trace flag 902

Microsoft says that, in some cases, you may need to start the service with trace flag 902: "Starting the service with T902 allows the service to skip execution of the upgrade scripts during startup. That way, you get a chance to investigate and fix the underlying issue." And: "Be sure to remove the trace flag once you have resolved the issue so the setup process can restart the upgrade script execution phase."

Save first. In SQL Server Configuration Manager, open the instance's Properties, go to the Startup Parameters tab and write down every startup parameter currently listed.

Risk: The next steps start the instance with upgrade scripts skipped. The instance may run with upgraded program files but databases that have not finished upgrading, so only use it to investigate and repair. Applications should not use the instance in this state. If you forget to remove the flag, the upgrade scripts will not run when you restart.

  1. In SQL Server Configuration Manager, select the instance in SQL Server Services, right-click and select Properties.
  2. On the Startup Parameters tab, type -T902 in the "Specify a startup parameter" field, select Add, and select OK.
  3. Start the SQL Server service.
  4. Fix the underlying problem named in the log, for example free disk space.

To undo it: on the same Startup Parameters tab, select -T902 and remove it, then restart the service so the upgrade scripts can run.

If master is damaged: restore it, after a rebuild if the instance will not start

Microsoft's page for error 3417 says the choices are to restore master from a full backup, repair it, or rebuild it. The restore page says that if the instance cannot start because master is damaged, you "must rebuild the system databases first".

Save first. Microsoft's rebuild page asks you to record the server configuration values, the version and collation, the file locations of the system databases, and to locate your most recent full backups of master, model and msdb. If the instance cannot start, Microsoft's recording queries (sys.configurations, SERVERPROPERTY values, sys.master_files) cannot be run, so take the same facts from your documentation or monitoring.

The file copy in this paragraph is advice, not a step on Microsoft's pages, and Microsoft does not say that it makes a rebuild reversible. The files can be copied only while the SQL Server service is stopped. It is already stopped when the instance will not start; if it is running, stopping it ends every connection, so do that in a maintenance window. In File Explorer, copy the data and log files of master, model, msdb and tempdb (from where the instance keeps them) to a new, empty folder on another drive, and leave that folder untouched afterwards.

Disruptive step: The next command rebuilds the system databases. Microsoft states that master, model, msdb and tempdb "are dropped and recreated in their original location" and that "Any user modifications to these databases are lost", for example user-defined objects in master, scheduled jobs in msdb and changes to model. Microsoft lists re-creating missing entries for user databases among the tasks that follow a rebuild. The command runs Setup, and the instance cannot be used while it runs.

Microsoft's command, run from a command prompt as Administrator in the folder that holds setup.exe:

setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=InstanceName /SQLSYSADMINACCOUNTS=accounts

Microsoft says /SAPWD is required if the instance uses Mixed Authentication mode, and /SQLCOLLATION is optional and defaults to the server's current collation. It adds that "When Setup has completed rebuilding the system databases, it returns to the command prompt with no messages" and to examine the Summary.txt log.

After the rebuild Microsoft lists these tasks: restore your most recent full backups of master, model and msdb; re-create any missing entries (user databases, logins, backup devices) if a backup is not current; restore the distribution database if the instance is a replication distributor; move system databases back to the locations you recorded; and verify the server configuration values. Microsoft warns that if you changed the server collation, "do not restore the system databases".

To undo it: Microsoft's page gives no way back from a rebuild. The only way back offered here is advice, not a Microsoft procedure: stop the service and copy the saved files back into the folders they came from, keeping the saved folder itself as it is. Microsoft does not say that this returns a working instance.

Restore master

To restore master, Microsoft's steps are to start the instance in single-user mode from a command prompt, connect with sqlcmd from another command prompt window, and run a full restore. Microsoft says that if the instance cannot start because master is damaged, the system databases must be rebuilt first.

Save first. With the service stopped, copy the data and log files of master again into a second new, empty folder, so the state just before the restore can be put back. Do not reuse the folder from the rebuild step.

Risk: The next two commands start the instance in single-user mode and then replace the master database. Microsoft says of WITH REPLACE: "The existing database, if any, is deleted." After master is restored the instance shuts down and ends the sqlcmd session, and Microsoft says to remove the single-user startup parameter before restarting. Microsoft's warning applies too: the instance should match the original as closely as possible, with the same version, edition and patch level.

In the instance's Binn folder, for a default instance, Microsoft's command to start single-user mode is:

sqlservr -c -f -s MSSQLSERVER -mSQLCMD

Then, in sqlcmd, Microsoft's restore statement is:

RESTORE DATABASE master FROM <backup_device> WITH REPLACE;

To undo it: this is advice, not a Microsoft procedure. Stop the service and copy the files from the second saved folder back into the folders they came from, keeping that saved folder as it is.

Confirm the fix

  • The SQL Server service starts and stays running.
  • The error log shows the recovery of master completing, with no 912 or 3417.
  • Databases and logins are present, and the applications that use them connect.

What to watch afterwards

  • Whether -T902 is still on the startup parameters.
  • Backups of master, model and msdb. Microsoft says these three must always be backed up, and to back up master on a schedule that protects the data sufficiently for your business needs, with an extra backup after a substantial update.
  • Free disk space before the next update.

Risks and limits

These steps have not been run in a lab. Microsoft's error 3417 page gives no version range, and its 912 page covers SQL Server 2019 and later on Windows. The rebuild and restore are destructive for the system databases; the saved file copies are the only way back, and Microsoft's pages give no promise that copies taken this way restore cleanly. The restore needs a backup of master that matches the instance's version, edition and patch level.

Sources