MySQL – Exporting Query to Text File via Cron Job

exportlinuxMySQLscripting

I'm in the beginning stages of hacking together code that syncs two completely different pieces of software/webware. I'm looking for a way to run a SQL query and output the results to a text file. I'd like this information to be updated daily so I'm leaning towards a cron job.

My current line of thought is to just write a script that logs into the database(phpBB) run the query (All users that have X number of posts) and then export that list of users to a text file.

My question/s:

  1. Do I have to worry about putting the database username/password into this cron script?

  2. Is there another more efficient way for me to get this information from the database without logging into MySQL?

  3. Is Cron jobs appropriate for retrieving information from a database?

  4. Is there any MySQL specific cron/scripting issues I need to be aware of while programing this.

Please keep in mind that I will be using linux to code and execute this script and the MySQL server is hosted on a unix machine.

Best Answer

After talking to someone more knowledgeable then me I have came up with the following answers to my original questions.

TLDR; Cron should be fine for what your doing but don't write a bash script, write a PHP script instead, it's better.

Q1. Do I have to worry about putting the database username/password into this >cron script?

A1. As long as the script is in a safe spot on the server that is running the >>cron it should be fine. I can also create a new database user with very >>limited access specifically for this script.

Q2. Is there another more efficient way for me to get this information from >the database without logging into MySQL?

A2. There is a more efficient way to get the information, MySQLi or PDO via a >>PHP script, but there is no way to get around logging in.

Q3. Is Cron jobs appropriate for retrieving information from a database?

A3. Cron jobs should work fine for running the script on a regular basis.

Q4. Is there any MySQL specific cron/scripting issues I need to be aware of >while programing this.

A4. There isn't any cron specific issues but Bash or any other *nix based >>scripting language shouldn't be used because of limited access to MySQL. >>Instead a php script should be created utilizing either (PDO or MySQLi).

If anyone can answer the "Why" PHP is better then Bash Scripting in this scenario feel free to leave a post or comment.