Install MySQL from official archive
This commit is contained in:
@@ -27,7 +27,6 @@ WINGET_PACKAGES = {
|
||||
"Gradle": "Gradle.Gradle",
|
||||
"Node.js": "OpenJS.NodeJS.LTS",
|
||||
"Git": "Git.Git",
|
||||
"MySQL": "Oracle.MySQL",
|
||||
}
|
||||
|
||||
MAVEN_VERSION = "3.10.0"
|
||||
@@ -38,6 +37,12 @@ TOMCAT_ARCHIVE_NAME = f"apache-tomcat-{TOMCAT_VERSION}-windows-x64.zip"
|
||||
TOMCAT_DOWNLOAD_BASE = f"https://dlcdn.apache.org/tomcat/tomcat-9/v{TOMCAT_VERSION}/bin"
|
||||
MYSQL_VERSION = "8.0.43"
|
||||
MYSQL_ROOT_PASSWORD = "123456"
|
||||
MYSQL_INSTALLER_ARCHIVE_NAME = f"mysql-installer-community-{MYSQL_VERSION}.0.msi"
|
||||
MYSQL_INSTALLER_URL = (
|
||||
"https://downloads.mysql.com/archives/get/p/25/file/"
|
||||
f"{MYSQL_INSTALLER_ARCHIVE_NAME}"
|
||||
)
|
||||
MYSQL_INSTALLER_SHA256 = "F60502EB1EE10A9C1D0A26706A627D224DFAE466D82EF3EB9D76A8A718DB9D80"
|
||||
|
||||
ANSI_PATTERN = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
|
||||
PERCENT_PATTERN = re.compile(r"(\d{1,3})\s*%")
|
||||
@@ -86,14 +91,6 @@ def build_install_command(tool_name: str, version: str | None = None) -> list[st
|
||||
"--id",
|
||||
package_id,
|
||||
]
|
||||
if tool_name == "MySQL":
|
||||
command.extend(["--version", MYSQL_VERSION])
|
||||
console_args = (
|
||||
f'CONSOLEARGS="install server;{MYSQL_VERSION};x64:*:'
|
||||
"type=config;open_win_firewall=false;tcp_ip=true;port=3306;"
|
||||
f'root_passwd={MYSQL_ROOT_PASSWORD} --silent --auto-handle-prereqs"'
|
||||
)
|
||||
command.extend(["--override", console_args])
|
||||
command.extend([
|
||||
"--accept-source-agreements",
|
||||
"--accept-package-agreements",
|
||||
@@ -102,6 +99,22 @@ def build_install_command(tool_name: str, version: str | None = None) -> list[st
|
||||
return command
|
||||
|
||||
|
||||
def build_mysql_installer_command(installer_path: Path) -> list[str]:
|
||||
console_args = (
|
||||
f"CONSOLEARGS=install server;{MYSQL_VERSION};x64:*:"
|
||||
"type=config;open_win_firewall=false;tcp_ip=true;port=3306;"
|
||||
f"root_passwd={MYSQL_ROOT_PASSWORD} --silent --auto-handle-prereqs"
|
||||
)
|
||||
return [
|
||||
"msiexec.exe",
|
||||
"/i",
|
||||
str(installer_path),
|
||||
"/qn",
|
||||
"/norestart",
|
||||
console_args,
|
||||
]
|
||||
|
||||
|
||||
def find_mysql_executable() -> Path | None:
|
||||
discovered = shutil.which("mysql")
|
||||
if discovered:
|
||||
@@ -197,15 +210,81 @@ def configure_mysql_weak_password_policy(
|
||||
on_output("MySQL 已永久允许 6 位弱密码,root 默认密码配置完成。")
|
||||
|
||||
|
||||
def install_mysql(command: list[str], on_output: Callable[[str], None]) -> Path:
|
||||
exit_code = run_install_command(command, on_output)
|
||||
if exit_code != 0:
|
||||
def install_mysql(
|
||||
on_output: Callable[[str], None],
|
||||
opener: Callable[..., BinaryIO] | None = None,
|
||||
elevated_runner: Callable[[list[str]], int] | None = None,
|
||||
) -> Path:
|
||||
open_url = opener or urllib.request.urlopen
|
||||
run_elevated = elevated_runner or run_elevated_command
|
||||
with tempfile.TemporaryDirectory(prefix="autodeploy-mysql-") as temporary:
|
||||
installer_path = Path(temporary) / MYSQL_INSTALLER_ARCHIVE_NAME
|
||||
on_output(f"正在从 MySQL 官方归档地址下载 MySQL Installer {MYSQL_VERSION}...")
|
||||
_download_to_file(MYSQL_INSTALLER_URL, installer_path, open_url, on_output)
|
||||
actual_checksum = _sha256(installer_path)
|
||||
if actual_checksum.lower() != MYSQL_INSTALLER_SHA256.lower():
|
||||
raise RuntimeError("MySQL 安装包 SHA-256 校验失败,已停止安装。")
|
||||
on_output("MySQL 安装包校验通过,正在请求管理员权限安装...")
|
||||
exit_code = run_elevated(build_mysql_installer_command(installer_path))
|
||||
if exit_code not in {0, 1641, 3010}:
|
||||
raise RuntimeError(f"MySQL 安装命令失败,退出码: {exit_code}")
|
||||
if exit_code in {1641, 3010}:
|
||||
on_output("MySQL 安装完成,Windows 提示稍后需要重新启动。")
|
||||
executable = wait_for_mysql_executable()
|
||||
configure_mysql_weak_password_policy(executable, on_output)
|
||||
return executable
|
||||
|
||||
|
||||
def run_elevated_command(command: list[str]) -> int:
|
||||
if os.name != "nt":
|
||||
return subprocess.run(command, shell=False).returncode
|
||||
|
||||
from ctypes import wintypes
|
||||
|
||||
class ShellExecuteInfo(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("cbSize", wintypes.DWORD),
|
||||
("fMask", wintypes.ULONG),
|
||||
("hwnd", wintypes.HWND),
|
||||
("lpVerb", wintypes.LPCWSTR),
|
||||
("lpFile", wintypes.LPCWSTR),
|
||||
("lpParameters", wintypes.LPCWSTR),
|
||||
("lpDirectory", wintypes.LPCWSTR),
|
||||
("nShow", ctypes.c_int),
|
||||
("hInstApp", wintypes.HINSTANCE),
|
||||
("lpIDList", wintypes.LPVOID),
|
||||
("lpClass", wintypes.LPCWSTR),
|
||||
("hkeyClass", wintypes.HKEY),
|
||||
("dwHotKey", wintypes.DWORD),
|
||||
("hIcon", wintypes.HANDLE),
|
||||
("hProcess", wintypes.HANDLE),
|
||||
]
|
||||
|
||||
shell_execute = ctypes.windll.shell32.ShellExecuteExW
|
||||
shell_execute.argtypes = [ctypes.POINTER(ShellExecuteInfo)]
|
||||
shell_execute.restype = wintypes.BOOL
|
||||
info = ShellExecuteInfo()
|
||||
info.cbSize = ctypes.sizeof(info)
|
||||
info.fMask = 0x00000040 # SEE_MASK_NOCLOSEPROCESS
|
||||
info.lpVerb = "runas"
|
||||
info.lpFile = command[0]
|
||||
info.lpParameters = subprocess.list2cmdline(command[1:])
|
||||
info.nShow = 0
|
||||
if not shell_execute(ctypes.byref(info)):
|
||||
error_code = ctypes.windll.kernel32.GetLastError()
|
||||
if error_code == 1223:
|
||||
raise RuntimeError("用户取消了 MySQL 安装所需的管理员授权。")
|
||||
raise OSError(error_code, "无法以管理员权限启动 MySQL 安装程序")
|
||||
try:
|
||||
ctypes.windll.kernel32.WaitForSingleObject(info.hProcess, 0xFFFFFFFF)
|
||||
exit_code = wintypes.DWORD()
|
||||
if not ctypes.windll.kernel32.GetExitCodeProcess(info.hProcess, ctypes.byref(exit_code)):
|
||||
raise OSError(ctypes.windll.kernel32.GetLastError(), "无法读取 MySQL 安装程序退出码")
|
||||
return int(exit_code.value)
|
||||
finally:
|
||||
ctypes.windll.kernel32.CloseHandle(info.hProcess)
|
||||
|
||||
|
||||
def _run_mysql_sql(mysql_executable: Path, sql: str, password: str) -> subprocess.CompletedProcess[str]:
|
||||
environment = os.environ.copy()
|
||||
environment["MYSQL_PWD"] = password
|
||||
@@ -368,6 +447,14 @@ def _sha512(path: Path) -> str:
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _sha256(path: Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for chunk in iter(lambda: source.read(1024 * 1024), b""):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _safe_extract_zip(archive_path: Path, destination: Path) -> None:
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
destination_resolved = destination.resolve()
|
||||
|
||||
Reference in New Issue
Block a user