1. 由于编码问题我直接贴代码(ANSI编码)

  1. @echo off & setlocal
  2. :loop
  3. set /p answer=Do you want to continue (y/n)
  4. echo You chose: %answer%
  5. if /i {%answer%}=={y} goto :mysqlName
  6. if /i {%answer%}=={n} goto :no
  7. echo.
  8. echo.
  9. echo Invalid selection, try again&&goto :mysqlName
  10. rem 选择mysqld.exe 的新名字
  11. :mysqlName
  12. echo ================ 选择mysql版本 ===============
  13. echo -------------------------------------
  14. echo 1. mysqld56
  15. echo 2. mysqld57
  16. echo 3. mysqld80
  17. echo ==========PRESS '0' TO QUIT==========
  18. echo.
  19. set inputn=
  20. set /p inputn=Please select a number:
  21. if /i '%inputn%'=='1' goto mysqld56s
  22. if /i '%inputn%'=='2' goto mysqld57s
  23. if /i '%inputn%'=='3' goto mysqld80s
  24. if /i '%inputn%'=='0' goto end
  25. echo.
  26. echo.
  27. rem 输入错误跳回 input
  28. echo wrong number, try it again&&goto :loop
  29. :mysqld56s
  30. set setMysql=mysqld56
  31. goto search
  32. :mysqld57s
  33. set setMysql=mysqld57
  34. goto search
  35. :mysqld80s
  36. set setMysql=mysqld80
  37. goto search
  38. rem 指定待搜索的文件
  39. :search
  40. echo 正在搜索,请稍候...
  41. for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  42. if exist %%a:\ (
  43. for /f "delims=" %%b in ('dir /a-d /s /b "%%a:\*%setMysql%.exe" 2^>nul') do (
  44. if /i "%%~nxb" equ "%setMysql%.exe" (
  45. echo 搜索结束,文件路径为:%%b
  46. for %%a in (%%b) do (
  47. echo %%~nxa
  48. set fileName=%%~nxa
  49. )
  50. set currentDirss=%%b
  51. goto searchOk
  52. )
  53. )
  54. )
  55. )
  56. :searchOk
  57. set isNull=%currentDirss%
  58. if "%isNull%"=="" (
  59. echo 搜索的文件不存在
  60. goto end
  61. ) else (
  62. goto yes
  63. )
  64. :yes
  65. echo ================ 选择mysql功能 ===============
  66. echo.
  67. set currentDir=%currentDirss:~0,-13%
  68. set drives=%currentDirss:~0,2%
  69. SET dataFolder=%currentDir:~0,-3%data
  70. echo 查看mysql路径%currentDir%
  71. echo 启动的文件名:%fileName%
  72. echo cd 路径:%currentDir% %drives%
  73. echo data文件夹路径:%dataFolder%
  74. echo.
  75. echo -------------------------------------
  76. echo 1. 启动mysql 1
  77. echo 2. 进入mysql 2
  78. echo 3. 结束进程mysql 3
  79. echo ==========PRESS '0' TO QUIT==========
  80. echo.
  81. echo.
  82. set input=
  83. set /p input=Please select a number:
  84. if /i '%input%'=='1' goto startMysql
  85. if /i '%input%'=='2' goto goMysql
  86. if /i '%input%'=='3' goto killMysql
  87. if /i '%input%'=='0' goto end
  88. echo.
  89. echo.
  90. rem 输入错误跳回 input
  91. echo wrong number, try it again&&goto :mysqlName
  92. :startMysql
  93. echo.
  94. call taskkill /f /im %fileName%
  95. if %errorlevel% == 0 (
  96.  echo 杀死%setMysql%进程成功
  97. ) else (
  98.  echo 杀死%setMysql%进程失败
  99. )
  100. echo.
  101. echo Starting %setMysql% ...
  102. if exist %dataFolder% (
  103. rem 目录data已存在,无需创建
  104. echo 目录%dataFolder%目录已存在,无需创建!
  105. goto startMysqlSucceed
  106. ) else (
  107. rem 创建data目录,使用复制方法
  108. echo %dataFolder%目录不存在,已创建该目录!
  109. md %dataFolder%
  110. rem 3秒创建文件夹
  111. timeout /T 3 /NOBREAK
  112. rem 睡醒了
  113. cd /d %currentDir%
  114. call %setMysql% --initialize-insecure --console && (goto startMysqlSucceed) || goto startMysqlFailed
  115. )
  116. :startMysqlSucceed
  117. rem 睡三秒
  118. timeout /T 3 /NOBREAK
  119. cd /d %currentDir%
  120. echo 当前路径 %cd%
  121. start %fileName%
  122. echo 启动%setMysql%成功
  123. echo.
  124. echo.
  125. goto loop2
  126. :startMysqlFailed
  127. echo 初始化失败 删除文件夹
  128. rd /s /q %dataFolder%
  129. goto end
  130. :goMysql
  131. echo.
  132. echo.
  133. cd /d %currentDir%
  134. echo 当前路径 %cd%
  135. call mysql
  136. if %errorlevel% == 0 (
  137.  echo 启动%setMysql%命令行成功
  138. ) else (
  139.  echo 启动%setMysql%命令行失败
  140. )
  141. echo.
  142. echo.
  143. goto loop2
  144. :killMysql
  145. echo.
  146. echo 当前路径 %cd%
  147. echo.
  148. call taskkill /f /im %fileName%
  149. if %errorlevel% == 0 (
  150.  echo 杀死%setMysql%进程成功
  151. ) else (
  152.  echo 杀死%setMysql%进程失败
  153. )
  154. echo.
  155. echo.
  156. goto loop2
  157. :loop2
  158. echo ================ mysql ===============
  159. echo -------------------------------------
  160. echo 1. 选择mysql版本 1
  161. echo 2. 选择mysql功能 2
  162. echo ==========PRESS '0' TO QUIT==========
  163. echo.
  164. set input=
  165. set /p input=Please select a number:
  166. if /i '%input%'=='1' goto mysqlName
  167. if /i '%input%'=='2' goto yes
  168. if /i '%input%'=='0' goto end
  169. echo.
  170. echo.
  171. rem 输入错误跳回 input
  172. echo wrong number, try it again&&goto :loop
  173. goto end
  174. :no
  175. :end
  176. echo Good-bye!
  177. rem pause
  178. endlocal & @echo on

2. mysql ( my.ini )

  1. [mysql]
  2. # 设置 mysql 客户端默认字符集
  3. default-character-set=utf8mb4
  4. [mysqld]
  5. #设置 3306 端口,不配置默认3306
  6. port=3316
  7. # 设置 mysql 的安装目录
  8. basedir=D:\app\S_mysql\mysql-5.6.23-winx64
  9. # 设置 mysql 数据库的数据的存放目录
  10. datadir=D:\app\S_mysql\mysql-5.6.23-winx64\data
  11. # 允许最大连接数
  12. max_connections=200
  13. # 服务端使用的字符集默认为 8 比特编码的 latin1 字符集
  14. character-set-server=utf8mb4
  15. # 免密码登录
  16. skip-grant-tables
  17. #时间戳问题, 不然会报错
  18. explicit_defaults_for_timestamp=true
  19. # 创建新表时将使用的默认存储引擎
  20. default-storage-engine=INNODB
  21. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  22. [client]
  23. # 设置mysql客户端连接服务端时默认使用的端口
  24. port=3316
  25. default-character-set=utf8
  26. user=root

3. 注意事项

3.1 将下载好的mysql.zip解压

3.1.1 修改 ../bin/mysqld.exe文件问 mysqld版本号.exe

3..1.1.1 修改的同时请注意修改 启动脚本的

  1. :mysqld56s
  2. set setMysql=你修改后的名字;eg: 你改成了 mysqld12.exe,这里就填写 mysqld12 下同
  3. goto search
  4. :mysqld57s
  5. set setMysql=mysqld57
  6. goto search
  7. :mysqld80s
  8. set setMysql=mysqld80
  9. goto search

3.1.2 my.ini 文件复制到 ../mysql-5.6.23-winx64/根目录下

3.1.2.1 注意修改basedir datadir port

3.2 我设置的免密登录,如果要修改请将int文件中的[client]也做修改或 启动脚本中goMysql call mysql 做修改,要不然脚本的goMysql会失效

4. 参考

bat 判断命令是否执行成功参考:https://www.cnblogs.com/ibingshan/p/10906157.html

mysql启动脚本参考:https://blog.csdn.net/weixin_34062329/article/details/85985399

bat批处理 取得当前路径 %CD% 参考 :https://www.cnblogs.com/keyi/p/11688413.html

bat选择参考:https://zhidao.baidu.com/question/1431111218879314179.html

Bat搜索文件参考: https://www.jb51.net/article/54791.htm

Bat 字符串处理参考: https://www.cnblogs.com/accumulater/p/10647497.html

bat 获取最后一个斜杠之后的字符参考:https://zhidao.baidu.com/question/1511252184336599100.html

文件夹存在判断参考:https://gusanshang.blog.csdn.net/article/details/85067931?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control

https://www.cnblogs.com/wenanry/archive/2010/09/29/1838806.html

Bat sleep参考:https://blog.csdn.net/techfield/article/details/85164904


Bat 参数判空参考: https://blog.csdn.net/u012474286/article/details/18220719

【bat脚本】 000-切换到其他盘符 : https://blog.csdn.net/freehawkzk/article/details/81451426