SQL Server 密码丢失如何重置

如果你WINDOWS密码和SQL Server密码都忘记了,无法连接到SQL Server,有没有办法可以恢复密码呢?答案是将数据单用户模式启动,用Windows Local administrator连接到SQL Server,然后重置密码。

 

You may have faced the issue of losing the SQL Server SA password. Perhaps you followed the security best-practice of removing the  builtin\Administrators from the sysadmin server role,  and no one  you can find is in the sysadmin role.   At this point you may think that your only options are to reinstall SQL Server and attach the databases, or to  directly access the master database files, which may potentially damage the data.

SQL Server 2005 provides a better disaster recovery option for this scenario that is non-intrusive for master DB and that will help you preserve any objects and data stored in master DB (such as logins, certificates, Service Master Key, etc.) intact. Members of the Windows Administrators group now have access to SQL Server when SQL Server is in started in single-user mode, also known as “maintenance mode “.

Using the single-user mode, SQL Server 2005 prevents a  Windows Administrator to abuse this privilege to act on behalf of the sysadmin without being noticed. This allows Windows Administrator accounts to perform certain maintenance tasks, such as installing patches.

In order to start SQL Server in single-user mode, you can add the parameter “-m” at the command line. You can also use theSQL Server Configuration Managertool, which provides proper controls for the file access and other privileges. To use theConfiguration Manager tool to recover your system, use the following steps:

1. Open the Configuration Manager tool from the "SQL Server 2005| Configuration" menu

2. Stop the SQL Server Instance you need to recover

3. Navigate to the “Advanced” tab, and in the Properties text box add “;–m” to the end of the list in the “Startup parameters” option

4. Click the “OK” button and restart the SQL Server Instance

NOTE: make sure there is no space between “;” and “-m”, the registry parameter parser is sensitive to such typos. You should see an entry in the SQL Server ERRORLOG file that says “SQL Server started in single-user mode.”

5. After the SQL Server Instance starts in single-user mode, the Windows Administrator account is able to connect to SQL Server using the sqlcmd utility using Windows authentication. You can use Transact-SQL commands such as "sp_addsrvrolemember" to add an existing login (or a newly created one) to the sysadmin server role.

The following example adds the account "Buck" in the "CONTOSO" domain to the SQL Server "sysadmin" role:

EXEC sp_addsrvrolemember 'CONTOSO\Buck', 'sysadmin';

GO

6. Once the sysadmin access has been recovered, remove the “;-m” from the startup parameters using the Configuration Manager and restart the SQL Server Instance

Important Security Notes:

This process should only be used for disaster recovery when no other method to access the system with a privileged (i.e. sysadmin or equivalent) is available.

This process allows a Windows Administrator account to override their privileges within SQL Server. It requires explicit and intrusive actions that can be monitored and detected, including:

·         Stop SQL Server and restart it in single use mode

·         Connecting to SQL Server using Windows credentials

Reference:http://blogs.msdn.com/b/raulga/archive/2007/07/12/disaster-recovery-what-to-do-when-the-sa-account-password-is-lost-in-sql-server-2005.aspx

 

如果忘记了 SQL Server 2019 的账户密码,可以尝试以下几个步骤进行恢复: ### 步骤一:查询当前登录信息 首先,在其他已知账户下登录到 SQL Server Management Studio 或命令行工具,运行如下 T-SQL 语句,查看是否有关于丢失密码账户的信息。 ```sql SELECT * FROM sys.server_principals WHERE name = 'username'; ``` 替换 `'username'` 为你想检查的用户名。 ### 步骤二:尝试密码找回功能 SQL Server 提供了密码找回的功能。你可以通过以下几种方式进行操作: #### 使用自定义的邮件服务发送密码 如果你在创建账户的时候配置过邮件服务,可以通过设置中的邮件服务将临时密码发送到你的邮箱。 1. 登录到 SQL Server 实例。 2. 执行如下命令打开“安全”选项卡: ```sql EXEC sp_password_reset; ``` 3. 系统会提示输入邮箱地址,然后系统将会把密码重置链接发送至指定邮箱。 #### 利用 Windows 账户关联 如果你在创建数据库账户时关联了 Windows 用户名,则可以通过 Windows 身份验证直接登录 SQL Server: 1. 关闭所有使用该 SQL Server 账号的应用程序。 2. 尝试使用 Windows 登录凭据连接 SQL Server。 ### 步骤三:修改密码 一旦找回了密码,你需要立即修改初始密码以提高安全性: 1. 登录 SQL Server 管理界面或使用命令行工具。 2. 运行以下命令更改密码: ```sql EXEC master..xp_cmdshell 'net user username password /newpassword:y'; ``` 其中 `username` 是你要更改密码的用户,`password` 和 `/newpassword:y` 分别是你希望的新密码和确认新密码。请注意,为了安全起见,建议选择强密码,并避免使用易猜测的组合。 ### 相关问题: 1. 如果找不到密码找回的方式,怎么办? 2. 是否有办法不使用管理员权限进行密码找回? 3. 密码找回过程中遇到错误应该如何解决? 记住,保持良好的密码管理习惯对于防止此类情况非常重要。建议定期更换密码、使用复杂且不易猜测的组合,并谨慎存储个人账号信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值