Connecting to MySQL via batch file
I configured a batch file to check the db website every 30 seconds or so to see if the value in one field was set to 1. I'm happy with how this works and the results go through fine.
One problem, however, is that from time to time the script is unable to connect to the SQL server. When this happens my script and batch file errors stop.
Connection string used:
mysql --host=xxx.xxx.xxx --port=3306 --force --user=wmc --password=xxxxxxxxx --database=xxx < xxx.sql
The sql file at the end contains the commands I want to run. I read some documentation that says -force should make the script continue after an error, but it still throws an error when it cannot connect to the SQL server. Can I delay this error?
source to share
The main problem is error handling. I would personally modify my script to detect the failure and then try again until successful.
Quick test
A rough test I did on my own system to check the error level pseudo-ecological 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 is %errorlevel%
D:\xampp\mysql\bin\mysql --host=127.0.0.1 --port=3306 --force --user=root --password= --database=saasplex < test.sql
echo Exit Code is %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 a password on the command line interface can be insecure.
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'saasplex'
F:\_MBAK_DOCUMENTS\_OTHER_PROJECTS\STACKEXCHANGE\August6>echo Exit Code is 1
Exit Code is 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 a password on the command line interface can be insecure.
1
1
F:\_MBAK_DOCUMENTS\_OTHER_PROJECTS\STACKEXCHANGE\August6>echo Exit Code is 0
Batch script with 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
source to share