内网项目需要Mysql数据时,一步步手动安装Mysql数据库比较繁琐,使用Bat脚本一键安装,大大方便了项目部署过程。
Mysql安装脚本
@ECHO ON
:: 获取当前运行路径
@set Home=%~dp0
:: 查询有没有安装过Mysql服务,安装过就不需要重复安装了。
@sc query mysql
@if %errorlevel% EQU 0 @(
@echo Mysql server exists
@goto end
)
@echo init mysql
:: 安装Mysql 5.7 依赖于 C++ 环境 Redistributable 2013
@echo "Microsoft Visual C++ 2013 Redistributable Package Installation verification"
@reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0\VC\Runtimes\x64" /v "Installed"
@if %errorlevel% GTR 0 @(
@goto call
)
:verifyInstalled
@for /f "skip=2 tokens=3" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0\VC\Runtimes\x64" /v "Installed"') do @(
@set Installed=%%i
)
@if %Installed% EQU 0x1 @(
@echo "Microsoft Visual C++ 2013 Redistributable Package installed"
@goto init
)
@goto end
:call
@echo "Microsoft Visual C++ 2013 Redistributable Package start install"
@call "%Home%Microsoft Visual C++ 2013 Redistributable Package\vcredist_x64.exe"
@reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0\VC\Runtimes\x64" /v "Installed"
@if %errorlevel% GTR 0 @(
@goto end
)
@goto verifyInstalled
:init
:: Mysql的根目录
@set MysqlHome=%Home%mysql\
@set Port=13306
:: Mysql初始化时需要 my.ini 的配置文件
@echo init database configuration file
@del %MysqlHome%my.ini
@echo [mysql]>>%MysqlHome%my.ini
@echo default-character-set=utf8mb4>>%MysqlHome%my.ini
@echo [mysqld]>>%MysqlHome%my.ini
@echo port=%Port%>>%MysqlHome%my.ini
@echo basedir=%MysqlHome%>>%MysqlHome%my.ini
@echo datadir=%MysqlHome%data>>%MysqlHome%my.ini
@echo tmpdir=%MysqlHome%data>>%MysqlHome%my.ini
@echo socket=%MysqlHome%mysql.sock>>%MysqlHome%my.ini
@echo max_connections=200>>%MysqlHome%my.ini
@echo max_connect_errors=10>>%MysqlHome%my.ini
@echo character-set-server=utf8mb4>>%MysqlHome%my.ini
@echo default-storage-engine=INNODB>>%MysqlHome%my.ini
@echo default_authentication_plugin=mysql_native_password>>%MysqlHome%my.ini
@echo max_heap_table_size=64M>>%MysqlHome%my.ini
@echo sort_buffer_size=8M>>%MysqlHome%my.ini
@echo join_buffer_size=32M>>%MysqlHome%my.ini
@echo query_cache_size=64M>>%MysqlHome%my.ini
@echo skip-external-locking>>%MysqlHome%my.ini
@echo lower_case_table_names=2>>%MysqlHome%my.ini
@echo sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTIO>>%MysqlHome%my.ini
:: 初始化Mysql
@%MysqlHome%bin\mysqld.exe --initialize-insecure --lower-case-table-names=1 --user=mysql --console
@echo start install database
:: 安装Mysql
@%MysqlHome%bin\mysqld.exe --install
:: 启动Mysql
@net start mysql
@echo init password and tables
:: 重置Mysql密码
@%MysqlHome%bin\mysql.exe -uroot -P%Port% < "%MysqlHome%init.sql"
:: 停止Mysql
@net stop mysql
:: ==================================== mysql ====================================
:: end
@pause
重置密码
重置密码为 123456 并修改Root用户可远程登录。
use mysql;
select Host, User from user;
update user set authentication_string = password('123456') where user = 'root';
update user set host='%' where user='root';
flush privileges;
quit
验证Mysql安装
- 进入 cmd 界面
- net start mysql 启动 Mysql
- mysql.exe的全路径 -uroot -P13306 -p
mysql.exe 在 Mysql 的 bin 目录下。
- 输入密码:123456,回车登录。
- Mysql登录成功,安装完成