Mac一键快速控制 MySQL 服务的脚本

179 阅读2分钟

MySQL 是一种常用的关系型数据库管理系统,用于存储和管理大量的结构化数据。在开发和管理过程中,经常需要启动或关闭 MySQL 服务。为了简化这一过程,开发者使用 AppleScript 和 Terminal 应用程序编写了一个便捷的脚本。

  1. 首先,一个包含作者信息的对话框显示在屏幕上,要求用户选择进入或退出操作。
  2. 如果用户选择进入操作,将弹出另一个对话框。该对话框提供了两个按钮选项:"开启服务"和"关闭服务"。用户可以根据需要选择其中一个选项。
  3. 用户在对话框中输入密码,以确保拥有管理员权限执行相应的命令。
  4. 如果用户选择开启服务,脚本将使用 sudo 命令调用 Terminal 应用程序,并执行启动 MySQL 服务的命令。启动成功后,将显示一个包含"开启成功"消息的对话框。
  5. 如果用户选择关闭服务,脚本将使用 sudo 命令调用 Terminal 应用程序,并执行停止 MySQL 服务的命令。停止成功后,将显示一个包含"关闭成功"消息的对话框。
  6. 如果用户选择退出操作,脚本将结束执行。
set btns to {"进入", "退出"}
set Author to "快速一键开启Mysql
									Create  By Mr.Han"
set Nginx to {"开启服务", "关闭服务"}
set success to {"OK"}
display dialog Author buttons btns default button 1
if button returned of result = "进入" then
	set myInput to display dialog "点击按钮选择你要执行的操作10秒没有操作自动关闭" default answer "" buttons Nginx giving up after 10 with hidden answer
	set ans to button returned of myInput
	set pwd to text returned of myInput
	if ans is "开启服务" then
		tell application "Terminal"
			set res to do shell script "echo " & pwd & " |sudo -S /usr/local/mysql/support-files/mysql.server start"
			set visible of front window to false
		end tell
		display dialog "开启成功" buttons success
	else if ans is "关闭服务" then
		tell application "Terminal"
			set res to do shell script "echo " & pwd & " |sudo -S /usr/local/mysql/support-files/mysql.server stop"
			set visible of front window to false
		end tell
		display dialog "关闭成功" buttons success
	end if
else if button returned of result = "退出" then
end if

image.png

image.png

image.png