While working on various SQL Migrations these are my findings on how to migrate a database between servers with no downtime on the SQL server side.
I’ve been working on a big migration project for several months now, and I would like to share my experiences with you to make your migration go as smooth as possible.
One of the most common words you will hear while doing a migration is downtime.
When you ask your business people if there can be any downtime their answer will be quite straight forward: NONE!
In this post I would like to show you how to achieve that on a SQL server level on following editions: Enterprise BI and Standard.
Let’s look at all the functionalities we can use:
• Enterprise
o High Availability groups
o SQL Server Replication
o Database Mirroring
o Logshipping
• BI
o SQL Server Replication
o Database Mirroring
o Logshipping
• Standard
o SQL Server Replication
o Database Mirroring
o Logshipping
As you can see we have a lot of tools to achieve our goals. My personal preferred method is the one of SQL Server Database mirroring, in this post I will be continuing by explaining how to set up Database mirroring between 2 servers and how to successfully failover and make sure you can still go back should things go wrong. I’ve chosen to go in deeper on Database mirroring because this is available in Enterprise, BI & Standard edition, and these were the versions I was aiming at for my no downtime scenario.
Let’s get started…
We commence by setting up two database instances on a Hyper-V Server
After this we create a database on the ‘old’ server which we are going to migrate to the ‘new’ server.
/****** Object: Database [MirroringDB] Script Date: 9/12/2014 15:40:52 ******/ CREATE DATABASE [MirroringDB] CONTAINMENT = NONE ON PRIMARY ( NAME = N'MirroringDB', FILENAME = N'?\YourLocation\MirroringDB.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 8192KB ) LOG ON ( NAME = N'MirroringDB_log', FILENAME = N'?\YourLocation\MirroringDB_log.ldf' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 8192KB ) GO
We first backup our database with a FULL & Transaction log backup. (make sure your log chain isn’t broken, otherwise mirroring will not start)
FULL
BACKUP DATABASE [MirroringDB] TO DISK = N'?\YourLocation\MirroringDB.bak' WITH NOFORMAT, NOINIT, NAME = N'MirroringDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10 GO <span;">LOG BACKUP LOG [MirroringDB] TO DISK = N'?\YourLocation\MirroringDB.trn' WITH NOFORMAT, NOINIT, NAME = N'MirroringDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO
We then proceed by restoring these backups on the ‘new’ server. Do not forget to put on the WITH NORECOVERY OPTION.
USE [master] RESTORE DATABASE [MirroringDB] FROM DISK = N'?\YourLocation\MirroringDB.bak' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 5 RESTORE LOG [MirroringDB] FROM DISK = N'?\YourLocation\MirroringDB.trn' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 5 GO
When we have created the database and restored the backup with no recovery, I’ll go into the database properties of the ‘old’ SQL Server database
We choose the mirroring tab and click configure security
We click next on the first screen and choose not to use a witness
In the next screen we configure the endpoint. It is important to use a port that is not being used by a Machine. Since we are using one virtual machine and two instances, I will be using different ports for the two instances. 5022 & 5023. If we would be migrating cross server you can use 5022 for both endpoints
In the next screen we will give in the service accounts of the 2 instances. This is an example so no AD-accounts were used. It is the default SQL server windows account.
When we click finish the SQL server database migration will be configured by wizard.
Then click on start mirroring
Then the mirroring should start and you should see something like this
Now the mirroring has been set up between the two servers.
This concludes the setup of the SQL database mirroring. We now have the database running in sync. The next step is failing over the database to the new server, thus making the ‘new’ server the active one.
We start by taking a last backup to make sure we can go back should anything fail.
BACKUP DATABASE [MirroringDB] TO DISK = ?\YourLocation\MirroringDB2.bak' WITH NOFORMAT, NOINIT, NAME = N'MirroringDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO
Now we go back to the database mirroring properties screen of our old server. We can see that there is a button called Failover.
When we click it
Click yes in this screen.
After this we see that the mirroring has been suspended and the database is now running on the new server. This was achieved with no data loss and practically no downtime. (On SQL server level). It is important to know that the old database has now a restoring state and will not be able to be used again. So should things fail, you have to restore the backup we took just before we failed over. The only thing left to do now is remove the mirroring and we have successfully migrated a database between a SQL SERVER 2012 server and a SQL SERVER 2014 server. We can achieve this by clicking remove mirroring in the database properties mirroring screen of the principal database(the new one).
After you removed the mirroring you should see something like below.
As we can see in the image above we have successfully migrated the database.
This concludes my post on how I migrate a SQL Server Database, I know this is not the bulletproof way of migrating databases. That’s why in future posts I will be explaining other scenario’s for migration. For myself this and the Always on availability groups have proven to be the most easy to set up and resilient option. That’s why in my next post I will be talking about the setup of always on availability groups.
© 2023 Kohera
Crafted by
© 2022 Kohera
Crafted by
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |