SamueleS SamueleS - 20 days ago 5
MySQL Question

How to execute a crontab with MySql code?

I have a file:

test.txt
. This file is updated every day automatically. Example of text in the file:

TRUNCATE TABLE names;
INSERT INTO names (id, name) VALUES (1, 'john');
TRUNCATE TABLE apples;
INSERT INTO apples (id, name) VALUES (1, 'green');


I just need to set a crontab that executes this MySQL code every day at 02:00.

I know how to set a crontab command that launches a PHP file, but I can't find an answer on how to launch a MySQL code stored in another file with more than one query.

Answer

Try the documentation page 4.5.1.5. Executing SQL Statements from a Text File

commands.sql:

TRUNCATE TABLE names; 
INSERT INTO names (id, name) VALUES (1, 'john'); 

TRUNCATE TABLE apples; 
INSERT INTO apples (id, name) VALUES (1, 'green');

The command:

mysql -uusername -ppassword db_name < commands.sql
Comments