批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[已解决][40元已支付] 请问如何截取生成的随机密码并保存便于后续调用?

本帖最后由 xuc 于 2022-4-20 10:42 编辑

在写一个一键安装mysql的bat,输入mysqld --initialize --console命令后会生成一个随机密码,(最后12位,如:A temporary password is generated for root@localhost: C6lGXsJfk,4D),需要记录下这个密码,再执行mysqld --install mysql,net start mysql,mysql -uroot -p,然后输入刚刚的密码,最后执行ALTER USER 'root'@'localhost' IDENTIFIED BY '111111';
目前不知道如何保存密码并在后续需要输入的时候自动调用
已经写好的内容截取部分如下:
cd ./bin
mysqld --initialize --console
mysqld --install mysql
if !errorlevel! equ 0 (
        echo ******************安装mysql服务成功*******************
        echo.
) else (
        echo ******************安装mysql服务失败*******************
        echo 安装mysql服务失败 >> ../install_log.txt
        echo.
        pause
        exit
)
echo ******************开始启动mysql服务*******************
echo.
net start mysql
if !errorlevel! equ 0 (
        echo ******************启动mysql服务成功*******************
        echo 1 >> ./is_install.txt
        echo.
) else (
        echo ******************启动mysql服务失败*******************
        echo 启动mysql服务失败 >> ../install_log.txt
        echo.
        pause
        exit
)
实际运行生成密码部分如下:
D:\mysql-8.0.28-winx64\mysql-8.0.28-winx64\bin>mysqld --initialize --console
2022-04-19T11:45:10.951279Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
2022-04-19T11:45:10.951298Z 0 [System] [MY-013169] [Server] D:\mysql-8.0.28-winx64\mysql-8.0.28-winx64\bin\mysqld.exe (mysqld 8.0.28) initializing of server in progress as process 14012
2022-04-19T11:45:10.951379Z 0 [ERROR] [MY-010338] [Server] Can't find error-message file 'D:\mysql-8.0.26-winx64\mysql-8.0.26-winx64\share\errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2022-04-19T11:45:10.990057Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-04-19T11:45:11.514595Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-04-19T11:45:12.296844Z 0 [Warning] [MY-013829] [Server] Missing data directory for ICU regular expressions: D:\mysql-8.0.26-winx64\mysql-8.0.26-winx64\lib\private\.
2022-04-19T11:45:13.580235Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: C6lGXsJfk,4D
另外,安装过程参考https://blog.csdn.net/a802976/article/details/119255644?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164984901516782184696627%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164984901516782184696627&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-119255644.142^v7^control,157^v4^control&utm_term=mysql%E4%B8%8B%E8%BD%BD&spm=1018.2226.3001.4187,配置环境变量部分已写好,只需要截取12位密码保存,并在执行mysqld --install mysql,net start mysql,mysql -uroot -p这三个命令后调用

不会,帮顶哦

TOP

本帖最后由 zaqmlp 于 2022-4-20 14:55 编辑
  1. @echo off
  2. for /f "delims=" %%a in ('mysqld --initialize --console 2^>^&1^|find /i "password is generated"') do set "line=%%a"
  3. set "password=%line:~-12%"
  4. echo;"%password%"
  5. pause
  6. exit
复制代码
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

回复 3# zaqmlp


    你好,大部分已解决,尚有一个小问题:之后需要输入密码时如何自动输入呢?
目前的代码截取主要部分如下(去掉了if语句):for /f "delims=" %%a in ('mysqld --initialize --console 2^>^&1^|find /i "password is generated"') do set line=%%a
set password=%line:~-12%
echo;%password% >> ../install_log2.txt
pause
mysqld --install mysql
net start mysql
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '111111';
在倒数第二行运行后,如下所示,需要手动输入12位密码,请问能不能不手动输入,而是直接调用之前的password呢?
D:\mysql-8.0.28-winx64\mysql-8.0.28-winx64\bin>mysql -uroot -p
Enter password:

TOP

本帖最后由 zaqmlp 于 2022-4-19 22:06 编辑
  1. >"C:\t.sql" echo;ALTER USER 'root'@'localhost' IDENTIFIED BY '111111';
  2. mysql -uroot -p"%password%"<"C:\t.sql"
复制代码
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

返回列表