MySQL automatic installation batch script

        As a patient with obsessive-compulsive disorder, you must use the zip mode to install MySQL. My computer is my decision, but the problem is that manual installation is really troublesome. It is impossible for us to remember every step of the operation, so I wrote a batch process for everyone refer to.

        MySQL5.7 and above are slightly different from MySQL5.7!

Script for versions below MySQL 5.7:

@echo off
echo.
set stdir=%~dp0%
echo [mysqld]>%stdir%my.ini
echo ##数据库根路径>>%stdir%my.ini
echo basedir=%stdir%>>%stdir%my.ini
echo ##数据库数据文件存放路径>>%stdir%my.ini
echo datadir=%stdir%data>>%stdir%my.ini
echo ##数据库端口号>>%stdir%my.ini
echo port=3306>>%stdir%my.ini
echo ##数据库字符集>>%stdir%my.ini
echo character_set_server=utf8>>%stdir%my.ini
echo. >>%stdir%my.ini
echo [client]>>%stdir%my.ini
echo ##数据库端口号>>%stdir%my.ini
echo port=3306>>%stdir%my.ini
echo ##数据库字符集>>%stdir%my.ini
echo default-character-set=utf8>>%stdir%my.ini
echo.
echo 开始安装数据库服务
%stdir%bin\mysqld -install MySQL
echo 启动数据库
net start MySQL
echo.
echo MySQL已经成功安装,数据保存在data文件夹中
pause

MySQL5.7 and above (including MySQL8.x):

@echo off
echo.
set stdir=%~dp0%
echo [mysqld]>%stdir%my.ini
echo ##数据库根路径>>%stdir%my.ini
echo basedir=%stdir%>>%stdir%my.ini
echo ##数据库数据文件存放路径>>%stdir%my.ini
echo datadir=%stdir%data>>%stdir%my.ini
echo ##数据库端口号>>%stdir%my.ini
echo port=3306>>%stdir%my.ini
echo ##数据库字符集>>%stdir%my.ini
echo character_set_server=utf8mb4>>%stdir%my.ini
echo. >>%stdir%my.ini
echo [client]>>%stdir%my.ini
echo ##数据库端口号>>%stdir%my.ini
echo port=3306>>%stdir%my.ini
echo ##数据库字符集>>%stdir%my.ini
echo default-character-set=utf8mb4>>%stdir%my.ini
echo.
echo 开始安装数据库服务
%stdir%bin\mysqld -install MySQL
echo 开始初始化数据库
%stdir%bin\mysqld --initialize
echo ===============================================
echo 数据库已初始化完成,请在data文件夹中查找.err后缀文件
echo 在文件中找到A temporary password is generated for root@localhost信息
echo 请记录该数据库默认密码,并在首次登录时修改密码
echo ===============================================
echo 启动数据库
net start MySQL
echo.
echo MySQL已经成功安装,数据保存在data文件夹中
pause

It can be seen that there are two differences in scripts of versions above 5.7:

1. Change the character set to utf8mb4. This utf8 format supports emoji characters, and the encoding is more powerful. If you still want to use the utf8 character set, please modify it manually.

2. The step of initializing the database is added. Versions above 5.7 require the database to be initialized before it can run, and a default password will be added during initialization. The password can be found in the file with the suffix .err in the data folder: A temporary password is generated for root@localhost This sentence is followed by the colon and the password.

How to use this script:

1. Copy the script to a text file and change the file name to xxx.bat file

Note: After saving the bat, open it with [Notepad] > [Save As] > [Modify the encoding to: ANSI], otherwise the Notepad will be saved as UTF-8 encoding, and Chinese characters will be garbled when executed

2. Put the script into the program package extracted from the Mysql you decompressed, at the same directory level as D:/MySQL/bin, such as: D:/MySQL/xxx.bat

3. Right-click the script and select "Run as Administrator"

        The script will automatically create the my.ini configuration file and the data directory (no need to create manually), and after the automatic installation is complete, it will automatically install a system service named: MySQL and start it automatically, that is, the MySQL database.

Attached:

How to modify the database default password:

# 1.用管理员权限打开cmd

# 2.切换路径至MySQL目录下bin文件夹中

cd /D d:/mysql/bin

# 3.登录数据库(默认登录密码在.err文件中找)

mysql -u root -p

# 4.修改密码

alter user 'root'@'localhost' identified by 'root';

How to uninstall MySQL:

# 1.用管理员权限打开cmd

# 2.切换路径至MySQL目录下bin文件夹中

cd /D d:/mysql/bin

# 3.用命令停止MySQL服务

net stop MySQL

# 4.执行命令卸载数据库

mysqld --remove MySQL

        How is it, is it helpful to you?

Guess you like

Origin blog.csdn.net/Asgard_Hu/article/details/124165418