I did tried to search, but nothing comes up that really works for me.
So i would start this thread to see if anyone can help. I hope this is not a stupid question that i overlook something simple.
I have a mac mini, that running with a MySQL server.
There is some day end job, so i put them into a script, trigger by a crontab (Actually I also tried launched as this is mac OS X, but same behavior)
crontab looks like this
15 00 * * * /Users/fgs/Documents/database/process_db.sh > /Users/fgs/Documents/database/output.txt 2>&1
the script looks like this
#!/bin/bash
#some data patching task before everything start
#This sql takes 3 sec
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/loadrawdata.sql
#This sql takes 90 sec
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/LongLongsql.sql
#This sql takes 1 sec
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/anothersql.sql
Behavior:
A. When i execute the shell script directly in terminal, all the 3 sql works
B. When i execute this with crontab, the 90 sec SQL doesn't work (it is an insert into with a very big join, so there is no output printed, i did also tried to > output file, adding 2>&1, also no output), but the SQL before and after it works as expected.
C. To simulate crontab behavior, I tried to use
env - /bin/sh
and then start the shell script manually.
It appears that, the 90 sec longlongsql.sql was running only 5 sec, and skipped to the next line. No error message was displayed
I am wondering if there is any kind of timeout for crontab? (I did searched but found nothing)
I did checked ulimit is unlimited (checked within "env - /bin/sh", and also did tried to put into the script)
I believe it is not related to mysql command, since it works fine by running same scripts (I also did searched this topic, and nothing interesting)
Just wondering if anyone can shed some light on me, a direction or whatever will help.
Thanks everyone in advance.
Don't forget that cron will start an isolated shell where it may not be able to read the file.
I would recommend to put your mysql-stuff inside a script. If you are able to execute the script, cron should also be able to do so.
#!/bin/bash
/usr/local/bin/mysql dbname -u root "-ppassword" < /Users/fgs/Documents/database/LongLongsql.sq
Or:
#!/bin/bash
/usr/local/bin/mysql --user=root --password=xxxxxx -e "/Users/fgs/Documents/database/LongLongsql.sq"
Then call the script from crontab...
Related
I've been searching to understanding the following MySQL command:
framework_mariadb.sql | mysql -u username -p password -h 127.0.0.1 -P 26257 target
My guess was the sql statements within the sql file get executed by mysql for the given target/database. But then I came across the source command in MySQL, ie
\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
So my question is, does the first command and the second command essentially do the same thing? My apologies if this has already been asked, I haven't been able to find details for the first SQL command.
This is more about Linux shell capabilities than it is about MySQL.
The second form runs the mysql client, and uses the < symbol to tell it to take its input from the specified file.
The first form does essentially the same thing, but uses the pipe character | to indicate that the output of the first command should be sent to the input of the second command.
However, for the first form I'd expect the line to start with cat (as in cat framework_mariadb.sql | mysql ...) because the SQL script won't normally run as a shell command. cat is a command that reads one or more files and send s them to its output.
It is possible to set the SQL script up to run like this, but that requires a specific line (#! /bin/cat or similar) to be present at the top of the file, and the file must have the execution bit set. (At least, that's how I'd do it. There might be some other bash magic I'm not aware of. bash is not my forté)
There are many resources on the web that can teach the fundamentals of the Linux shell. You could try Microsoft's Introduction to bash, but there are many others.
I have a 7GB database dump from a live site i want to use as as apart of a batch file which restores a dev database on my Wamp Server. because of how big it is i expect it'll take a while to go trhough the entire file.
when i go to use
mysql -u [USERNAME] -p"[PASSWORD]" -D [DATABASE] -e "source dbBackup.sql"
it just sits there looking like it's doing nothing. bit if i went
mysql -u [USERNAME] -p"[PASSWORD]" -D [DATABASE]
then used source dbBackup.sql it then outputs to the Command Prompt window stuff like
Query OK, 13896 rows affected (0.98 sec)
Records: 13896 Duplicates: 0 Warnings: 0
which is what i want because then i can see that it hasn't just frozen. i've tried putting
mysql -u [USERNAME] -p"[PASSWORD]" -D [DATABASE]
source dbBackup.sql
exit
into my batch file however when it does the first line it opens up mysql like i expect it to but doesn't execute source dbBackup.sql. it'll work if i copy and paste those lines into the Command Prompt Window (open mysql, run `source dbBackup.sql then exit) but that kinda defeats the point of making a batch file.
So is there a way for me to get my batch file to output to the command prompt window the same output as what would using source dbBackup.sql give me?
NOTE: the reason why i want the messages to appear in the Command Prompt Window (as opposed to loging them in a file) is to show that something is actually working. it looks bad if i'm sitting at my work PC reading Manga with what looks to be an idle Command Prompt Window, especially if one of my bosses come in and when asked why i'm not doing my work i have to explain (again) that i am restoring my database to remove the junk test data
I have a .sql file 25 sql statements, that runs ok when all statements are copypasted into mysql client It takes app 20 mins to finish and no warnings are displayed. However, if I call this file through batch file on windows (I would like to add this to task scheduler) - - the job does not get finished. I can see which statement is the problem - however - no warnings get displayed - I can only see in (show processes), that mysql process end and no subsequent commands from the file get executed - this happens somewhere in the middle on my initial sql file. Again - the statement get executed ok when copypasted into mysql console. If anyone has any idea - would appreciate a lot..
c:
cd\program files\mysql\mysql server 5.7\bin
mysql --show-warnings -h12.24.56.78 -u user -ppass --port=3306 --default-character-set=utf8 < "C:\SVN\dis\777e\777a_777e\777a_ua_into_777e.sql" >> "C:\Users\user1\db_transfers\777a_ua\777a_ua_into_777e_warnings.log" 2>&1
I need the script because a DB schema has changed and I have to fill a new table with some values (these have to be calculated using a php class). That was just a short summary, for a small overview).
Here's the first thing I can't figure out. I'm new to scripts so I was watching a few basic/beginner guids. In the shell (windows) I can run:
mysql -B -D www -h xxx -u yyy -pzzz -e "show tables"
this prints me all tables. Fine. If I try something similar in a batch script just to test if a can use variables
#!/bin/bash
V=10 // or V="10" // or V=$10, i saw all of it
mysql -B -D www -h xxx -u yyy -pzzz -e "select * from session_content_score where score_id<$V"
I'm always getting the error The command V cannot be found. I tried every declaration I could find. Whats the problem? How can I make usage of variables, because I need them for fulfilling my task?
The correct way to set a variable in a windows batch script is:
SET foo=bar
echo %foo%
A good windows batch tutorial is here:
Windows batch tutorial
You should question whether you are doing the right thing when you want to populate your database from windows batch scripts :)
I have set up a cronjob using crontab -e as follows:
12 22 * * * /usr/bin/mysql >> < FILE PATH >
This does not run the mysql command. It only creates a blank file.
Whereas mysqldump command is running via cron.
What could the problem be?
Surely mysql is the interactive interface into MySQL.
Assuming that you're just running mysql and appending the output to your file with >>, the first time it tries to read from standard input, it will probably get an end-of-file and exit.
Perhaps you might want to think about providing a command for it to process, something like:
12 22 * * * /usr/bin/mysql
-u me
-p never_you_mind
-e "select * from my_table"
-D my_database
>>/home/me/output_file
(split across multiple lines for readability, but should be on one line).
As an aside, that's not overly secure since your password may be visible from ps while the process is running. Since it's only an example, I'm not too worried, but you should consider storing the password in a properly secured my.cnf file if you go down this path.
In terms of running a shell script from cron which in turn executes MySQL commands, that should work as well. One choice is with a here-doc:
/usr/bin/mysql -u me -p never_you_mind -D my_database <<EOF
select * from my_table
select * from my_other_table where id = 74
EOF
12 22 * * * /usr/bin/mysql >> < FILE PATH > 2>&1
Redirect your error message to the same file so you can debug it.
There is also a good article about how to debug cron jobs:
How to debug a broken cron job