Hi guys,
I need to write in bash a script which do these things:
1) Login to MariaDB;
2) Truncate a table to make it empty;
3) Import data from a file to this table.
I found this guide https://notearena.com/lesson/how-to-access-databasemysql-mariadb-in-linux-shell-scripting/ and I'd like to know if it's the right way! :)
Now I do everything manually...
Regards.
^Bart
I found this guide https://notearena.com/lesson/how-to-access-databasemysql-mariadb-in-linux-shell-scripting/
and I'd like to know if it's the right way! :)
Hi guys,
I need to write in bash a script which do these things:
1) Login to MariaDB;
2) Truncate a table to make it empty;
3) Import data from a file to this table.
I found this guide https://notearena.com/lesson/how-to-access-databasemysql-mariadb-in-linux-shell-scripting/ and I'd like to know if it's the right way! :)
Now I do everything manually...
Regards.
^Bart
Hi guys,
I need to write in bash a script which do these things:
1) Login to MariaDB;
2) Truncate a table to make it empty;
3) Import data from a file to this table.
I found this guide https://notearena.com/lesson/how-to-access-databasemysql-mariadb-in-linux-shell-scripting/ and I'd like to know if it's the right way! :)
Now I do everything manually...
You don't need anything but SQL and the CLI.
Just create a text file with the SQL commands necessary to use the
database, truncate the table and import from the file. Call it
reload.sql, for instance.
Then use the CLI source command to run that file, i.e.
mysql -u user -ppassword
source reload.sql
Or you could do it with
mysql -u user -ppassword < reload.sql
It's not a good idea to have the password in a file.
The main issue with that script is the base64 encoded password, from
security standard point of view that is still as if the password was
stored in plain text.
sure if you need to automate this like running cron job, then you will
need to provide a password, then see to that the file is just readable
by the user it's executed as and that user ain't someone that people
just can login in with, don't forget that by default root will be able
to read the file.
You may also consider to create a store procedure that does everything
you need,
https://mariadb.com/kb/en/create-procedure/ https://mariadb.com/kb/en/stored-routine-privileges/ https://mariadb.com/kb/en/call/
I'd almost certainly write that in C or PHP. The APIS are very simple
for something like that.
I found this guide
https://notearena.com/lesson/how-to-access-databasemysql-mariadb-in-linux-shell-scripting/
and I'd like to know if it's the right way! :)
At first glace, that looks good, give it a try on a test database.
Thanks for your reply, I used PHP years and years ago and I don't
remember it very well but really thanks to share this solution! :)
You don't need anything but SQL and the CLI.
Ok!
Just create a text file with the SQL commands necessary to use the
database, truncate the table and import from the file. Call it
reload.sql, for instance.
Ok.
Then use the CLI source command to run that file, i.e.
mysql -u user -ppassword < reload.sql
So... I should have a file where I'll store the login data and I could
name it like "login.sql" and inside of it I should insert the "reload.sql".
Thanks for your reply, I used PHP years and years ago and I don't
remember it very well but really thanks to share this solution! :)
I did in my crontab this:
*/5 * * * * /usr/bin/mysql -u my_username -h localhost -p my_password < /home/itoffice/query.sql
I think I must use php like what this site said: https://stackoverflow.com/questions/9948220/how-can-i-set-a-crontab-to-execute-a-mysql-query-and-log-the-output
I think also I should save php file not in my home folder like
/home/itoffice but in /var/www/html right?
*/5 * * * * (root) (/usr/bin/mysql -umy_username -pmy_password
databasename <
/home/itoffice/query.sql)
*/5 * * * * (root) (/usr/bin/mysql -umy_username -pmy_password
databasename <
/home/itoffice/query.sql)
*/5 * * * * (itoffice) (/usr/bin/mysql -u username -p password db_name < /home/it_office/query.sql)
It doesn't work :\ but it doesn't work also if I insert it in a php
script; here below is the working *.sql file used from MariaDB CLI:
Thanks for your reply, I used PHP years and years ago and I don't
remember it very well but really thanks to share this solution! :)
I did in my crontab this:
*/5 * * * * /usr/bin/mysql -u my_username -h localhost -p my_password < /home/itoffice/query.sql
It doesn't work... :/
If I run query.sql after I did a manual login in MariaDB it works.
I think I must use php like what this site said: https://stackoverflow.com/questions/9948220/how-can-i-set-a-crontab-to-execute-a-mysql-query-and-log-the-output
I think also I should save php file not in my home folder like
/home/itoffice but in /var/www/html right?
^Bart
No space between your -p and your password. This is different from
other parameters.
No space between your -p and your password. This is different from
other parameters.
You got the point, it works without space from -p and password but...
It works from command line:
$ mysql -u MY_USER -pMY_PASSWORD < /home/itoffice/query.sql
It doesn't work from crontab -e also from root:
*/5 * * * * -u MY_USER -pMY_PASSWORD < /home/itoffice/query.sql
I don't know why... :\
^Bart
perhaps adding 'mysql' would help?
I did it but I didn't past it on my reply! :)
I also inserted the full path of Mysql but... nothing appened...
perhaps adding 'mysql' would help?
On 04/11/2023 09.42, ^Bart wrote:
perhaps adding 'mysql' would help?
I did it but I didn't past it on my reply! :)
I also inserted the full path of Mysql but... nothing appened...
Did you look at the mail from crontab? by default it goes to the
localhost root user (you can change that with setting the MAILTO
variable), if something wen t wrong while running the mysql command it
will be in that mail (if no output, no mail).
You could check your /var/log/mysql/mysql.{err,log} too
I hope the "-u MY_USER" was a typo, otherwise loose the space after '-u'.
Did you look at the mail from crontab? by default it goes to the
localhost root user (you can change that with setting the MAILTO
variable), if something wen t wrong while running the mysql command it
will be in that mail (if no output, no mail).
You could check your /var/log/mysql/mysql.{err,log} too
I hope the "-u MY_USER" was a typo, otherwise loose the space after '-u'.
Did you look at the mail from crontab? by default it goes to the
localhost root user (you can change that with setting the MAILTO
variable), if something wen t wrong while running the mysql command it
will be in that mail (if no output, no mail).
You could check your /var/log/mysql/mysql.{err,log} too
I hope the "-u MY_USER" was a typo, otherwise loose the space after '-u'.
I found this solutions:
*/5 * * * * /home/itoffice/job01.sh
This is job01.sh
#!/bin/bash
#sostituisce tutte le virgole presenti nel file con dei punti
sed -i -e 's/,/./gip' /home/itoffice/data_bridge/articoli.txt
But now I need to do other things in my company so... I know it's a
dirty way to use a *.sh file but I dont't have free time to fix it maybe
next weeks... ;)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 297 |
Nodes: | 16 (2 / 14) |
Uptime: | 110:45:09 |
Calls: | 6,662 |
Files: | 12,209 |
Messages: | 5,335,843 |