search
HomeDatabaseMysql Tutorial启动/停止SQL Server服务的批处理脚本

提供一个方便网站管理员用的程序 SQL Server 启动/停止服务的批处理脚本代码,有需要的朋友可以参考一下。

 案例一:在一个Windows操作系统中,安装有10个SQL Server实例,每一个实例存在大约120个。现在要同时启动10个实例的服务(SQL Server & SQL Server Agent & SQL Server FullText Search)。通过SQL Server Configuration Manager,需要一个实例,一个服务的一一启动。那是多么繁琐、笨拙的操作。

      案例二:在案例一的场景,启动了全部的实例。不久,就发现程序运行缓慢,即使在Microsoft SQL Server Management Studio(MSMS)管理器查询数据,都有如蜗牛爬树。这时候,需要关闭一些暂时不用的SQL Server实例的服务。等需要的时候再启动。一些时候,可能会碰到经常要关闭这个实例的服务,启动另外实例的服务,以便解决Windows系统资源紧张的问题。

 解决上面案例繁琐的操作问题,可以考虑通过Net命令,来启动或停止各个SQLServer服务,如:

 代码如下 复制代码
net Start SQLAgent$SQL2005DE1  /*启动实例SQL2005DE1中的SQLAgent服务*/
net Stop SQLAgent$SQL2005DE1  /*停止实例SQL2005DE1中的SQLAgent服务*/

 

      根据Net命令,可以通过编写一个批处理脚本实现,启动各个实例的各服务。Copy下面的代码,存储为后缀名为Bat的批处理文件“Start&StopSQLServer.bat”:

 代码如下 复制代码
View Code
@echo off
:a
echo 本机的实例列表:
echo ---------------------------
echo     1    PC143SQL2005DE1
echo     2    PC143SQL2005DE2
echo     3    PC143SQL2005DE3
echo     4    PC143SQL2005DE4
echo     5    PC143SQL2005DE5
echo     6    PC143SQL2005DE6
echo     7    PC143SQL2005DE7
echo     8    PC143SQL2005DE8
echo     9    PC143SQL2005DE9
echo     10    PC143SQL2005DE10
echo ---------------------------
echo 操作动作:
echo     1     启动服务
echo     0     停止服务
echo ---------------------------
echo.
Set/p var2=请输入操作动作:[1/0]
Set/p var1=请输入实例编号:[1/2/3/4/5/6/7/8/9/10]
if %var1% ==1 if %var2% ==1 goto S1
if %var1% ==2 if %var2% ==1 goto S2
if %var1% ==3 if %var2% ==1 goto S3
if %var1% ==4 if %var2% ==1 goto S4
if %var1% ==5 if %var2% ==1 goto S5
if %var1% ==6 if %var2% ==1 goto S6
if %var1% ==7 if %var2% ==1 goto S7
if %var1% ==8 if %var2% ==1 goto S8
if %var1% ==9 if %var2% ==1 goto S9
if %var1% ==10 if %var2% ==1 goto S10
if %var1% ==1 if %var2% ==0 goto T1
if %var1% ==2 if %var2% ==0 goto T2
if %var1% ==3 if %var2% ==0 goto T3
if %var1% ==4 if %var2% ==0 goto T4
if %var1% ==5 if %var2% ==0 goto T5
if %var1% ==6 if %var2% ==0 goto T6
if %var1% ==7 if %var2% ==0 goto T7
if %var1% ==8 if %var2% ==0 goto T8
if %var1% ==9 if %var2% ==0 goto T9
if %var1% ==10 if %var2% ==0 goto T10
echo.
cls
goto a:
echo.
:S1
net Start SQLAgent$SQL2005DE1 /Y
net Start msftesql$SQL2005DE1 /Y
goto EndApp
echo.
:S2
net Start SQLAgent$SQL2005DE2 /Y
net Start msftesql$SQL2005DE2 /Y
goto EndApp
echo.
:S3
net Start SQLAgent$SQL2005DE3 /Y
net Start msftesql$SQL2005DE3 /Y
goto EndApp
echo.
:S4
net Start SQLAgent$SQL2005DE4 /Y
net Start msftesql$SQL2005DE4 /Y
goto EndApp
echo.
:S5
net Start SQLAgent$SQL2005DE5 /Y
net Start msftesql$SQL2005DE5 /Y
goto EndApp
echo.
:S6
net Start SQLAgent$SQL2005DE6 /Y
net Start msftesql$SQL2005DE6 /Y
goto EndApp
echo.
:S7
net Start SQLAgent$SQL2005DE7 /Y
net Start msftesql$SQL2005DE7 /Y
goto EndApp
echo.
:S8
net Start SQLAgent$SQL2005DE8 /Y
net Start msftesql$SQL2005DE8 /Y
goto EndApp
echo.
:S9
net Start SQLAgent$SQL2005DE9 /Y
net Start msftesql$SQL2005DE9 /Y
goto EndApp
echo.
:S10
net Start SQLAgent$SQL2005DE10 /Y
net Start msftesql$SQL2005DE10 /Y
goto EndApp
echo.
:T1
net Stop MSSQL$SQL2005DE1 /Y
net Stop msftesql$SQL2005DE1 /Y
goto EndApp
echo.
:T2
net Stop MSSQL$SQL2005DE2 /Y
net Stop msftesql$SQL2005DE2 /Y
goto EndApp
echo.
:T3
net Stop MSSQL$SQL2005DE3 /Y
net Stop msftesql$SQL2005DE3 /Y
goto EndApp
echo.
:T4
net Stop MSSQL$SQL2005DE4 /Y
net Stop msftesql$SQL2005DE4 /Y
goto EndApp
echo.
:T5
net Stop MSSQL$SQL2005DE5 /Y
net Stop msftesql$SQL2005DE5 /Y
goto EndApp
:T6
net Stop MSSQL$SQL2005DE6 /Y
net Stop msftesql$SQL2005DE6 /Y
goto EndApp
:T7
net Stop MSSQL$SQL2005DE7 /Y
net Stop msftesql$SQL2005DE7 /Y
goto EndApp
:T8
net Stop MSSQL$SQL2005DE8 /Y
net Stop msftesql$SQL2005DE8 /Y
goto EndApp
:T9
net Stop MSSQL$SQL2005DE9 /Y
net Stop msftesql$SQL2005DE9 /Y
goto EndApp
:T10
net Stop MSSQL$SQL2005DE10 /Y
net Stop msftesql$SQL2005DE10 /Y
goto EndApp
:EndApp
Set/p var3=是否继续操作:[y/n]
If %var3% == y goto a:
 

 

这里演示了PC143上的10个SQL Server实例启动、停止的批处理脚本。下面来运行这个脚本,启动PC143上其中一个实例服务PC143SQL2005DE4:

SQL2005DE4:

image

 

关闭实例服务,类似启动实例服务,如:

 

image

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools