Blog Post

TIL: Dismount-DbaDatabase and Mount-DbaDatabase

,

We have a double feature for today’s dbatools blog post, as these two commands go hand-in-hand.

Todays commands: Dismount-DbaDatabase and Mount-DbaDatabase

Dismount-DbaDatabase `
    -SqlInstance PRECISION-M7520SQL2016 `
    -Database 'test'
Mount-DbaDatabase `
    -SqlInstance PRECISION-M7520SQL2016 `
    -Database 'test'

Detach-DbaDatabase and Attach-DbaDatabase can be used as aliases. For the purpose of this blog i’m using the official names, because reasons™

Note that the .mdf and .ldf files are still present, meaning detaching a database can keep you from deleting a database you were told wasn’t needed anymore, only to instantly have someone ask why they can’t get to the database they need. If the files are still there, you can attach it and it’s good as new.

Dismount-DbaDatabase `
    -SqlInstance PRECISION-M7520SQL2016 `
    -Database 'test' `
    -Force

If the database is part of a mirror or something, -Force can be used to detach it anyways.


Dismount-DbaDatabase `
    -SqlInstance PRECISION-M7520SQL2016 `
    -Database 'test' `
    -UpdateStatistics

This will update the statistics of the database before it’s detached.


$testAttach = New-Object System.Collections.Specialized.StringCollection
$testAttach.Add("C:Program FilesMicrosoft SQL ServerMSSQL13.SQL2016MSSQLDATAtest.mdf")
$testAttach.Add("C:Program FilesMicrosoft SQL ServerMSSQL13.SQL2016MSSQLDATAtest_log.ldf")
Mount-DbaDatabase `
    -SqlInstance PRECISION-M7520SQL2016 `
    -Database 'test' `
    -FileStructure $testAttach

The -FileStructure parameter can be used to point out where the files to attach are. If not specified, it’ll use backup history to figure out what you mean, if possible.


Mount-DbaDatabase `
    -SqlInstance PRECISION-M7520SQL2016 `
    -Database 'test' `
    -DatabaseOwner 'sa'

The -DatabaseOwner parameter lets you set the, well, database owner, when you attach the database. Defaults to sa.


Dismount-DbaDatabase `
    -SqlInstance PRECISION-M7520SQL2016 `
    -Database 'test', 'square[]brackets'

The -Database parameter can take multiple values.

That’s all i got for this one, not too complicated unless you’re doing something with credentials and stuff. Follow me (b|t) on twitter!

Previous Dbatools posts:

Backup-DbaDatabase

Restore-DbaDatabase

Get-DbaLastBackup

Get-DbaAgentJobHistory

Measure-DbaBackupThroughput

Get-DbaErrorLog

Future Dbatools posts:

None yet!

The post TIL: Dismount-DbaDatabase and Mount-DbaDatabase appeared first on DallasDBAs.com.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating