windows mysql automatically starts bat script

Previous article: windows mysql service automatically starts error module name: ntdll.dll_csdn_aspnet's blog-CSDN blog

According to the previous article, I will supplement the manual startup of the mysql service. Since the demonstration machine system is installed with win11 home version, for the sake of safety, during the project demonstration, to avoid the failure to start the mysql service during the use of the database or the startup of the computer, write a bat script. Just name the bat file according to your own habits:

net start mysql8

Store the above command as mysql.bat, where mysql8 is the service name:

  

However, there is no problem if I run it directly as an administrator on my own machine. Occasionally, there will be no permissions on the Win11 Home Edition demo machine, so I once again write a code to automatically obtain administrator permissions, which can be directly double-clicked to run. The code is as follows:

@echo off

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (

goto UACPrompt

) else ( goto gotAdmin )

:UACPrompt

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"

exit /B

:gotAdmin

if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

net start mysql8

pause;

After direct execution, it flashed by and it was actually successful. I wanted to give a prompt and allow the cmd command interface to stay for a few seconds and then close:

@echo off

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (

goto UACPrompt

) else ( goto gotAdmin )

:UACPrompt

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"

exit /B

:gotAdmin

if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

net start mysql8023

@echo off
echo 本Dos窗体将在5秒后自动关闭
echo if wscript.arguments(0)^>0 then wscript.Sleep(wscript.arguments(0) * 1000):end if>"%Temp%\delay01.vbs"
cscript "%temp%\delay01.vbs" 5
del "%Temp%\delay01.vbs"

After the test was executed by double-clicking, it was found that this sentence: echo This Dos form will automatically close the Chinese garbled output after 5 seconds:

 

So add a line of encoding conversion, and the final code is as follows:

@echo off

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (

goto UACPrompt

) else ( goto gotAdmin )

:UACPrompt

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"

exit /B

:gotAdmin

if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

net start mysql8023

@echo off
chcp 65001
echo 本Dos窗体将在5秒后自动关闭
echo if wscript.arguments(0)^>0 then wscript.Sleep(wscript.arguments(0) * 1000):end if>"%Temp%\delay01.vbs"
cscript "%temp%\delay01.vbs" 5
del "%Temp%\delay01.vbs"

Test again and double-click to run the interface as follows:

 

 In this way, there is no need to right-click and run as administrator.

Guess you like

Origin blog.csdn.net/hefeng_aspnet/article/details/131779080