gpt4 book ai didi

mysql - 批处理 MySQL if else 语句

转载 作者:行者123 更新时间:2023-11-30 22:54:37 29 4
gpt4 key购买 nike

我正在尝试创建一个批处理脚本,该脚本将在执行时创建一个数据库。如果数据库已经存在,脚本应该回显它已经存在。我让它在 Linux 上运行,但在 Windows 上显然有很大不同。

这是脚本:

@echo off
SET dbname=1234
SET user=1234
SET password=1234
SET hostinfo=test3
SET RESULT="mysql -u root -ppassword --skip-column-names -e SHOW DATABASES LIKE '%dbname%'"

if %RESULT% == "%dbname%" (
echo "The database already exists. You can use the reinstall function instead."
) else (
mysql -h localhost -u root -ppassword -e "CREATE USER '%user%'@'%%'"
mysql -h localhost -u root -ppassword -e "SET PASSWORD FOR '%user%'@'%%' = PASSWORD('%password%')"
mysql -h localhost -u root -ppassword -e "GRANT USAGE ON * . * TO '%user%'@'%%' IDENTIFIED BY '%password%' WITH MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0"
mysql -h localhost -u root -ppassword -e "CREATE DATABASE IF NOT EXISTS `%dbname%`"
mysql -h localhost -u root -ppassword -e "GRANT ALL PRIVILEGES ON `%dbname%` . * TO '%user%'@'%%'"

echo A new MySQL database has been created for you.
echo Username: %user%
echo Password: %password%
echo Database: %dbname%
echo Host: %hostinfo%:3306
)

输出如下:

Warning: Using a password on the command line interface can be insecure.
ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for '1234'@'%'
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
A new MySQL database has been created for you.
Username: 1234
Password: 1234
Database: 1234
Host: test3:3306

我该怎么做?好像 if else 语句没有正常工作...

亲切的问候,丹尼斯

最佳答案

请在 if statement 'echo %RESULT% and %dbname%' 之前添加,这将帮助您了解为什么会出现此问题

我建议将 if 更改为 if "%RESULT%"== "%dbname%"结果变量包括命令的字符串但未执行..因此您必须执行它并将输出设置到其中。检查这个:

@echo off
SET dbname=1234
SET user=1234
SET password=1234
SET hostinfo=test3
SET RESULT="mysql -u root -ppassword --skip-column-names -e SHOW DATABASES LIKE '%dbname%'"

for /f "delims=" %%i in ('%RESULT%') do set RESULT=%%i

if %RESULT% == "%dbname%" (
echo "The database already exists. You can use the reinstall function instead."
) else (
mysql -h localhost -u root -ppassword -e "CREATE USER '%user%'@'%%'"
mysql -h localhost -u root -ppassword -e "SET PASSWORD FOR '%user%'@'%%' = PASSWORD('%password%')"
mysql -h localhost -u root -ppassword -e "GRANT USAGE ON * . * TO '%user%'@'%%' IDENTIFIED BY '%password%' WITH MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0"
mysql -h localhost -u root -ppassword -e "CREATE DATABASE IF NOT EXISTS `%dbname%`"
mysql -h localhost -u root -ppassword -e "GRANT ALL PRIVILEGES ON `%dbname%` . * TO '%user%'@'%%'"

echo A new MySQL database has been created for you.
echo Username: %user%
echo Password: %password%
echo Database: %dbname%
echo Host: %hostinfo%:3306
)

在查看这篇文章之前,我做了一个类似的批处理 http://muhammadhamed.blogspot.com/2013/01/install-mysql-as-windows-service-from.html

希望对您有所帮助!

关于mysql - 批处理 MySQL if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976293/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com