Jul 29 2008

SQL 2005 Mirroring – Automatic Failover

This post is a follow on from a previous post ‘Complete Guide to SQL 2005 mirroring using the GUI

Now we know when Mirroring in the ‘High safety with automatic failover’ mode it will automatic failover, which works superbly, but a few questions for you, are you using SQL authentication for you databases? have you tried the failover and then tried to call your app through the failover server now as the principle server? Do you manually change your .config or use a script to do a find and replace if your web application is using a connection string?

 

If you answered

Yes to SQL authentication
No to trying the failover
Yes to manually change the connection string

this post if for you read on…

Lets start with the connection string, now hopefully your application is using .NET 2.0 or above, if it is you will be very happy to know you don’t have to script anything for when a server dies and your mirrored database takes over, for those using 1.1 or below UPGRADE, but if you cant well I under stand, you might want to read another post about a find and replace tool I found that will make your scripting life much easier for the failover it can be found here, anyway back to the .NET 2.0 users, if you are using a mirror then you will be very happy Microsoft have included some additional functionality within the connection string, where you can specify a failover partner, this will allow you to enter the failover’s server name or IP and once the principle mirror has failed you need not worry if you have the failover partner string within the connection string, it will detect this and failover immediately.

Here is an example:

Server=MyPrincipleDatabaseServer; Failover Partner=MyFailoverDatabaseServer;; Database=AdventureWorks;

it works perfectly, try it, you will be amazed.

find more info on the above here

 

Ok so manually change the connection string….. Check

now moving along to SQL authentication

If you use SQL authentication and lets say you take a backup of database and then restore it to another SQL server, what one thing that you will always have to do….?

give up… You will have to remove the user and then re-add the user to the database, why because the SID (security Identifier) is different on each SQL server, the same applies to mirroring, when you failover even though you are using the same user and password for the SQL authentication, the app / database will fail to work because once again just like a restore you will have to delete the user and then re-create it, one massive pain in the but, especially if you want automatic failover and then it does failover in the middle of the night, but yet all your databases still fail, now wouldn’t that just be a kick in the face.

However we have an easy fix for that, all you need to do is make the failover database SID the same as the principles SID for that user, or all users that you are using for the mirrored databases.

So first thing is getting the SID of the principle SQL server.

use the below script: (use master for the query)

SELECT 
'create login [' + p.name + '] ' + 
case when p.type in('U','G') then 'from windows ' else '' end +
'with ' +
case when p.type = 'S' then 'password = ' + master.sys.fn_varbintohexstr(l.password_hash) + ' hashed, ' +
'sid = ' + master.sys.fn_varbintohexstr(l.sid) + ', check_expiration = ' +
case when l.is_policy_checked > 0 then 'ON, ' else 'OFF, ' end + 'check_policy = ' + case when l.is_expiration_checked > 0 then 'ON, ' else 'OFF, ' end +
case when l.credential_id > 0 then 'credential = ' + c.name + ', ' else '' end 
else ''<
/span> end +
'default_database = ' + p.default_database_name +
case when len(p.default_language_name) > 0 then ', default_language = ' + p.default_language_name else '' end
 
FROM sys.server_principals p
LEFT JOIN sys.sql_logins l
ON p.principal_id = l.principal_id
LEFT JOIN sys.credentials c
ON  l.credential_id = c.credential_id
WHERE p.type in('S','U','G')
AND p.name <> 'sa'

once run you should see something like (you would probably have a heck of a lot more users)

image

Ok so find the user you want to replicate the SID on your Failover, for this example I am using test_user

The below is what I have pulled out directly from the above query for my Test_user

create login [test_user] with password = 0×01004981172537214b13a072a6674d0316673b48cbdadd184d3b hashed, sid = 0×6f7bd56e4f79ab4d89247b79180db390, check_expiration = OFF, check_policy = OFF, default_database = master, default_language = us_english

So now I want to copy user above and take its SID and re-create the user on my fail over so I would now go to the failover database and then run 


Use Master

create login [test_user] with password = 'PasswordGoesHere', sid = 0x6f7bd56e4f79ab4d89247b79180db390, check_expiration = OFF, check_policy = OFF, default_database = master, default_language = us_english

REMEMBER: to remove the hashed password replace it with a clear text password, I also advise to keep the password expiration off, other wise you will find yourself doing this task again and again, or worse yet forget about it and then when the SQL server fails, so does the mirror :(

image

Now that that is done time for that testing you answered No to, so lets start the test:

(I do suggest you try this on a dev environment before you do this on production)

go on to the primary and then tasks > Mirror > click on Failover

image

your failover I should now failover quickly and smoothly, without any manual intervention.

Now just one last thing that might be helpful, if you are running a mirror and your failover server is not the same spec as your Primary SQL server, or you just don’t want the databases to run on the failover server until the next failover, then you can run a TSQL script to make them go back to the primary database server:

Use master

ALTER DATABASE DatabaseNameHere SET PARTNER FAILOVER

go

Repeat this for each database, if you have hundreds of databases, do a SP_database and then copy to excel and then save to CSV and the play with it that way.


Jul 24 2008

ASP.NET tab forces restart of IIS

This has been a pain in the back side for some time (well at least for me), whilst there are few scripts out their that clam to solve this problem, I have personally found this is not true there is still IIS restarts.

And sure you could directly go and edit the metabase.xml, But that leaves to many rooms for mistakes, and you can also go and change the version of  .Net by going into the application configuration within IIS and then manually change the version their.

image

image

Both above ways will change the .NET without a restart, but that’s just too much work, I stumbled across Jerry Orman Blog post on this very subject

Jerry Orman : ASP.NET tab forces restart of W3SVC

Jerry has created a very nifty VBS to over come this pain in the but IIS restart, and it works

Cheers Jerry.


Jul 18 2008

Which web site belongs to the w3wp process

 

Really the better way of saying it is which ‘application pool’ belongs to the w3wp process, anyway there are a few ways to get the app pool names for each PID.

Microsoft were nice enough to provide a VBS that will display this information for us, you can find this in the system32 folder of your 2003 server – ‘IISapp.vbs’

I wrote a 2 second batch file that will get the PID’s and the app pools for you

 

   1: REM: this will find all of the W3WP PID against each app pool
   2: REM: enter to continue
   3: Pause
   4: c:
   5: cd windows
   6: cd system32
   7: iisapp.vbs 
   8:  
   9: Pause

Read more about this below:

Which w3wp.exe process belongs to which App Pool in IIS6 – Scott Forsyth’s Blog


Jul 16 2008

CRM 4: Event Log MSCRMKeyGenerator

Your here because you got an CRM 4 error ‘unable to load’

Below the event log you should be seeing:

Event Type:    Error
Event Source:    MSCRMKeyGenerator
Event Category:    None
Event ID:    18949
Date:        16/07/2008
Time:        10:47:26 AM
User:        N/A
Computer:    ETDC2
Description:
Current active key (KeyType : CrmWRPCTokenKey) is expired.  This can indicate that a key is not being regenerated properly.  Current Active Key : CrmKey(Id:7ca9e53d-db38-dd11-9398-003048781151, ScaleGroupId:00000000-0000-0000-0000-000000000000, KeyType:CrmWRPCTokenKey, Expired:True, ValidOn:06/12/2008 23:56:58, ExpiresOn:07/15/2008 23:56:58, CreatedOn:06/12/2008 23:56:58, CreatedBy:NT AUTHORITY\NETWORK SERVICE.  Key Setting :

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

 

easy fix

Start the CRM Asynchronous service as below:

image


Jul 9 2008

Complete Guide to SQL 2005 mirroring using the GUI

Well it has occurred to me that there are no real guides to setting up mirroring that deal with what errors you could, or more like you will run into whilst setting up a mirror.

 

Ok for this example I am going to setup a ‘High safety with automatic failover’ this particular mirror uses a Principle server, failover server and a witness server, so for this example you will need 3 servers (you could get a way with 2 as the witness could be the failover, but best to have 3)

So first thing is first the prerequisites.

3 SQL 2005 server’s

Service Packs installed
at least SP1 applied, if this is not applied you will get an error saying that this feature is not enabled, you can enable this feature, however I recommend rather then playing with the startup values you patch your MSSQL server to the current SP for SQL.

MSSQL Service
You must have all three SQL servers using an account that has network access, by default most of us will have used the local system account, which in this case is not going to work, you will get this obscure error message when trying to start the mirror.

image

 All databases in FULL recovery Mode
both the mirror and the failover database both need to be in a Full recovery mode, if not you will get the below error

‘the database cannot be mirrored because it does not use the full recovery model’

image

If you get this, right click on your database and go to ‘properties’ then to ‘options’ you can change the mode their.

 

OK, Now your ready to start getting the mirror together

first thing is first, take a full backup of your database that you want to start mirroring, move this backup to a share or somewhere that your failover server can restore the database from.

Now using the same name of the database on the Principle server, create a database, then restore the database backup, make sure when restoring the database you are using the NORECOVERY method

You could use this TSQL to create and complete your database restore:

restore databasename from disk backup location and name with replace, NORECOVERY

example:

restore mydatabase from disk d:\SQL_backups\mydatabase.bak with replace, NORECOVERY

Ok now you should have a database on your failover server and it should be in a ‘restoring’ state, if you are not seeing this then refresh your databases, if you still don’t see then then you probably missed the NORECOVERY part, go back and do this again with NORECOVERY applied.

we are halfway there, all you need to do now is again create another backup on the principle server, but this time it must be a transactional backup. So once again create a new transactional backup, place the backup in a place where the fail over server can restore the database from, and then perform your restore again using the NORECOVERY method.

or do this from TSQL

restore mydatabase from disk  d:\SQL_backups\mydatabaseTRANSACTIONAL.TRN with NORECOVERY

Once again you will have a database that should be still in a restoring state.

If this step is missed or you restore an older TRN log file, then you can expect to get the following error when trying to create the mirror :

‘the mirror database …., has insufficient transaction log data to preserver log backup chain of the principle  database.

image

Now we are ready to start the mirroring!
right click on the principle database mirror, choose tasks followed by Mirror

image

You will be prompted with the following:

Click on security in the top right side

image 

Yes we want to include a witness server, (in this case we are doing the high availability so we need a witness server for it to work)

 image

Make sure all three are ticked

image

This is where we get to set the port that the mirroring is going to be using to connect to each other, and the endpoint name. I have accepted defaults, but before excepting default ports ensure that all of your servers can talk to each other throughout that port best way to test would be

telnet servername 5022

do this for all combinations so from:

principle to witness
principle to fail over

failover to principle
failover to witness

witness to Failover
witness to principle

If you can make successful connections to each then you are good to go.

image

Once you have run through the 3 database servers you will be prompted with the below screen:

Here if they are all on the same network and using integrated (AD) authentication which recommenced) then you may skip this, otherwise you will be required to enter in valid credentials

image

You should come to this screen after the review screen, if you have failed on one of the servers, then read the error and correct the problem (these errors are generally helpful ones :) )

image

 

Now start you mirror!

 

Other problems you may run into:

If you tried to start your mirror, and you get an error ‘one or more of the servers network addresses lacks a fully qualified domain name’  that’s an easy one, you must edit the server so it has a FQDN like server.domain.com if your DNS is being painful add it to the host file of the server you are setting the mirror up on.

image

 

That should be enough to get your mirror up and runn
ing

:)

 

Stay tuned more on mirroring coming soon

as promised a new more in depth look auto- failover of SQL Mirroring, it can be found here, SQL 2005 Mirroring – Automatic Failover


Jul 8 2008

Windows Command Line Find and replace – Made easy

I finally found a great little application that does a find and replace on a particular file or file type, then replacing it with a string, of your choice. It also looks in sub directory, whilst there are many grep’s out there most are GUI for windows or pay for items that run command line… I like free better, don’t you?

And yes SED could do it, as could a few other apps, but why write something when this  can be done so much easier?

the small app is called FART, yes that’s right FART!

Here are the options FART provides

Find And Replace Text  v1.99b                         by Lionello Lunesu

Usage: FART [options] [--] <wildcard>[,...] [find_string] [replace_string]

Options:
-h, –help          Show this help message (ignores other options)
-q, –quiet         Suppress output to stdio / stderr
-V, –verbose       Show more information
-r, –recursive     Process sub-folders recursively
-c, –count         Only show filenames, match counts and totals
-i, –ignore-case   Case insensitive text comparison
-v, –invert        Print lines NOT containing the find string
-n, –line-number   Print line number before each line (1-based)
-w, –word          Match whole word (uses C syntax, like grep)
-f, –filename      Find (and replace) filename instead of contents
-B, –binary        Also search (and replace) in binary files (CAUTION)
-C, –c-style       Allow C-style extended characters (\xFF\t\n\r\\ etc.)
     –cvs           Skip cvs dirs; execute “cvs edit” before changing files
     –svn           Skip svn dirs
     –remove        Remove all occurences of the find_string
-a, –adapt         Adapt the case of replace_string to found string
-b, –backup        Make a backup of each changed file
-p, –preview       Do not change the files but print the changes

 

As an example

fart -c -r -i -p *.config databaseserver1 databaseserver2

this will look for all config files in a sub directory then looking for the databaseserver1 string within the .config file and then changing it to databaseserver2, If you notice i have a -p which means it wont actually change anything because this is a preview, showing you how many strings it found within each .config file.

Download FART here

http://sourceforge.net/projects/fart-it/


Jul 6 2008

Could the HTC Diamond and Touch Pro be launched soon?

Well I certainly hope so this has be by far the best styled and feature rich windows mobile devices that have ever been launched….

I was reading on a blog that HTC released an email (if you are on there mailing list you would have also got it too) that was  an invite to a product launch on the 24th of July!! whilst the invite does not specify what product it is, but in saying that what else do they have that’s not yet been released…. NOTHING.

see below for more info on the invite

http://www.techau.tv/blog/?p=479

but back to the features list

now For the

HTC Diamond

Processor
Qualcomm® MSM7201A™ 528 MHz

Operating System
Windows Mobile® 6.1 Professional

Memory
ROM: 256 MB
RAM: 192 MB DDR SDRAM
Internal storage: 4 GB

Dimensions
102 mm (L) X 51 mm (W) X 11.35 mm (T)

Weight
110 g (with battery)

Display
2.8-inch TFT-LCD flat touch-sensitive screen with VGA resolution

Network
HSDPA/WCDMA:

  • Europe/Asia: 900/2100 MHz

  • Up to 384 kbps up-link and 7.2 Mbps down-link speeds

Tri-band GSM/GPRS/EDGE:

  • Europe/Asia: 900/1800/1900 MHz

(Band frequency and data speed are operator dependent.)

Device Control
TouchFLO™ 3D
Touch-sensitive navigation control

GPS
GPS and A-GPS ready

Connectivity
Bluetooth® 2.0 with EDR
Wi-Fi®: IEEE 802.11 b/g
HTC ExtUSB™ (11-pin mini-USB 2.0 and audio jack in one)

Camera
Main camera: 3.2 megapixel color camera with auto focus
Second camera: VGA CMOS color camera

Audio
Built-in microphone, speaker and FM radio with RDS
Ring tone supported formats:

  • MP3, AAC, AAC+, WMA, WAV, and AMR-NB

  • 40 polyphonic and Standard MIDI format 0 and 1 (SMF)/SP MIDI

Battery
Rechargeable Lithium-ion or Lithium-ion polymer battery
Capacity: 900 mAh
Talk time:

  • Up to 270 minutes for WCDMA

  • Up to 330 minutes for GSM

Standby time:

  • Up to 396 hours for WCDMA

  • Up to 285 hours for GSM

Video call time: Up to 145 minutes for WCDMA
(The above are subject to network and phone usage.)

 

 

HTC Touch Pro

Which is what I AM VERY excited about, in my opinion this has it all over the I-Phone.

Processor
Qualcomm® MSM7201A™ 528 MHz

Operating System
Windows Mobile® 6.1 Professional

Memory
ROM: 512 MB
RAM: 288MB

Dimensions
102 mm (L) X 51 mm (W) X 18.05 mm (T)

Weight
165 g (with battery)

Display
2.8-inch TFT-LCD flat touch-sensitive screen with VGA resolution

Network
HSDPA/WCDMA:

  • Europe/Asia: 900/2100 MHz

  • Up to 384 kbps up-link and 7.2 Mbps down-link speeds

Tri-band GSM/GPRS/EDGE:

  • Europe/Asia: 900/1800/1900 MHz

frequency and data speed are operator dependent.)

Device Control
TouchFLO™

(Band  3D
Touch-sensitive navigation control

Keyboard
Slide-out 5-row QWERTY keyboard

GPS
GPS and A-GPS ready

Connectivity
Bluetooth® 2.0 with EDR
Wi-Fi®: IEEE 802.11 b/g
HTC ExtUSB™ (11-pin mini-USB 2.0, audio jack, and TV Out* in one)

Camera

Main camera: 3.2 megapixel color camera with auto focus and flash light
Second camera: VGA CMOS color camera

Audio
Built-in microphone, speaker and FM radio with RDS
Ring tone supported formats:

  • MP3, AAC, AAC+, WMA, WAV, and AMR-NB

  • 40 polyphonic and standard MIDI format 0 and 1 (SMF)/SP MIDI

Battery
Rechargeable Lithium-ion or Lithium-ion polymer battery
Capacity: 1340 mAh
Talk time:

  • Up to 397 minutes for WCDMA

  • Up to 485 minutes for GSM

Standby time:

  • Up to 503 hours for WCDMA

  • Up to 406 hours for GSM

Video call time: Up to 201 minutes for WCDMA
(The above are subject to network and phone usage.)

Expansion Slot
microSD™ memory card (SD 2.0 compatible)

 

Remember to book in the 24th of July 08, I know I have.

:)