数据库镜像怎么还原数据库
Sometimes our Database fails and we need to reestablish the Database services immediately. Is there a SQL Server solution for high availability?
有时我们的数据库失败,我们需要立即重新建立数据库服务。 是否有用于高可用性SQL Server解决方案?
With Database Mirroring, we can have a replica of the database ready to be used if the main database fails. In this article, we will give a step-by-step tutorial about Database Mirroring. There are other solutions for high availability like high AlwaysOn, Clusters and Log Shipping, but in this chapter, we will start with Mirroring.
使用数据库镜像,如果主数据库发生故障,我们可以准备使用数据库的副本。 在本文中,我们将提供有关数据库镜像的分步教程。 还有其他一些针对高可用性的解决方案,例如高AlwaysOn,群集和日志传送,但是在本章中,我们将从镜像开始。
使用的服务器 ( Servers used )
For Database Mirroring, you require a Principal Server, which will be used as the main database. If it fails, the other server (Mirror Server) will be used to replace the Principal Server. Once the principal is fixed, you can return using the Principal server.
对于数据库镜像,您需要一个Principal Server ,它将用作主数据库。 如果失败,则将使用其他服务器( 镜像服务器 )替换主体服务器。 固定主体后,即可使用主体服务器返回。
If you want an automatic failover (when the principal server fails, the Mirror Server will be automatically activated to replace the principal server), a third Server is necessary.
如果要自动故障转移(当主体服务器发生故障时,镜像服务器将被自动激活以替换主体服务器),则需要第三台服务器。
数据库镜像角色 ( Database Mirroring Roles )
In Database Mirroring, there are two main operative modes:
在数据库镜像中,有两种主要的操作模式:
- High-safety mode is a mode secure and safe to switch from the Principal to the Mirror Server. This mode can be automatic (requires 3 servers) or manual (requires 2 servers). 高安全性模式是一种安全可靠的模式,可以从主体切换到镜像服务器。 此模式可以是自动的(需要3个服务器)或手动的(需要2个服务器)。
- High-performance mode is a faster mode to switch from the Principal to the Mirror Server, but some data loss can occur with this mode. 高性能模式是从主体切换到镜像服务器的更快模式,但是此模式可能会发生一些数据丢失。
要求 ( Requirements )
- You need three SQL Servers or at least three SQL Server Instances (three instances can be used for testing purposes only, but it is not recommended for production environments). 您需要三个SQL Server或至少三个SQL Server实例(三个实例只能用于测试目的,但不建议在生产环境中使用)。
- You can use SQL Server Enterprise, Standard, or Business Intelligence Edition for this article. 您可以在本文中使用SQL Server Enterprise,Standard或Business Intelligence Edition。
Create a Full backup of the Database to Mirror in the principal server.
创建要在主体服务器中镜像的数据库的完整备份。
Figure r1. A Full Backup
Figure r2. A Full Backup optionBackup the Transactional Log.
备份事务日志。
Figure r3. Transactional Log BackupRestore the database in the Mirror Server.
在镜像服务器中还原数据库。
Figure r4. Database Restoration.Make sure to restore with the NORECOVERY State in the Mirror Server.
确保在镜像服务器中使用NORECOVERY状态进行还原。
Figure r5. NORECOVERY state.
入门 ( Getting Started )
Right click on the database and select Tasks>Mirror
右键单击数据库,然后选择“任务”>“镜像”
Figure 1. The Mirror Task
图1.镜像任务Press the Configure Security button.
按配置安全性按钮。
Figure 2 The button to start the configurationThe Configure Database Mirroring Security Wizard will be displayed. Press next.
将显示“配置数据库镜像安全向导”。 按下一步。
Figure 3. The Database Mirroring Wizard
图3. 数据库镜像向导The first wizard will ask if you want a Witness. If you want the manual failover or a high-performance mirroring, the Witness is not necessary. If you want a high availability mirroring with automatic failover, select the yes option. In this demo, we will use a Witness.
第一个向导将询问您是否要见证。 如果要手动故障转移或高性能镜像,则不需要见证。 如果要通过自动故障转移进行高可用性镜像,请选择“是”。 在此演示中,我们将使用见证人。
Figure 4. The option to include the Witness
图4. 包括见证人的选项The next option will let you select where to save the security configuration.
下一个选项将让您选择将安全配置保存到何处。
Figure 5. Select Server to configure.
图5. 选择要配置的服务器。In the next option, you will select the ports used. We also have an option to Encrypt the data send from one Server to Another. The Principal Server and the Mirroring Server will be synchronizing data constantly. The Endpoint name will be created here. If you are using the same server with different instances, a different port should be used for each server.
在下一个选项中,您将选择使用的端口。 我们还有一个选项可以加密从一台服务器发送到另一台服务器的数据。 主体服务器和镜像服务器将不断同步数据。 端点名称将在此处创建。 如果将同一服务器用于不同实例,则应为每个服务器使用不同的端口。
Figure 6. The Endpoint information
图6.端点信息For the Mirror server, press connect and specify the connection properties. Also, select the listener port and the Endpoint name. If you are using the same server with different instances, a different port should be used for each server.
对于镜像服务器,请按connect并指定连接属性。 另外,选择侦听器端口和端点名称。 如果将同一服务器用于不同实例,则应为每个服务器使用不同的端口。
Figure 7. Mirror information
图7. 镜像信息If you selected the option to use a Witness. You will be required to press the connect button and select the credentials to connect to the witness Server. Also, select the listener port and the Endpoint name. If you are using the same server with different instances, a different port should be used for each server.
如果选择使用见证人的选项。 您将需要按下连接按钮并选择凭据以连接到见证服务器。 另外,选择侦听器端口和端点名称。 如果将同一服务器用于不同实例,则应为每个服务器使用不同的端口。
Figure 8. Witness configuration
图8. 见证配置In the Service Account Window, specify a domain account for the Principal, Witness or Mirror Server.
在“服务帐户”窗口中,为主体,证人或镜像服务器指定一个域帐户。
Figure 9. Service Accounts
图9. 服务帐户The Compete Wizard contains a list of all the configurations used. If you agree, press finish if not, press Back and change the configurations.
竞争向导包含所有使用的配置的列表。 如果您同意,则按完成,否则按Back,然后更改配置。
Figure 10. Configurations made.
图10.进行的配置。If everything is OK, a Success message will be display. Otherwise, you will receive error messages.
如果一切正常,将显示一条成功消息。 否则,您将收到错误消息。
Figure 11. The Success Window.
图11. 成功窗口。You will receive a message to start the Mirroring. If you are ready, press the Start Mirroring button.
您将收到一条消息,以开始镜像。 如果准备好了,请按“开始镜像”按钮。
Figure 12. The Start Mirroring message.
图12.“开始镜像”消息。Once you are done, the rest is easy. You can simulate that your Service is down (by stopping the SQL Server Database Engine Service in the Principal Server).
一旦完成,剩下的就很容易了。 您可以模拟服务已关闭(通过在Principal Server中停止SQL Server数据库引擎服务)。
Figure 13. Restarting the Sql service.
图13 。 重新启动 S ql 服务。
You will find that the Mirror Database is active now. If you start the SQL Server service again and stop the Mirror SQL Server Service, the Principal Server will be active now.
您会发现镜像数据库现在处于活动状态。 如果再次启动SQL Server服务并停止镜像SQL Server服务,则主体服务器现在将处于活动状态。
And that’s..that’s … that’s that’s all folks !
那就是..那就是所有的人!
错误讯息 ( Error messages )
A famous error message is this one:
这是一个著名的错误消息:
Figure 14. Typical error message
图14.典型错误消息
Error 1418, The network address can not be reached or does not exist. Check the network address name and that the ports for the local and remote endpoints are operational.
错误1418,无法访问或不存在网络地址。 检查网络地址名称,以及本地和远程端点的端口是否正常运行。
If you have this error message, you can verify the following steps.
如果出现此错误消息,则可以验证以下步骤。
- Verify that the Mirror server is in no-recovery mode (check the requirements). 确认镜像服务器处于不可恢复模式(检查要求)。
- this link. 此链接。
- this link.此链接。
有关数据库镜像的一些有用技巧 ( Some useful tips for Database Mirroring )
如何使用UI监视数据库镜像 ( How to monitor the Database Mirroring using the UI )
You can monitor your Database Mirroring using the Launch Database Mirroring Monitor
您可以使用启动数据库镜像监视器来监视数据库镜像
Figure 15. The Database Mirroring Monitor Option
图15. 数据库镜像监视器选项
如何检查用于镜像的端点的状态 ( How to check the status of the Endpoint for mirroring )
You can also, check the mirror status using the sys.database_mirroring_endpoints view:
您也可以使用sys.database_mirroring_endpoints视图检查镜像状态:
select * from sys.database_mirroring_endpoints
The status description show the status of the endpoint.
状态描述显示端点的状态。
Figure 16. The status of the endpoints.
如何使用T-SQL从镜像中删除数据库 ( How to remove the database from mirroring using T-SQL )
If for some reason you can not remove the database from UI, you can use the T-SQL for that purpose.
如果由于某种原因您无法从UI中删除数据库,则可以为此使用T-SQL。
ALTER DATABASE db_name SET PARTNER OFF
如何授予对镜像端点的连接权限。 ( How to grant connection permissions to the Mirroring Endpoints. )
You can also use T-SQL to grant Endpoint permissions.
您也可以使用T-SQL授予Endpoint权限。
GRANT CONNECT ON ENDPOINT::Mirroring TO [username]
如何使用T-SQL查看有关镜像的角色,配置信息 ( How to view role, configuration information about Mirroring using T-SQL )
select * from sys.database_mirroring
数据库镜像怎么还原数据库