Connecting to MySQL via a batch file -
i've configured batch file check web hosted db every 30 seconds or see if value in 1 field has been set 1. i'm happy how works , results come through fine.
however 1 issue have once in while script unable connect sql server. when happens script errors out , batch file stops.
the connection line using is:
mysql --host=xxx.xxx.xxx --port=3306 --force --user=wmc --password=xxxxxxxxx --database=xxx < xxx.sql
the sql file @ end contains commands want run. read documentation said --force should make script continue after error still errors out when can't connect sql server. there way can trap error?
your main problem seems error handling. change script able detect failure , retry until successful.
quick test
a rough test did on own system check error level pseudo environmental variable:
test.bat
d:\xampp\mysql\bin\mysql --host=127.0.0.1 --port=3306 --force --user=root2 --password= --database=saasplex < test.sql echo exit code %errorlevel% d:\xampp\mysql\bin\mysql --host=127.0.0.1 --port=3306 --force --user=root --password= --database=saasplex < test.sql echo exit code %errorlevel%
console result
f:\_mbak_documents\_other_projects\stackexchange\august6>mysqlerrordetect.bat f:\_mbak_documents\_other_projects\stackexchange\august6>d:\xampp\mysql\bin\mysql --host=127.0.0.1 --port=3306 --force --user=root2 --password= --database=saasplex 0<test.sql warning: using password on command line interface can insecure. error 1044 (42000): access denied user ''@'localhost' database 'saasplex' f:\_mbak_documents\_other_projects\stackexchange\august6>echo exit code 1 exit code 1 f:\_mbak_documents\_other_projects\stackexchange\august6>d:\xampp\mysql\bin\mysql --host=127.0.0.1 --port=3306 --force --user=root --password= --database=saasplex 0<test.sql warning: using password on command line interface can insecure. 1 1 f:\_mbak_documents\_other_projects\stackexchange\august6>echo exit code 0
batch script retries
set tries=10 :loop d:\xampp\mysql\bin\mysql --host=127.0.0.1 --port=3306 --force --user=root --password= --database=saasplex < test.sql if errorlevel 1 ( set /a tries=tries-1 if %tries%==0 goto exitloop goto loop ) :exitloop
Comments
Post a Comment