Modify physical_name from my sys.master_files in SQL Server - sql-server

I freshly installed SQL Server on my new Windows 11 machine, but I can't start the MSSQLSERVER service. I get an error that some files can't be found (example: d:\dbs\sh\s19s\0924_133725\cmd\2\obj\x64retail\sql\mkmastr\databases\mkmastr.proj\model.mdf). They can't be found because that path doesn't exist, I don't have a d:\ drive. I want to know how is it possible to modify the physical_name of my files ?
SELECT name, physical_name
FROM sys.master_files;
Returns:
name physical_name
---------------------- -----------------------------------------------------
master C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\master.mdf
mastlog C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\mastlog.ldf
tempdev d:\dbs\sh\s19s\0924_133725\cmd\2\obj\x64retail\sql\mkmastr\databases\mkmastr.proj\tempdb.mdf
templog d:\dbs\sh\s19s\0924_133725\cmd\2\obj\x64retail\sql\mkmastr\databases\mkmastr.proj\templog.ldf
modeldev d:\dbs\sh\s19s\0924_133725\cmd\2\obj\x64retail\sql\mkmastr\databases\mkmastr.proj\model.mdf
modellog d:\dbs\sh\s19s\0924_133725\cmd\2\obj\x64retail\sql\mkmastr\databases\mkmastr.proj\modellog.ldf
MSDBData d:\dbs\sh\s19s\0924_133725\cmd\2\obj\x64retail\sql\mkmastr\databases\mkmastr.proj\MSDBData.mdf
MSDBLog d:\dbs\sh\s19s\0924_133725\cmd\2\obj\x64retail\sql\mkmastr\databases\mkmastr.proj\MSDBLog.ldf
(8 rows affected)
and
SELECT name, filename FROM sysfiles
returns:
name physical_name
---------------------- -------------------------------------------------------------
master C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\master.mdf
mastlog C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\mastlog.ldf
ALTER TABLE sys.master_files
doesn't do anything.

Related

SQL Server: attach database without a log file

I have uninstalled my SQL Server 2019 and installed SQL Server 2022 Express. All my databases are under:
C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA
My new location is:
C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA
Attaching a database directly from the old location results in:
Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DBName.mdf". Operating system error 5: "5(Access is denied.)".
I then copied the .mdf file to the new location. But trying to attach it from the new location, I get a similar error, but for the log file:
Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DBName_log.ldf". Operating system error 5: "5(Access is denied.)".
I can't copy the log file into the new location because I don't have enough space on my HD.
Any idea?
Thanks
Operating system error 5: "5(Access is denied.)".
SQL Server locks down the data folder with NTFS ACLs. So you can fix this by taking ownership of the folder and files and granting full control to the other SQL Server instance.
SQL Server disables permissions inheritance on the files, so you need to grant full control to the other SQL instance to each file. And if you're connected using Windows Integrated Auth, you'll also need to grant yourself full control over the files.
In my test, after granting full control over the mdf and ldf to 'NT Service\MSSQLSERVER' and to my windows user, I was able to run
CREATE DATABASE [Foo]
ON (FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\Foo.mdf' ),
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\Foo_log.ldf' )
for attach
from the default instance.
You can also move (not copy) the files without needing additional space.

How to import .bak file to using mssql on vscode?

I use Linux. But I work on sql, mssql. So i only find a way to use mssql on linux with vscode's sql extension. So in this extension i can find how to create db or table etc. But i can not find how to import an exist db. How can i handle this?
Thanks in advance.
Restoring databases in MS SQL is covered extensively in the documentation and even on SO:
How to: Restore Files and Filegroups (Transact-SQL)
RESTORE Statements (Transact-SQL) - SQL Server
Import .bak file to a database in SQL server
Migrate a SQL Server database from Windows to Linux using backup and restore
If you are using vscode then you are only looking for the SQL syntax/commands to complete the task, so ignore the solutions that use management studio and the UI.
The complicating factor for many in SQL Backup/Restore operations is that the paths that you specify in the SQL commands to perform the operations is relative to the server, NOT to your workstation where you are executing the command from.
So the first step is to copy the backup file to a location that the database engine has file system level access to, then use that path in the SQL scripts.
This is an example:
RESTORE DATABASE YourDB
FROM DISK = '/var/opt/mssql/backup/YourDB.bak'
WITH MOVE 'YourDB' TO '/var/opt/mssql/data/YourDB.mdf',
MOVE 'YourDB_Log' TO '/var/opt/mssql/data/YourDB_Log.ldf'
But read through the docs for the specific restoration sequence and parameters for your needs.
If you are unsure of the current filepaths in use, you can query them from the database:
SELECT
MDF.database_id,
MDF.name,
MDF.physical_name as data_file,
LDF.physical_name as log_file,
db_size_mb = CAST((MDF.size * 8.0)/1024 AS DECIMAL(8,2)),
log_size_mb = CAST((LDF.size * 8.0 / 1024) AS DECIMAL(8,2))
FROM (SELECT * FROM sys.master_files WHERE type_desc = 'ROWS' ) MDF
JOIN (SELECT * FROM sys.master_files WHERE type_desc = 'LOG' ) LDF ON MDF.database_id = LDF.database_id
database_id
name
data_file
log_file
db_size_mb
log_size_mb
1
master
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\master.mdf
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\mastlog.ldf
5.38
2.00
2
tempdev
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\tempdb.mdf
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\templog.ldf
8.00
8.00
3
modeldev
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\model.mdf
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\modellog.ldf
8.00
8.00
4
MSDBData
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\MSDBData.mdf
C:\Program Files\Microsoft SQL Server\MSSQL15.TEST\MSSQL\DATA\MSDBLog.ldf
19.69
28.81
5
MyApp
D:\SQL Server\MyApp.mdf
L:\SQL Server\MyApp_log.ldf
392.00
19912.00

No mapping between account names and security IDs was done - Install SQL Server MDS Workflow Integration Service

I try to create custom workflow by following article in https://learn.microsoft.com/en-us/sql/master-data-services/develop/create-a-custom-workflow-master-data-services.
I try to run InstallUtil Microsoft.MasterDataServices.Workflow.exe, but it failes. I wonder why.
I have specifed as servername\mds_workflow_service user when prompted during installation. I have just for case reset password to make sure that password is correct. The user have read and execute rights to the \bin folder. What could be wrong?
C:\Program Files\Microsoft SQL Server\130\Master Data Services\WebApplication\bin>InstallUtil Microsoft.MasterDataServices.Workflow.exe
Microsoft (R) .NET Framework Installation utility Version 4.7.2053.0
Copyright (C) Microsoft Corporation. All rights reserved.
Running a transacted installation.
Beginning the Install phase of the installation.
See the contents of the log file for the C:\Program Files\Microsoft SQL Server\130\Master Data Services\WebApplication\bin\Microsoft.MasterDataServices.Workflow.exe assembly's progress.
The file is located at C:\Program Files\Microsoft SQL Server\130\Master Data Services\WebApplication\bin\Microsoft.MasterDataServices.Workflow.InstallLog.
Installing assembly 'C:\Program Files\Microsoft SQL Server\130\Master Data Services\WebApplication\bin\Microsoft.MasterDataServices.Workflow.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\Program Files\Microsoft SQL Server\130\Master Data Services\WebApplication\bin\Microsoft.MasterDataServices.Workflow.exe
logfile = C:\Program Files\Microsoft SQL Server\130\Master Data Services\WebApplication\bin\Microsoft.MasterDataServices.Workflow.InstallLog
An exception occurred during the Install phase.
System.ComponentModel.Win32Exception: No mapping between account names and security IDs was done
Solved by enterering service account in format in .\mds_workflow_service

SQL Server database export to another computer

I use SQL Server Managment Studio, SQL Server Express. I gererate a script with schema and data from Task -> Generate Script. And I try to import this script in other computer with the same SQL Server but this giving me error :
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf" failed with the operating system error 3 (The system cannot find the path specified.).
What should I have done to import /export entire database from one PC to another .
If you read through the first few lines of the script, you will see code that specifies the name and location of the files of the database. In your case look for 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf' in your script. Then look at the directory structure of the target machine. You will need to either:
1. create a directory 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\' and make sure the sql instance on the machine has RW access to that directory.
or much better,
2. Modify the script to put the file in a location that the target server wants to put files.
In regards to #2, you can have the target server give you a hint as to where to put files by right-clicking on "Databases" in Microsoft SQL Server Management Studio and create a script to create a database. The directory of the files in the create database script from the target-server should show you what to do.

Copying .mdf file from app_data folder to default localhost folder

My friend gave me a database file: record.mdf. I copied that .mdf file to my app_data folder and I can access it.
However, the connection string contains absolute path:
AttachDbFilename="C:\Users\Dell\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\App_Data\record.mdf"
But I want it to connect using:
Data Source=localhost\SQLEXPRESS;
How do I copy .mdf file to SQL Server's local folder, so that the connection string does not use an absolute path to the database?
I am using Visual Studio 2010. I do not have SQL Server Management Studio.
Step 1: you need to find out your SQL Server's data directory. This will be something like
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data
by default (for SQL Server 2008 R2 Express) - it might be slightly different in your case, depending on how you installed your SQL Server Express (and which version you have).
Step 2: copy that record.mdf file to that directory
Step 3: attach it to your SQL Server Express instance - using sqlcmd if you don't have Mgmt Studio at hand:
c:\> sqlcmd -S .\SQLExpress
Then at the sqlcmd prompt, type in:
USE [master]
GO
CREATE DATABASE record ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\record.mdf' )
FOR ATTACH_REBUILD_LOG;
GO
This will attach the .mdf file as your new "logical" database record to your SQL Server Express instance, and it will rebuild the missing transaction log file (.ldf) in the process.
From now on, you can use
server=.\SQLEXPRESS;Database=record;Integrated Security=SSPI;
as your connection string to connect to your database
Rather than copying it to the SQL server local folder, you can access it from the App_Data directory using |DataDirectory|\record.mdf
Documentation: http://msdn.microsoft.com/en-us/library/ms247257(v=vs.80).aspx

Resources