批处理MySQL if else语句

时间:2014-11-17 15:40:54

标签: mysql batch-file if-statement

我正在尝试创建一个批处理脚本,在执行时将创建一个数据库。如果数据库已存在,则脚本应回显已存在的数据库。我有它在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语句没有正常工作......

亲切的问候, 丹尼斯

1 个答案:

答案 0 :(得分:0)

请在声明' echo%RESULT%和%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

我希望这可以提供帮助!