Geek Sync: Field Medic’s Guide to Database Mirroring

download Geek Sync: Field Medic’s Guide to Database Mirroring

of 60

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Geek Sync: Field Medic’s Guide to Database Mirroring

  1. 1. Field Medics Guide to Database Mirroring August 18, 2015
  2. 2. Hi, Im Kendra Little
  3. 3. What youll learn today Rescue scenarios Why mirroring matters Special setup for mirrors Monitoring mirroring 3 1 2 4
  4. 4. Field medics need to know Rescue scenarios Why mirroring matters Special setup for mirrors Monitoring mirroring 3 1 2 4
  5. 5. Mirroring: Your friend since 2008 Became supported in SQL Server 2005 SP1 An important performance enhancement called Log Stream Compression was added in SQL Server 2008 Works in Standard Edition (synchronous only) Basic Availability Groups are a planned feature for SQL Server 2016 that we believe will be in Standard Edition, but
  6. 6. (continued) Mirroring is much easier to manage than Availability Groups No Windows Failover Cluster required Much simpler to document Management can all be done using SSMS/TSQL Mirroring has perks, too! Automatic page repair Doesnt require a domain / can be used with multiple domains (using certificates)
  7. 7. Mirroring is deprecated but this doesnt mean its dead It will be removed in a future version of SQL Server What version that is hasnt been disclosed. Features may stay deprecated for many many versions without being removed. Fully supported in SQL Server 2012 and 2014 Well known, widely documented, established feature Supported by many vendors Typically stable
  8. 8. Not ending a TSQL Statement in a ; Also deprecated
  9. 9. Mirroring modes Choose between safety and performance High Safety mode Synchronous Automatic failover is optional Witness use a witness if youre using automatic failover. (Can be Express Edition) High performance mode Asynchronous Enterprise Edition only No automatic failover Do not use a witness with this mode
  10. 10. An example: Async
  11. 11. Database Mirroring
  12. 12. Problems mirroring doesnt solve Databases are mirrored individually, and fail over individually No distributed transaction support (not in AGs prior to 2016, feature is planned at date of this presentation) If you want them to fail over as a group, you have to write custom code and make sure it happens You must keep SQL Agent Jobs and logins in sync and manage where they run Mirroring lots of databases on one instance can be difficult due to thread requirements (Also true for AGs)
  13. 13. Mirroring is the right choice when One mirror is enough Individual database failover is enough (not a group) You arent ready to manage the complexity of a cluster And you may not have many other choices if youre using SQL Server 2008 or SQL Server 2008R2
  14. 14. Demo: automatic page repair in action
  15. 15. This database is mirrored
  16. 16. High safety with no witness no automatic failover
  17. 17. The view from the GUI
  18. 18. We change to high performance mode
  19. 19. Lets corrupt a page dbo.CriticalTable has only one data page The table has a single row where the val column is quoting a famous sloth meme Were going to corrupt that page and see if mirroring can fix the issue
  20. 20. Heres the offset of the page in the data file We have to shut down the SQL Instance this time to edit the page
  21. 21. Open the data file with a hex editor
  22. 22. CTRL+G, set offset as dec relative to begin
  23. 23. Type over the values to corrupt
  24. 24. Save and click YES
  25. 25. Start the SQL Server back up
  26. 26. Try reading the page
  27. 27. Try again
  28. 28. Verify what happened
  29. 29. The fine print Automatic page repair only kicked in when corruption was detected Maximum of 100 rows per database Rows in sys.dm_db_mirroring_auto_page_repair are cleared on instance restart You still need alerts and monitoring to set page_verification to CHECKSUM for the database to run CHECKDB (to detect corruption where reads havent been happening) to investigate root cause when this happens But isnt this much better than repairing the page yourself?
  30. 30. This feature is also in AlwaysOn Availability Groups. Mirrors are just easier to manage!
  31. 31. Field medics need to know Rescue scenarios Why mirroring matters Special setup for mirrors Monitoring mirroring 3 1 2 4
  32. 32. Setting up logins You can script these all out from SQL Server Management Studio If you allow SQL Authentication, this can be complex though You have to look up the passwords yourself, and you dont want to save them in a script in plain text The SID of the login matters so that the login will map up with the database users, and the SID doesnt script automatically Robert Davis has published a free script that helps you automate this and solve both of these problems: database-mirror
  33. 33. Orphaned users If you do have a login where the SID wasnt transferred over to the mirror, the users associated with the login will be orphans and they wont work You can detect and fix this without recreating the login if it happens using sp_change_users_login:
  34. 34. CLR Assemblies: Trustworthy? CLR Assemblies are part of the database and are mirrored But they may not work when you first fail over without extra steps The TRUSTWORTHY database property isnt automatically restored with the database. If you need this, it must be enabled the first time you fail over Not all CLR assemblies require this-- you can see your current setting in the sys.databases DMV
  35. 35. Setting up SQL Server Agent jobs The easy way to script out all jobs in SSMS: View: Object Explorer Details In the Object Explorer Details pane, you highlight all jobs and script them out in a single script But beware If job steps use log files, make sure the same path exists where youre installing it Make sure you enable/disable the right jobs on the mirror
  36. 36. Making jobs work auto-magically after failover You need custom code to help your jobs out: If an automatic failover occurs, the right jobs need to run automatically, too You need to accomplish this without having jobs fail all the time if theyre running against the mirror because that will destroy monitoring for failed jobs Each job needs to check Am I the principal? and act accordingly This goes for your application jobs as well as maintenance
  37. 37. Special setup: Certificates If youre using certificates that are signed in the master database, these need to be set up on the mirror also Mirroring doesnt replace the need to securely back up those certificates in a place where youre sure you wont use them!
  38. 38. Field medics need to know Rescue scenarios Why mirroring matters Special setup for mirrors Monitoring mirroring 3 1 2 4
  39. 39. Split brain in High Safety Mode Split brain is as bad as it sounds: everything shuts down Lets say youre using high safety mode with a principal, a mirror, and a witness If the principal cant see either the mirror or the witness, it cant know if the database is online at the witness. It will take the database offline. If the mirror cant see the principal or the witness, it freaks out too, and puts the database offline. The database is offline everywhere. Weve lost quorum! Constant communication between the principal, mirror, and witness is critical
  40. 40. Split brain The database could be online somewhere else. Id better shut it down! Derp derp derp derp The database could be online somewhere else. Id better shut it down!
  41. 41. Suspended mirror Suspended status can happen after things go wrong Forced service SQL Server bugs in some versions (Examples: , ) Suspended means that data is not being transferred from the principal to the mirror The log will grow on the principal Youre no longer protected by mirroring To fix, on either partner run: ALTER DATABASE YourMirrorDB SET PARTNER RESUME;
  42. 42. Suspended mirrorIll save all the changes in my transaction logs for when you come back! Whats going on?
  43. 43. Disconnected witness Weve seen this if there have been network interruptions to the witness, or problems in DNS Mirroring may keep running (depending on whether or not the communication between the primary and the mirror is interrupted) If you have a disconnected witness, you may fix this by restarting the endpoints on the witness To prevent unplanned failovers, I prefer to: Switch to high performance mode and remove the witness Repair the witness Add the witness back and switch to high safety mode
  44. 44. Disconnected witness I sure hope theres no network interruptions Me too! Derp derp d derp
  45. 45. Forcing up the mirror Example: youre using asynchronous mirroring (high performance mode). This doesnt have automatic failover. The principal server is lost! You need to bring things up on the mirror. To do this, you run: ALTER DATABASE YourMirrorDB SET PARTNER FORCE_SERVICE_ALLOW_DATA_LOSS; Discussion: What do you do to identify how much data was lost? What other steps need to be done immediately if you have to do this?
  46. 46. Mirroring and Virtualization Technically, its supported by VMware: AlwaysOn Availability Groups and database mirroring can be used in combination with vSphere vMotion, vSphere HA, and DRS to maximize SQL Server availability. SQL Server on VMware Availability and Recovery Options SQL_Server_on_VMware- Availability_and_Recovery_Options.pdf
  47. 47. But its not all that simple Communication is critical Network interruptions can cause big problems: Unplanned failovers in mirroring Loss of quorum Even planned movements of virtual machines can cause packet loss and communication drops (a