Unable to connect to the (localdb)\MSSQLLocalDB
Asked Answered
E

1

5

Cannot connect to (LocalDB)\MSSQLLocalDB. A network-related or instance-specific error occurred while establishing a connection to SQL server. The server was not found or was not accessible. Verify that the instance name is correct and that the SQL server is configured to allow remote connections.(provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start.)( Microsoft SQL server)

Yesterday I was able to connect to the (LocalDB)\MSSQLLocalDB and was able to see all the databases in it in SQL server management studio. But today when I am trying to connect, am getting the above mentioned error.

How I was able to connect yesterday but why not now? Please help me.

Extirpate answered 17/12, 2021 at 10:57 Comment(25)
Try running sqlqlocaldb start MSSQLLocalDB from a command prompt.Dishman
I have tried. It is giving me the error that the start of LocaDB instance"MSSQLLocalDB" failed because of the following error: Error occurred during LocalDB instance startup: SQL server process failed to start.Extirpate
Check the SQL Server error log for more detail error messages: %USERPROFILE%\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb\error.logDishman
Where can I find it?Extirpate
I posted the path. Run notepad "%USERPROFILE%\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb\error.log" from a command prompt. Add the output to your question.Dishman
It is not recognising as a command.Extirpate
The path I provided is for SQL 2019 localdb. The exact path may differ for older versions but there should be an error log file in the mssqllocaldb directory.Dishman
Am using SQL server 2019 only. But it's not working.Extirpate
Do you see the directory on your machine? dir "%USERPROFILE%\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb"Dishman
No. Am not finding it.Extirpate
Does this command return anything? Wmic /node:%COMPUTERNAME% product get name | findstr "LocalDB". Also check for related messages in the Event Viewer.Dishman
Getting "Invalid Global Switch"Extirpate
It seems your computer name contains special characters. Try hard-coding the name and enclose it in single quotes: Wmic /node:'your-computer-name' product get name | findstr "LocalDB"Dishman
My computer name is like DESKTOP-9MGOUF. Now can u pls tell me the exact command what I have to give. And what this command will give? Also pls tell me about the event viewer. I don't know about that. I just opened it. What I need to check there?Extirpate
In the event viewer, I have opened the Application section of windows logs. I have selected the error. Below in the General tab, something is displayed that " The "DataDirectory" registry value is missing in the LocaDB instance registry key: {1BC91CE3-6325-44C6-87B8-5B92E315257D}Extirpate
The exact command is Wmic /node:'DESKTOP-9MGOUF' product get name | findstr "LocalDB". It should return "Microsoft SQL Server 2019 LocalDB". The missing registry value suggests your localdb instance is corrupt. You can recreate it with sqllocaldb delete MSSQLLocalDB and sqllocaldb create MSSQLLocalDB -s. This will recreate the registry entries.Dishman
That command is not returning anything for me now. If I delete and recreate the MSSQLLocalDB, don't I loose my databases which I had created earlier in my MSSQLLOCALDB?? Now can't I retrieve the data from the database created by me?Extirpate
Do you see your database files in your user directory? dir %USERPROFILE%\*.mdfDishman
Yes. I can see the database files and their log files in my user directory. But when I try to open them it is showing that those files are used by OS and by various programs. Editing or modify them could damage the system.Extirpate
Open them with what? Database files are managed by SQL Server; you can't open them in an application. You should be able to recreate the instance as I suggested and then attach the databases (mdf files), assuming they are not being used by another SQL instance.Dishman
This is the first time am using the SQL server nd SQL server management studio. I had learnt how to implement a database in the VS. I don't have much knowledge as am a beginner. Can you please clarify my doubt. If I uninstall and then reinstall all these applications will my issue be resolved? If I do so, will I lose my databases? However those databases are not much important to me. But to practice a course I need one of them. I can also recreate them. But I want my doubt to be get clarified. Thank you.Extirpate
Uninstalling/re-installing seems like overkill. Before doing that, recreate the instance as I suggested. Then connect to (LocalDB)\MSSQLLocalDB with SSMS and attach each the mdf file with a SQL query like EXEC sp_attach_db 'YourDatabaseName','C:\Users\YourUserName\YourDatabaseName.mdf';Dishman
Thank you. I will follow what u have suggested. But there is an attach option in SSMS. I can browse the files and attach them. But am not able to see the mdf files in my users folder. I mentioned that am able to see the database files and log files in my users folder. Are those and mdf files same?Extirpate
The files you attached are the ones in the location you browsed to. The are different if in the same directly. If you can see your tables in the databases you attached, you're good.Dishman
@DanGuzman there is a typo on your first comment: sqlqlocaldb. If anyone else (like myself) is trying that command, please change it to sqllocaldb start MSSQLLocalDB.Gemini
D
14

The error message in the Windows log about the missing registry value suggests your localdb instance is corrupt. You can recreate the MSSQLLocalDB instance and registry entries by running these commands from a command prompt:

sqllocaldb delete MSSQLLocalDB
sqllocaldb create MSSQLLocalDB -s
Dishman answered 17/12, 2021 at 14:36 Comment(3)
Finally! A solution that works! You're the man Dan.Envy
What if I don't want to lose all of my data? Every solution I have found says I need to wipe the database and start over? Why?Thermobarograph
@Caleb, if the database file exists, you should be able to attach it after recreating the instance. Find the data file(s) with dir "%USERPROFILE%\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb\*.mdf"Dishman

© 2022 - 2024 — McMap. All rights reserved.