SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Attaching Content DB's in MOSS/WSS3
Matt Groves Blog

News

  • <head> <style type="text/css"> .style1 { text-decoration: line-through; } </style> </head> Matt Groves
    Matt Groves- blogger!
    In my professional life: I am a Strategic Consultant with an MS Gold Partner in the UK, specialising in helping clients maximise benefit from effective use of technology, thought leadership and solution architecture. I am passionate about "Web2.0" and using social media in the enterprise. The Microsoft stack is of primary interest and I am focussed on the IW platforms (SharePoint, OCS, Office, Exchange, etc) I used to programme program code. Also interested in Federation and Virtualisation.

    Often heard saying:
    • "It depends..."
    • "Fascinating..."
    • "That's fixed in the next version..."
    • "Tea, black with one, please..."


    View Matt Groves's profile on LinkedIn

    View Matts profile on FaceBook

    picasalogo 


    In my personal life: I enjoy spending time with my family (I have 2 small children) and have a wide range of hobbies that I no longer have much time for including Fishing, Golf, DIY and Photography...

      <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script> <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/mattgroves.json?callback=twitterCallback2&amp;count=5"></script>

    After completing your capacity planning excercise prior to deploying MOSS/WSS3 you will have the number of content DB's you are going to start with (a very different proposition to working out the number of content DB's you'll end up with!). For the sake of the article let's say the number came out at 22 per WebApp.

    So, you can either create 21 DB's manually (remember, you already have one) and then assign them manually, which will be about 90 seconds to create each DB in SQL, plus around 60 seconds to attach each DB to the WebApp, so it will take (1+1.5)*21 for each WebApp, totalling 52.5 minutes of repetative boredom (unless you have a placement student or intern to hand!)...

    So, partly because I'm lazy, partly because a hate repetative tasks, but mainly because I like scripting, I thought I'd write some scripts to do all this for me...


    The SQL script

    This script will create you the additional 21 content DB's, it assumes your first content DB already exists and is called SharePoint_ContentDB_01, simply copy-paste into SQL Query Analyzer... (Thanks to Steve Maxwell for this)

    USE [master]

    GO

    DECLARE

    @DBName nVARCHAR(255)

    , @Number nVARCHAR(4)

    , @DataFileName nVARCHAR(255)

    , @LogFileName nVARCHAR(255)

    , @DataFile nVARCHAR(255)

    , @LogFile nVARCHAR(255)

    , @SQL nVARCHAR(MAX)

    , @ServiceIdentity nVARCHAR(255)

    , @AppPoolIdentity nVARCHAR(255)

    SELECT

    @Number = '02'

    /* set these to the correct values for you environment */

    , @ServiceIdentity = '<SERVICE Account>'

    , @AppPoolIdentity = '<AppPool Account>'

    WHILE @Number <= 22

    BEGIN

    SET @DBName = 'SharePoint_ContentDB_'+@Number

    SELECT

    @DataFileName = @DBName

    , @LogFileName = @DBName + '_log'

    /* set the following paths to the correct locations in your environment */

    , @DataFile = 'M:\Microsoft SQL Server\MSSQL.1\MSSQL\Data\' + @DBName + '.mdf'

    , @LogFile = 'L:\Microsoft SQL Server\MSSQL.1\MSSQL\Data\' + @DBName + '_log.LDF'

    SET @SQL = 'CREATE DATABASE ' + @DBName + '

    ON PRIMARY

    (NAME = ''' + @DBName + '''

    , FILENAME = ''' + @DataFile + '''

    /*set the following pre-size and growth values to whatever suits your needs - this helps ensure data file are contiguous*/

    , SIZE = 10240000KB

    , MAXSIZE = UNLIMITED

    , FILEGROWTH = 5120000KB)

    LOG ON

    (NAME = ''' + @LogFileName + '''

    , FILENAME = ''' + @LogFile + '''

    /*set the following pre-size and growth values to whatever suits your needs - this helps ensure log file are contiguous*/

    , SIZE = 2048000KB

    , MAXSIZE = UNLIMITED

    , FILEGROWTH = 1024000KB)

    COLLATE Latin1_General_CI_AS_KS_WS'

    EXEC (@SQL)

    EXEC dbo.sp_dbcmptlevel @dbname=@DBName, @new_cmptlevel=90

    EXEC [SharePoint_ContentDB_01].[dbo].[sp_fulltext_database] @action = 'enable'

    SET @SQL = 'USE '+@DBName+';EXEC sp_changedbowner '''+@ServiceIdentity+''''

    EXEC (@SQL)

    SET @SQL = 'USE '+@DBName+';CREATE USER ['+@AppPoolIdentity+'] FOR LOGIN ['+@AppPoolIdentity+']'

    EXEC (@SQL)

    SET @SQL = 'USE '+@DBName+';EXEC sp_addrolemember ''db_owner'', '''+@AppPoolIdentity+''''

    EXEC (@SQL)

    SET @Number = @Number + 1

    SET @Number = RIGHT('00' + @Number,2)

    END


    The VB

    Place this script in C:\Add_ContentDB_Script\
    It will create a batch file called AddDBAContentDB.bat which must be run in order to attach the content DB’s to the web application. The batch file must NOT exist already as no checks to find an existing file are performed.

    The variables

    The script must be changed to suit the environment and the following variables edited:

    SQLServ = SQL Server/Instance where the DB’s have been created (eg; MOSSSQLCLU01)
    DBnameconv = The naming convention used, without number. (eg; SharePoint_contentDB_)
    DBnumberEnd = The number of DB’s plus one (if 22 DB’s have been created use 23)
    DBnumberStart = The first DB not already attached to the MOSS/WSS site collection (eg; 02)
    SiteURL = The NLB URL of the MOSS/WSS WebApp to attach the DB’s (eg; http://web.archive.org/web/20070319224126/http://intranet.domain.com/)
    SiteLimit = The maximum number of sites allowed in the DB
    SiteWarn = The number at which a warning is generated

    The Script

    dim SQLServ

    Dim DBnameconv

    Dim DBnumberEnd

    Dim DBnumberStart

    Dim SiteURL

    Dim SiteLimit, SiteWarn

    'set the parameters used here

    SQLServ = "MOSSSQLCLU01"

    DBnameconv = "SharePoint_contentDB_"

    DBnumberEnd = 23

    DBnumberStart = 2

    SiteURL = "http://intranet.domain.com"

    SiteLimit = 50

    SiteWarn = 40

    Dim ObjFile, objExp

    Dim strPath, strFile, strFilePath, strMyFile

    ' ste the path to use

    strPath = "C:\Add_ContentDB_Script\"

    strFile = "AddDBAContentDB.bat"

    strFilePath = strPath & strFile

    ' FSO creates the object called - ObjFile

    ' no validation here = file cannot already exist!!!

    Set objFile = CreateObject("Scripting.FileSystemObject")

    Set strMyFile = objFile.CreateTextFile(strFilePath, True)

    strMyFile.WriteLine("cd\")

    ' write the header

    strMyFile.WriteLine("cd ""Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\""")

    DBnumberCurrent = DBnumberStart

    'start the loop

    do while DBnumberCurrent < DBnumberEnd

    ' .WriteLine to add data to each line in the file

    if DBnumberCurrent < 10 then

    strmyFile.WriteLine("STSADM -o addcontentdb -url " & SiteURL & " -databasename " & DBnameconv & "0" & DBnumberCurrent & " -databaseserver " & SQLServ & " -sitewarning " & SiteWarn & " -sitemax " & SiteLimit)

    Else

    strMyFile.WriteLine("STSADM -o addcontentdb -url " & SiteURL & " -databasename " & DBnameconv & DBnumberCurrent & " -databaseserver " & SQLServ & " -sitewarning " & SiteWarn & " -sitemax " & SiteLimit)

    End If

    DBnumberCurrent = (DBnumberCurrent + 1)

    Loop

    'done!

    strMyFile.Close

    WScript.Quit


    The batch file

    The file created can then be run (you'll need to be a farm admin to do this bit)...

    http://www.sharepointblogs.com/photos/mattgroves/images/21136/original.aspx


    You'll then have your 22 content DB's attached to your WebApp. Happy Days!

     


    Posted 03-15-2007 10:14 PM by Matt Groves

    Comments

    Matt Groves - SharePoint blogger. wrote Blog posts and articles slowly coming back...
    on 07-01-2007 4:19 PM

    Just started the process of restoring my blog content, will be a while before it&#39;s all back... For

    stop collaborate and listen wrote Creating & Attaching Content DB's - the Lazy Man's Way
    on 01-30-2008 4:16 AM

    A very useful article on how to create and attach content databases to your MOSS web application with

    Matt Groves - SharePoint blogger. wrote How to: create and attach multiple content databases in MOSS/WSS3
    on 03-11-2008 8:59 AM

    Just added an article on creating and attaching multiple content databases to a MOSS/WSS3 WebApp, all

    Matt Groves - SharePoint blogger. wrote Strange rankings in Google...
    on 03-11-2008 9:08 AM

    A colleague has just pointed out that if you google me (string: matt groves) my blog comes up first

    naspinski wrote Quickly add and attach multiple content databases to a MOSS 2007 install
    on 05-06-2008 1:11 AM

    Quickly add and attach multiple content databases to a MOSS 2007 install

    Add a Comment

    (required)  
    (optional)
    (required)  
    Remember Me?
    Need SharePoint Training? Attend a SharePoint Bootcamp!
    Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.