Friday, July 11, 2014

To find out which domain controller you are authenticated to on ther server ?

I was helping my friend in finance to check out some issue in test environment, and of course, which starts as "hey, we got some database issue since we can't connect to to backend...". Great pick up line as usual.

So my friend shows me his database issue, and I can point it out straightaway that it is an authentication plus DNS issue rather than database. It rings the bell that my OP team friend told me about a project that they are looking at upgrading the domain controllers and it is reasonable that they are doing a rehearsal for the big date.

There is 2 active directory servers online and I am not sure which one the application server is authenticated from. Well, I found the following handy environment variable for just the job:

Thursday, July 03, 2014

Start / Stop a service at remote Windows server

There is an useful Windows command I learn recently which I thought why I did come across this earlier. It helps a lot when it needs to stop a service on an remote app server prior to run a database script.

The command is sc.

Task Command syntax
To stop a Service sc \\{server} stop {service}
To start a Service sc \\{server} start {service}
To restart a Service sc \\{server} restart {service}

Event ID to identify a system shutdown, startup, reboot and System uptime

How many times that you are surprised by automated Windows patching task which restarts your database server in the middle of a job runs? Of course, you should discuss that with you system admin team to work out how to schedule and deal with Windows patching on database servers. To start a discussion like that, you always need to find if your server does restart in the first place and what time and by who/what.

So, what are you looking for? In the event viewer, you can filter the system logs with the following Event IDs as explain below:

Event ID Description
6005 / 6009 OS started
6006 OS shutdown
6013 OS daily update report. that's the OS has been running for more than a day, measure in seconds.
1074 This sweet event shows you which process and on behalf of which user a reboot was initiated.

Saturday, October 12, 2013

Log shipping 'Could not find a log backup file that could be applied to secondary database'

"I hate Friday and Monday."

Statistically, these 2 dates have demonstrated a much higher chance of system failure while compares to the rest of the other weekdays. Needless to mention that a majority of PMs love to ask for additional enhancement on Friday, in between 3pm and 5pm...

It was started at Thursday night, and I have a mini change to deploy. While I found that I can't remote in with some weird system error, my heart can sense that something had gone wrong. Unfortunately, data center is too far away to me, so there is nothing I can do.

Next day, reading the email from the operation team sent at late 4am. The team had worked around the clock to turn around the situation. They are later told that building maintenance was conducted some work and an "unforeseeable" event took place, and as a result air conditioning's power got cut off. Server room "virtually moved" to Africa with a temperature at high 40°C plus, servers shut itself down to survive the heat wave. Everything comes back online. A few issue here and there, but we can claim that we make it through. Check my SQL servers, most of the servers and databases are fine... except one pair of servers their log shipping is not working on all databases. After all my work is done to recover these log shipping, I think this is rather a rare scenario that a DBA can face. Given that, I really wanted to share here and hope it may help someone else who hates Friday like me.

Back to the topic. In general, log shipping would recover from most of the unexpected outage event very well. Usually, it just needs to catch up. Since we all know that the log backup are done in the order of transaction, and so does the restore job will only restore each file in the same order to ensure proper roll forward, there is really not much room for anything to go wrong. And if restore failed, one would assume that there could be a log backup file gone missing or corrupted. At worst, it could simply fix it by recreating the log shipping from scratch. In my case, I can't. I have a bunch of large SharePoint content databases and it will incur a big deal work and time to reinitialize them one by one.

Through investigation, what I found is:

  1. The log backup job and log copy job keep going without an issue, but the log restore job is just stuck and claims that it could not restore a log which is too recent, with a error message similar to this:
    • Error: The file 'S:\DEFAULT INSTANCE\LogShip\WHATEVERDB_20131010094501.trn is too recent to apply to the secondary database 'WHATEVERDB'.(Microsoft.SqlServer.Management.LogShipping)
    • Error: The log in this backup set begins at LSN 813434000023423100001, which is too recent to apply to the database. An earlier log backup that includes LSN 813226000014464300001 can be restored. Searching for an older log backup file. Secondary Database: 'WHATEVERDB'
    • Error: Could not find a log backup file that could be applied to secondary database 'WHATEVERDB'.(Microsoft.SqlServer.Management.LogShipping)
  2. I checked the list of log backup files and found that they are identical on both the primary and secondary server. I even delete those on the secondary and copy them over again. Therefore, I didn't miss any log backup that need to be restored. Still, restore job stuck at a single log backup file.
  3. By testing to manually restore the log backups, I can cross out the possibility that if the log backup is corrupted, as I believed that if the failure resulted of a corrupted log backup, the error message would be different.
  4. I noted that the system time on the server was gone strange for a short while, but it seems normal when I checked again.

I did some research and I found that there are 2 system tables in msdb, dbo.log_shipping_secondary and dbo.log_shipping_secondary_databases, can help to identify the last log backup file that it restored at the secondary database, Put them together in a query like the following:

So, I identified the last restored log backup and start to work from that point. I disabled the copy and restore jobs, then I tried to manually restore each log backup individually to the database. This is the list of files I got at the time:

First, I attempted to restore the trn file#1, which is done successfully. Next, I have a go with the trn file#2, and it complaint that it is too recent to apply to the database, same as reported from the restore job. Thought I have nothing to lose, so I tried to restore the trn file#3. I was expected a "too recent to apply" error again, however it surprises me with successfully done!! WTH? So, I retry on trn file#2, and it reports it's done successfully as well. After this point, I can restore in the normal order. So all I have done in order to fix it is to swap the order of restoring the trn file#2 and trn file#3. Strange, isn't it?

So, what is the "Lesson Learnt" here? I give it a thought on why it could be happened in the first place. Log backup file are named as [dbname_yyyymmddhhmiss.trn] format and log shipping job run based on the order of how the filename sorts in ascend. In our case, something gone wrong with the order when the 2 log backup files were written and named. Sounds non-sense, but it is possible. Remember, I have mentioned that the system time has gone strange for a short time. My guess is that time was incorrect on the server, and the time service doesn't able to correct it when the file was written. Therefore, when the log backup job first kicks in with a wrong system time, it named the file incorrectly. and once the time service fixed the time, you may found that SQL server would miss the right file because of the incorrect file sorting. That would explain why all the database on the server suffers the same symptom.

So, next time before you restart the log shipping from scratch with no choice but in pain, try to check the current situation. Sometimes, you may found that it is not as bad as you thought...

Monday, September 30, 2013

SQL Server 2008 R2 SSMS failed to install and complaining about Visual Studio 2008 SP1 should be installed

Recently, I have a developer asked me to help to install management studio on her laptop. Sure! How hard could it be? So I start as usual and installing the SSMS from the installation disc image.

In the middle of the installation, it stops. With the following error:

Given that, who on earth would be still using Visual Studio 2008!? Everyone moved forward to Visual Studio 2010 already. Something does make sense here... So, Yay!! It~~ is~~~ Google time!!

Did some research. This is, in fact, quite an old issue dated back to 2011 from most of the post I read. Note that most of the post reported that they haven't installed Visual Studio 2008 on their machine. Anyway, the solution is involved with registry key change, so make sure you have extract the registry for rollback purpose.

Firstly, you need to determine if you have a 32-bit or 64-bit OS. That will determine if you need to carry on with an additional step.

Next, let's check the following registry keys with the regedit. Make sure that the SP and SPIndex values are "1" and that the SPName is "SP1":

  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VC\Servicing\9.0
  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VC\Servicing\9.0\RED\1033

Now, if you have a 64-bit OS, this is extra that you need to go further. Similar deal, make sure that the SP and SPIndex values are "1" and that the SPName is "SP1":

  • HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\DevDiv\VS\Servicing\9.0
  • HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\DevDiv\VS\Servicing\9.0\VSR\1033

After all of this, just restart the installation again and this time the installation should be able to be completed. To credit the solution, here is the URL for reference: SQL Server 2008 R2 Management Studio install fails telling me to install Visual Studio 2008 SP1!. Thanks, Eric!!

Wednesday, September 25, 2013

Testing query permission with EXECUTE AS command

This post was formerly published at my beloved old blog "On the quest of a DBA's adventure......"

It is embarrassing to announce that you have created a super duper database object for your manager and then found out that they don't have permission to run it.

There is a good and easy way to check if your intended user have enough permission to call the newly created object you crafted for them. On the other hand, of course you can use this way to make sure those who should not be able to access is really cannot access to the object.

Simply, run the EXECUTE AS command before the query like following:

Then try to query a view or running a stored procedure, it will or not return result based on the database user permission.

Once you are finished with your testing, you can use REVERT command to get back to last credential before the EXECUTE AS command

SQL Trick KB: Function - How to check the datatype for a field or variable with T-SQL

This post was formerly published at my beloved old blog "On the quest of a DBA's adventure......"

Warning: This is not necessary useful - until you need it or in my case, to be honest, when you are getting too bored.

One day, I try to verify what datatype of a field that is used by a stored proceduce. Clicking through the GUI is too hard and un-cool. So, Google tells me how to achieve it. There is a built in function called SQL_VARIANT_PROPERTY and this is the reference to it: MSDN - SQL_VARIANT_PROPERTY (Transact-SQL)

How-to: Check cluster size configuration on an existing drive

This post was formerly published at my beloved old blog "On the quest of a DBA's adventure......"

One of the best practice that, when preparing for hard drives which SQL server is going to utilise for storing the data file, log file and tempdb files, DBA should ensure that these drives are format with cluster size to be set to 64kB instead of the default NTFS value, which is 4kB.  Here is the background: Disk Partition Alignment Best Practices for SQL Server.

OK, how about if you just start a new job and would like to do a sanity check on what are you facing?To check the cluster size of an existing drive, this is the command:

fsutil fsinfo ntfsinfo C:

This is what the output look like:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\Windows\system32>fsutil fsinfo ntfsinfo C:
NTFS Volume Serial Number :      0x3cc4654e561810d1
Version :                         3.1
Number Sectors :                  0x000000001116389f
Total Clusters :                  0x000000000222c713
Free Clusters  :                  0x0000000000c2cccb
Total Reserved :                  0x0000000000000040
Bytes Per Sector  :               512
Bytes Per Physical Sector :       <Not Supported>
Bytes Per Cluster :               4096
Bytes Per FileRecord Segment    : 1024
Clusters Per FileRecord Segment : 0
Mft Valid Data Length :           0x0000000005e40000
Mft Start Lcn  :                  0x0000000000000004
Mft2 Start Lcn :                  0x0000000001116389
Mft Zone Start :                  0x0000000000005e40
Mft Zone End   :                  0x0000000000011020
RM Identifier:        B9CC8B49-4513-11E0-8641-806E6F6E6963

C:\Windows\system32>

The highlighted row is exactly what we are looking for, the cluster size of my C: drive is 4kB ( ie. 4 x 1024 btyes). I wish I would not see the same number when I run the command against the data and log file drive.....

Sanpshot replication dropped all my index

This post was formerly published at my beloved old blog "On the quest of a DBA's adventure......"

Just find out that the snapshot replication dropped all the index in the subscription database for some reason. I don't think this is intended, though it would make sense that if index is in place, it will slow down the bulk insert.

Update
Yes, snapshot replication will do that. Still trying to find out where Microsoft did document this information. It is making sense in every way excepts if it is documented somewhere, it will feel much better.

Monitoring replication with T-SQL

This post was formerly published at my beloved old blog "On the quest of a DBA's adventure......"

A developer asked that if there is anyway to monitor replication via T-SQL. I found a forum post which pointed me to Microsoft book online page - How to: Programmatically Monitor Replication (Replication Transact-SQL Programming). There are a number of system stored procedures which would cover the aspect related to Replication monitor.