0x2 error in task scheduler windows 7 mysql - mysql

I'm trying to do mysql batabase dump periodically through task scheduler. When I run the following query from command prompt, it gives me the dump. But, when I'm running through Task Scheduler, it's not working.
Query
C:\xampp\mysql\bin\mysqldump.exe --user=root --password=root --host=localhost --database gm > E:\backupgmdb\gm_backup_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.sql
Task Scheduler error -
0x2 error screenshot
Task Action Definition Screen Print

make .bat file, let say c:\xampp\mysql\bin\backup.bat and execute it from scheduller. put into .bat file this commands:
c:
cd \
cd C:\xampp\mysql\bin\
C:\xampp\mysql\bin\mysqldump.exe --user=root --password=root --host=localhost --database gm > E:\backupgmdb\gm_backup_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.sql
Plus, read this: https://serverfault.com/questions/734035/running-a-batch-file-from-task-scheduler-without-user-being-logged-in
The Task Scheduler in Windows Server 2008 R2 and later and Windows Vista and later executes tasks under a "least privileges" security context by default.
Open the properties of the task and check Run with highest privileges. Press OK, then enter the password for the administrative account when prompted. This will override the default "least privileges" behavior and will allow the task to run with administrative privileges.

Related

Issue with mysqldump returning Permission denied

I'm trying to setup a backup system for MySQL from PHP by using mysqldump command but I'm having a Permission denied error.
I'm on MacOS Catalina 10.15.6, using system PHP and Homebrew mysql#57.
After many attempts, I could reproduce this issue in Terminal. If I run the command as me, it works fine and the backup file is correctly created, but when I run it as _www I get the error.
This works:
% mysqldump --defaults-extra-file="crd" --extended-insert mydb > backup.sql.gz
And this does not work:
% sudo -u _www mysqldump --defaults-extra-file="crd" --extended-insert mydb > backup.sql.gz
sudo: unable to execute /usr/local/opt/mysql#5.7/bin/mysqldump: Permission denied
I checked and mysqldump can be executed by user, group and other:
% ls -la /usr/local/opt/mysql#5.7/bin | grep mysqldump
-r-xr-xr-x 1 jbogdani staff 3853364 Aug 17 21:22 mysqldump
Other attempts to provide username and password in the command also fail.
mysqldump will need a password for the mysql user root. If you don't supply that password it won't work, sudo or no sudo.
instead of using sudo -u _www just execute it with current mysql user account.
if you need further reading
You need to use a full path on the output.
You do not have permissions to write to /usr/local/opt/mysql#5.7/bin/backup.sql.gz. Specify full path of the target backup archive to another directory
I think you'll find the _www user is restricted in some way. It might not have a valid shell, it might be locked, or there might be apparmour/selinux restrictions preventing it from running.
Check the output of dmesg and /var/log/secure for useful logs, otherwise check and change the shell and status of the user using usermod to find and isolate the issue.
Make sure you consider the security ramifications before doing anything in production though.

Mysql: how to get the mysql status info just using one command

I have installed mysql-server on ubuntu. If I want to know the status of the mysql, I need to type 'mysql -u root -p' in the command line, and then I need to type the password to enter the mysql interaction mode, finally in the interaction mode I need to type 'show status' and I can see the info. Now my question is, is there any method doesn't need to enter the mysql interaction mode, just type one command in the command line and get the mysql status info?
You can use:
mysql -p'password' -e "SHOW STATUS"
Where -e flag is:
-e, --execute=name Execute command and quit. (Disables --force and history file.)
If you don't want to use password as of mysql documentation you can store your password in an option file (Note to change permissions respectively):
Store your password in an option file. For example, on Unix, you can list your password in the [client] section of the .my.cnf file in your home directory:
[client]
password=your_pass
So finally you have to run:
mysql -e "SHOW STATUS"

Monitor progress when running MySQL queries from command line in Windows using a batch file

I'm running MySQL queries from within Windows command prompt by utilizing a batch file.
The file contains:
#echo off
cd "c:\Program Files\MySQL\MySQL Server 5.6\bin"
echo Please type the root password
set /p password=
mysql -uroot -p%password% < C:\Temp\GenerateDB.sql
echo Please wait while script is executing and when finished successfully
Pause
mysql -uroot -p%password% < C:\Temp\UpgradeSchema.sql
echo Please wait while script is executing and when finished successfully
Pause
Exit
My only problem is that I cannot know if the first query (GenerateDB.sql) is complete or not in order to proceed to the next one(UpgradeSchema.sql).
Is there a way to get a prompt when it is complete or even better track real time progress as when you run the script from MySQL Command Client?
Many thanks in advance

using batch to detect whether database exists,if not ,create it

I am going to write a .bat file to realize this function:
to detect whether the database and the table existed,if not exist,create them.
I tried to write the bat file like this:
"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost -u root --password=
select * from martin.aaaperson
Then in cmd when I execute this, the bat file will not run the query until after I exit from mysql.
C:\000test>"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost
-u root --password=
(information of mysql)
mysql> exit
Bye
C:\000test>select * from martin.aaaperson
"select" is not knowby cmd
you need to do something like
mysql.exe -u root -p < your_sql_commands_in_this_file.sql
Put your sql into a file, and then redirect that file into mysql, so it's used as actual commands.
Batch files can only work at the command line. Once you run mysql.exe the .bat file is suspended until mysql exits, and then the batch resumes. That means by the time the batch fires back up again, you've exited mysql and are no longer doing sql operations - you're just back at a command prompt.

execute mysql commands via desktop shortcut?

On a windows machine, every day i have to login to mysql via phpmyadmin, go to a particular table and run the same sql command to do some cleanup.
I want to automate this process without setting up a TRIGGER.....is there a command prompt solution for doing this, or an automatic process that can be simply run from a desktop shortcut?
Write a script Run.bat and copy the following contents in it:
mysql -h localhost -u root -ppassword -D database_name -e "cleanup command".
Schedule this command in your windows scheduler.
If you have multiple cleanup commands, then add them in a sql file and then schedule the following command:
mysql -h localhost -u root -ppassword -D database_name < path_to_sql_file
For windows you could use the task scheduler. You might not be able to get it to login to phpMyAdmin though. Perhaps cmd line for mysql?