简单点的:
@echo off if %~dp0 == %cd%\ ( echo 请使用管理员身份执行脚本,按下回车键终止此次安装! set /p in= exit ) echo mysql5.*压缩版一键安装脚本... echo 即将开始安装... set /p password=请设置密码: if not defined password ( echo 您没有输入密码,按下回车键终止此次安装! set /p in= exit ) echo === 检查服务是否存在 SC QUERY MySQL > NUL IF ERRORLEVEL 1060 GOTO NOTEXIST GOTO EXIST :NOTEXIST ECHO 不存在MySQL服务 GOTO END :EXIST echo === 服务存在 net stop MySQL echo === 服务停止完成 sc delete MySQL If errorlevel 1 ( echo ===删除服务出错了。按下回车退出 set /p in= exit ) Else ( echo ===删除服务成功 ) GOTO END :END rem 将当前路径存储起来 set mysqlPath=%~dp0 rem 进入当前路径 cd /d %mysqlPath% rem 将配置文件名存储起来 set ini=my.ini rem 创建data文件夹 mkdir data echo ************************************************************ echo * * echo * MYSQL 系统环境变量设置 * echo * * echo ************************************************************ echo. echo === 准备设置环境变量: PATH=%%MYSQL_HOME%%bin echo. set /P EN=请确认后按 回车键 开始设置! echo. echo. rem ::如果有的话,先删除MYSQL_HOME wmic ENVIRONMENT where "name='MYSQL_HOME'" delete rem ::创建MYSQL_HOME wmic ENVIRONMENT create name="MYSQL_HOME",username="<system>",VariableValue="%mysqlPath%" ::在环境变量path中,剔除掉变量MYSQL_HOME中的字符,回显剩下的字符串 set newPath=%Path%;%%MYSQL_HOME%%\bin echo === 环境变量: PATH=%newPath% ::将返回显的字符重新赋值到path中 wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%newPath%" rem 向my.ini文件输出内容 echo [Client] >> %ini% echo. echo port = 3306 >> %ini% echo. echo [mysqld] >> %ini% echo. echo port = 3306 >> %ini% echo. echo basedir=%mysqlPath% >> %ini% echo. echo datadir=%mysqlPath%\data >> %ini% echo. echo max_connections=200 >> %ini% echo. echo character-set-server=utf8 >> %ini% echo. echo default-storage-engine=InnoDB >> %ini% echo. echo sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES >> %ini% echo. echo skip-grant-tables >> %ini% echo default_authentication_plugin=mysql_native_password >> %ini% echo. echo. echo [mysql] >> %ini% echo. echo default-character-set=utf8 >> %ini% rem 进入bin目录 cd /d %mysqlPath%\bin rem 执行mysql安装命令 start /b /wait mysqld -install rem 执行mysql初始化命令 start /b /wait mysqld --initialize --console rem 启动mysql服务 start /b /wait net start mysql rem 设置root用户密码 start /b /wait mysql -uroot -pnon -e "update mysql.user set authentication_string=PASSWORD('%password%') where user='root' and host='localhost';" rem 刷新权限 start /b /wait mysql -uroot -pnon -e "flush privileges;" rem 停止mysql服务 start /b /wait net stop mysql rem 启动mysql服务 start /b /wait net start mysql echo === 初始化数据库 set /p dataBaseName=请设置数据库名称: if not defined dataBaseName ( echo 您没有输入数据库名称,按下回车键终止此次安装! set /p in= exit ) set MYSQL_CMD= mysql -uroot -pnon rem echo %MYSQL_CMD% rem 停止mysql服务 start /b /wait net stop mysql rem 启动mysql服务 start /b /wait net start mysql echo === 创建数据库====数据库:%dataBaseName% start /b /wait %MYSQL_CMD% -e "CREATE DATABASE IF NOT EXISTS %dataBaseName% DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;"; set /p sqlfile=请输入执行sql的文件名称包括后缀: if not defined sqlfile ( echo 请没有输入执行sql的文件名称包括后缀,按下回车键终止此次安装! set /p in= exit ) echo === 执行sql脚本 echo. echo === 执行sql脚本---%sqlfile% echo. start /b /wait %MYSQL_CMD% -D%dataBaseName% < %mysqlPath%%sqlfile% @echo on echo === 执行sql脚本---setSql.sql echo. start /b /wait %MYSQL_CMD% -D%dataBaseName% < %mysqlPath%setSql.sql @echo off rem 停止mysql服务 start /b /wait net stop mysql rem 进入根目录 cd /d %mysqlPath% rem 清空my.ini文件 CD. > %ini% rem 向my.ini文件输出内容 echo [Client] >> %ini% echo. echo port = 3306 >> %ini% echo. echo [mysqld] >> %ini% echo. echo port = 3306 >> %ini% echo. echo basedir=%mysqlPath% >> %ini% echo. echo datadir=%mysqlPath%\data >> %ini% echo. echo max_connections=200 >> %ini% echo. echo character-set-server=utf8 >> %ini% echo. echo default-storage-engine=InnoDB >> %ini% echo. echo sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES >> %ini% echo default_authentication_plugin=mysql_native_password >> %ini% echo. echo. echo [mysql] >> %ini% echo. echo default-character-set=utf8 >> %ini% rem 启动mysql服务 start /b /wait net start mysql echo 安装完成 pause >nul
复杂点的:
@echo off if %~dp0 == %cd%\ ( echo 请使用管理员身份执行脚本,按下回车键终止此次安装! set /p in= exit ) echo mysql5.*压缩版一键安装脚本... echo 即将开始安装... set /p password=请设置密码: if not defined password ( echo 您没有输入密码,按下回车键终止此次安装! set /p in= exit ) set /p serviceName=请设置服务名称: if not defined serviceName ( echo 您没有输入服务名称,按下回车键终止此次安装! set /p in= exit ) set /p port=请设置端口号: if not defined port ( echo 您没有输入端口号,按下回车键终止此次安装! set /p in= exit ) echo === 检查服务是否存在 SC QUERY %serviceName% > NUL IF ERRORLEVEL 1060 GOTO NOTEXIST GOTO EXIST :NOTEXIST ECHO 不存在%serviceName%服务 GOTO END :EXIST echo === 服务存在 net stop %serviceName% echo === 服务停止完成 sc delete %serviceName% If errorlevel 1 ( echo ===删除服务出错了。按下回车退出 set /p in= exit ) Else ( echo ===删除服务成功 ) GOTO END :END rem 将当前路径存储起来 set mysqlPath=%~dp0 rem 进入当前路径 cd /d %mysqlPath% rem 将配置文件名存储起来 set ini=my.ini rem 创建data文件夹 mkdir data echo ************************************************************ echo * * echo * MYSQL 系统环境变量设置 * echo * * echo ************************************************************ echo. echo === 准备设置环境变量: PATH=%%MYSQL_HOME%%bin echo. set /P EN=请确认后按 回车键 开始设置! echo. echo. rem ::如果有的话,先删除MYSQL_HOME wmic ENVIRONMENT where "name='MYSQL_HOME'" delete rem ::创建MYSQL_HOME wmic ENVIRONMENT create name="MYSQL_HOME",username="<system>",VariableValue="%mysqlPath%" ::在环境变量path中,剔除掉变量MYSQL_HOME中的字符,回显剩下的字符串 set newPath=%Path%;%%MYSQL_HOME%%\bin echo === 环境变量: PATH=%newPath% ::将返回显的字符重新赋值到path中 wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%newPath%" rem 向my.ini文件输出内容 echo [Client] >> %ini% echo. echo port = %port% >> %ini% echo. echo [mysqld] >> %ini% echo. echo port = %port% >> %ini% echo. echo basedir=%mysqlPath% >> %ini% echo. echo datadir=%mysqlPath%\data >> %ini% echo. echo max_connections=200 >> %ini% echo. echo character-set-server=utf8 >> %ini% echo. echo default-storage-engine=InnoDB >> %ini% echo. echo sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES >> %ini% echo. echo skip-grant-tables >> %ini% echo. echo [mysql] >> %ini% echo. echo default-character-set=utf8 >> %ini% rem 进入bin目录 cd /d %mysqlPath%\bin rem 执行mysql安装命令 start /b /wait mysqld -install %serviceName% rem 执行mysql初始化命令 start /b /wait mysqld --initialize --console rem 启动mysql服务 start /b /wait net start mysql rem 设置root用户密码 start /b /wait mysql -uroot -pnon -e "update mysql.user set authentication_string=PASSWORD('%password%') where user='root' and host='localhost';" rem 刷新权限 start /b /wait mysql -uroot -pnon -e "flush privileges;" rem 停止mysql服务 start /b /wait net stop mysql rem 启动mysql服务 start /b /wait net start mysql echo === 初始化数据库 set /p dataBaseName=请设置数据库名称: if not defined dataBaseName ( echo 您没有输入数据库名称,按下回车键终止此次安装! set /p in= exit ) set MYSQL_CMD= mysql -uroot -pnon rem echo %MYSQL_CMD% echo === 修改密码====密码:%password% start /b /wait %MYSQL_CMD% -e " alter user user() identified by '%password%';" echo === 设置密码永远不过期 start /b /wait %MYSQL_CMD% -e " alter user 'root'@'localhost'password expire never;" echo === 刷新权限 start /b /wait %MYSQL_CMD% -e "flush privileges;" rem 停止mysql服务 start /b /wait net stop mysql rem 启动mysql服务 start /b /wait net start mysql echo === 创建数据库====数据库:%dataBaseName% start /b /wait %MYSQL_CMD% -e "CREATE DATABASE IF NOT EXISTS %dataBaseName% DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;"; set /p sqlfile=请输入执行sql的文件名称包括后缀: if not defined sqlfile ( echo 请没有输入执行sql的文件名称包括后缀,按下回车键终止此次安装! set /p in= exit ) echo === 执行sql脚本 echo. echo === 执行sql脚本---%sqlfile% echo. start /b /wait %MYSQL_CMD% -D%dataBaseName% < %mysqlPath%%sqlfile% @echo on echo === 执行sql脚本---setSql.sql echo. start /b /wait %MYSQL_CMD% -D%dataBaseName% < %mysqlPath%setSql.sql @echo off rem 停止mysql服务 start /b /wait net stop mysql rem 进入根目录 cd /d %mysqlPath% rem 清空my.ini文件 CD. > %ini% rem 向my.ini文件输出内容 echo [Client] >> %ini% echo. echo port = %port% >> %ini% echo. echo [mysqld] >> %ini% echo. echo port = %port% >> %ini% echo. echo basedir=%mysqlPath% >> %ini% echo. echo datadir=%mysqlPath%\data >> %ini% echo. echo max_connections=20000 >> %ini% echo. echo character-set-server=utf8 >> %ini% echo. echo default-storage-engine=InnoDB >> %ini% echo. echo sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES >> %ini% echo. echo [mysql] >> %ini% echo. echo default-character-set=utf8 >> %ini% rem 启动mysql服务 start /b /wait net start mysql echo 安装完成 pause >nul
setSql.sql
flush privileges; set password for root@localhost = password('123456'); CREATE USER 'root'@'%' IDENTIFIED BY 'root'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; flush privileges; quit
评论区