文章目录
脚本准备
解压MySQL绿色版后,在MySQL根目录下创建以下文件
Ps:可以不建立ps1-lib
目录,但是要对应的删除startup-init.bat
文件中的$ConfigFile
部分以及Pause
之类的方法
./startup-init.bat
<# : Begin batch (batch script is in commentary of powershell v2.0+)
@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" )
pushd "%CD%"
CD /D "%~dp0"
setlocal
powershell -executionpolicy remotesigned -Command "Invoke-Command -ScriptBlock ([scriptblock]::Create($([System.IO.File]::ReadAllText('%~f0')))) -ArgumentList ([string]'%*').split()"
endlocal
goto:eof
#>
#init variables
$CurrentFile=[regex]::Match($MyInvocation.MyCommand.Definition, "ReadAllText\(\`'([\s\S]+?)\`'\)").Groups[1].Value
Set-Location "$CurrentFile\.."
$CurrentPath=Get-Location
#import functions
dir "$CurrentPath\ps1-lib" -Filter "*.ps1" | foreach {
. $_.fullName
#Write-Host "Import $($_.fullName)"
}
# here start your powershell script
$MYSQL_HOME=$CurrentPath
$env:path+=";$MYSQL_HOME;$MYSQL_HOME\bin"
#读取配置文件,并更改配置文件中的配置目录信息,注意,执行后会打乱注释以及keys,如果要保留注释以及配置顺序则自行手动删除以下4行
$ConfigFile=Get-IniContent "$MYSQL_HOME\my.cnf"
$ConfigFile['mysqld']['basedir']="`"$CurrentPath`""
$ConfigFile['mysqld']['datadir']="`"$CurrentPath\data`""
Out-IniFile "Default" "$CurrentPath\my.cnf" -Force $ConfigFile -PassThru
net stop MySQL
mysqld --remove MySQL
sc.exe delete "MySQL"
mysqld --defaults-file="$MYSQL_HOME\my.cnf" --initialize --console --user=mysql --basedir="$MYSQL_HOME" --datadir="$MYSQL_HOME\data"
mysqld --install MySQL --defaults-file="$MYSQL_HOME\my.cnf"
net start MySQL
Pause
./my.cnf
[client]
loose-default-character-set=utf8mb4
port=3306
[mysql]
default-character-set=utf8mb4
[mysqld]
basedir="D:\Development\SQL\MySQL\mysql-8.0.21-winx64"
character-set-server=utf8mb4
datadir="D:\Development\SQL\MySQL\mysql-8.0.21-winx64\data"
default-storage-engine=INNODB
general_log_file="execute_sql_result.log"
general-log=1
log-error=mysql.err
log-output=FILE
long_query_time=10
lower_case_table_names=1
max_connections=151
port=3306
server-id=1
slow_query_log_file="user-slow.log"
slow-query-log=1
table_open_cache=2000
thread_cache_size=10
tmp_table_size=16M
./ps1-lib/Get-IniContent.ps1
Function Get-IniContent {
<#
.Synopsis
Gets the content of an INI file
.Description
Gets the content of an INI file and returns it as a hashtable
.Notes
Author : Oliver Lipkau <oliver@lipkau.net>
Blog : http://oliver.lipkau.net/blog/
Source : https://github.com/lipkau/PsIni
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
Version : 1.0 - 2010/03/12 - Initial release
1.1 - 2014/12/11 - Typo (Thx SLDR)
Typo (Thx Dave Stiff)
#Requires -Version 2.0
.Inputs
System.String
.Outputs
System.Collections.Hashtable
.Parameter FilePath
Specifies the path to the input file.
.Example
$FileContent = Get-IniContent "C:\myinifile.ini"
-----------
Description
Saves the content of the c:\myinifile.ini in a hashtable called $FileContent