1、下载mysql安装包
下载mysql-8.0.15-winx64.zip,地址https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-winx64.zip
2、写bat文件
把文件夹解压缩到D盘(或解压到其他位置),在目录下新建初始化数据库.bat、初始化密码.bat和setpwd.sql
初始化数据库.bat内代码的内容为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@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内代码的内容为
1
2
3
4
5
|
@ECHO ON @echo 初始化密码 @SET sqlpath=%~dp0 @%sqlpath%\bin\mysql.exe -uroot < %sqlpath%\setpwd.sql @pause |
setpwd.sql内容为
1
2
3
4
5
6
7
8
|
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,内容为
1
2
3
4
5
6
7
|
@ECHO ON @echo 重新初始化 @SET sqlpath=%~dp0 net stop mysql mysqld --remove rmdir /s/q %sqlpath%\data del %sqlpath%\my.ini |
文章评论