Bat批处理命令实现一键安装mysql环境

安装mysql文件包
下载mysql-8.0.15-winx64.zip,地址是https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-winx64.zip

写bat文件
把文件夹解压缩到D盘(或解压到其他位置),在目录下新建初始化数据库.bat、初始化密码.bat和setpwd.sql

初始化数据库.bat内代码的内容为

@ECHO ON
@echo 设置环境变量
@SET sqlpath=%~dp0
@setx /M PATH "%sqlpath%bin;%path%"
@echo 初始化数据库配置文件
@del %sqlpath%\my.ini
@echo [mysqld]>>%sqlpath%\my.ini
@echo port=3306>>%sqlpath%\my.ini
@echo basedir=%sqlpath%>>%sqlpath%\my.ini
@echo datadir=%sqlpath%\data>>%sqlpath%\my.ini
@echo max_connections=200>>%sqlpath%\my.ini
@echo max_connect_errors=10>>%sqlpath%\my.ini
@echo character-set-server=utf8mb4>>%sqlpath%\my.ini
@echo default-storage-engine=INNODB>>%sqlpath%\my.ini
@echo default_authentication_plugin=mysql_native_password>>%sqlpath%\my.ini
@echo [mysql]>>%sqlpath%\my.ini
@echo default-character-set=utf8mb4>>%sqlpath%\my.ini
@echo [client]>>%sqlpath%\my.ini
@echo default-character-set=utf8mb4>>%sqlpath%\my.ini
@echo port=3306>>%sqlpath%\my.ini
@%sqlpath%\bin\mysqld.exe --initialize-insecure --lower-case-table-names=1 --user=mysql --console
@echo 开始安装数据库
@%sqlpath%\bin\mysqld.exe --install
@net start mysql
@pause

初始化密码.bat内代码的内容为  

@ECHO ON
@echo 初始化密码
@SET sqlpath=%~dp0
@%sqlpath%\bin\mysql.exe -uroot < %sqlpath%\setpwd.sql
@pause

setpwd.sql内容为

use mysql;
select Host, User from user;
update user set authentication_string='' where user='root'; 
update user set host='%' where user='root';
flush privileges;
alter user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;
exitru

如需重新初始化,新建重新初始化数据库.bat,内容为

@ECHO ON
@echo 重新初始化
@SET sqlpath=%~dp0
net stop mysql
mysqld --remove
rmdir /s/q %sqlpath%\data
del %sqlpath%\my.ini

  

posted @ 2022-06-23 11:08  86727515  阅读(1251)  评论(0编辑  收藏  举报