Setting Up JDK and MySQL on Windows for Seamless Environment Migration and Reuse
This guide walks through installing JDK and MySQL on Windows, configuring environment variables via batch scripts, adjusting Nginx paths, and using offline installation methods so that the entire stack—including data—can be copied to other machines for rapid, repeatable deployment.
Scenario
Deploy a front‑end/back‑end separated system on multiple Windows servers. Manual steps per machine (JDK install, MySQL install, environment‑variable configuration, Nginx path adjustments) are replaced by copying prepared directories and running batch files, enabling reuse with minimal manual work.
JDK Offline Migration and Batch Configuration
Instead of running jdk-8u301-windows-x64.exe and manually adding the install directory to PATH, copy the entire JDK folder (e.g., C:\Program Files\Java) to a shared location and replicate it on target machines.
A batch file placed beside the copied Java folder performs:
Set console code page to UTF‑8: chcp 65001 Obtain the batch file’s absolute path: SET "ABS_PATH=%~dp0" Define JAVA_HOME as %ABS_PATH%Java\jdk1.8.0_301 Read the system Path registry value, append %JAVA_HOME%\bin, and write back with setx /M Run the batch as administrator; verify with java -version.
MySQL Offline Installation and Migration
Use a zip‑based offline installation of MySQL 8.0.26: extract the archive, edit my.ini so datadir points to a sub‑folder within the same directory, making the whole MySQL folder portable.
A similar batch script configures MYSQL_HOME, updates the system Path, and sets the variables with setx /M. The script must be run with administrator rights.
Modifying my.ini via Batch
Because my.ini cannot use relative paths, the batch obtains the absolute path ( %ABS_PATH%) and replaces specific lines (line 8 for basedir, line 10 for datadir) using a for /f loop that reads the file with line numbers, substitutes the target lines, writes to a temporary file, then renames it.
setlocal enabledelayedexpansion
SET "ABS_PATH=%~dp0"
set MYSQL_HOME=%ABS_PATH%mysql-8.0.26-winx64
set MYSQL_HOME_DATA=%ABS_PATH%mysql-8.0.26-winx64\data
set MYINI_PATH=%ABS_PATH%mysql-8.0.26-winx64\my.ini
set "line8=basedir=%MYSQL_HOME%"
set "line10=datadir=%MYSQL_HOME_DATA%"
for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "%MYINI_PATH%"') do (
set "line=%%a"
set "content=%%b"
if "!line!"=="8" set "content=%line8%"
if "!line!"=="10" set "content=%line10%"
>>"%MYINI_PATH%.new" echo !content!
)
del "%MYINI_PATH%"
ren "%MYINI_PATH%.new" "my.ini"
endlocalInstalling and Starting MySQL Service
After updating my.ini, another batch script installs the MySQL service and starts it:
echo ---------------------------Installing MySQL service--------------------------
cd mysql-8.0.26-winx64\bin
call mysqld --initialize-insecure
call mysqld -install
call net start mysql
echo ---------------------------Installation complete--------------------------
pauseVerify with a database client. To stop or remove the service, use net stop mysql or mysqld -remove.
Adjusting Nginx for Relative Paths
Place front‑end static files in font/dist under the Nginx root. Change the Nginx configuration from an absolute Windows path to a relative one:
location / {
root font/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}This removes absolute‑path dependencies, allowing the entire project—including the backend JAR, Redis (offline unzip), JDK, and MySQL directories—to be copied to another Windows machine and started with the provided batch files.
Resources
All batch scripts and configuration files are available for download at https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/89513627
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
