【问题标题】:Start SQL Server only when SSMS is started仅在 SSMS 启动时启动 SQL Server
【发布时间】:2020-07-11 15:47:50
【问题描述】:

我最近安装了 SQL Server 2019,并且在启动时消耗了 3 GB 或 RAM。

我只是在家里使用它作为个人用途,所以我把它关掉了。

我想知道是否有办法在PowerShellCMD 中写入脚本:

  1. 在我启动 SSMS 时启动 SQL Server (MSSQLSERVER)
  2. 当我关闭 SSMS 时停止 SQL Server (MSSQLSERVER)

【问题讨论】:

  • 既然您已经标记了scripting - 只需创建一个脚本来启动 SQL 服务器和 SSMS 并将其固定到开始菜单或任务 Ba。困难的部分是在退出 SSMS 时停止 SQL Server 的方法。
  • 另外,考虑将 SQL Server 的 max server memory 设置为合理的限制。
  • @vonPryz,我工作时需要全力以赴,所以这不是一个选择

标签: sql-server powershell cmd scripting ssms


【解决方案1】:

您可以创建启动/停止服务器的脚本(例如批处理文件或 PowerShell),然后运行 ​​SSMS.exe。

您还可以在以下位置启动/停止 SQL 服务器引擎:

命令提示符

net start "SQL Server (MSSQLSERVER)"
net stop "SQL Server (MSSQLSERVER)"

PowerShell

# Get a reference to the ManagedComputer class.
CD SQLSERVER:\SQL\computername
$Wmi = (get-item .).ManagedComputer

$DfltInstance = $Wmi.Services['MSSQLSERVER']

# Display the state of the service.
$DfltInstance

# Start the service.
$DfltInstance.Start();

# Wait until the service has time to start.
# Refresh the cache.  
$DfltInstance.Refresh();

# Display the state of the service.
$DfltInstance

# Stop the service.
$DfltInstance.Stop();

# Wait until the service has time to stop.
# Refresh the cache.
$DfltInstance.Refresh();

# Display the state of the service.
$DfltInstance

更多详情请关注docs

【讨论】:

    【解决方案2】:

    这个脚本怎么样?它将等到 SSMS 退出,然后停止 SQL Server。当然,您的 SSMS 可执行文件的路径可能与我的不同。

    net start "SQL Server (MSSQLSERVER)"
    START /WAIT "" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft SQL Server Tools 17\Microsoft SQL Server Management Studio 17.lnk"
    net stop "SQL Server (MSSQLSERVER)"
    

    【讨论】:

      【解决方案3】:

      我自己找到了解决办法:

      # Execute this script as Administrator
      if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
      
      # Start all SQL* Services
      Get-Service | where {$_.Name -like "SQL*"} |  Start-Service
      
      # Start SSMS and wait till is closed
      Start-Process "C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\Ssms.exe" -NoNewWindow -Wait
      
      # Stop all SQL* Services
      Get-Service | where {$_.Name -like "SQL*"} |  Stop-Service
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-11
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        相关资源
        最近更新 更多