Mysql events using cpanel cronjob - mysql

We have a queueing system that was developed by our previous developer, and the truncate command was manually executed to the mysql query. We cant use event scheduler on cpanel so the best option we have is to use cronjob. however, we have no idea on how to execute linux command.
Can someone help me to make a linux query for this?
TRUNCATE TABLE counter_logs_vxphl;

Try to execute the command using the following command, replace {USER} with the username and {PASSWORD} with the password, if you want to specify the host just add -h {HOSTNAME}
mysql -u {USER} -p{PASSWORD} -e "TRUNCATE TABLE counter_logs_vxphl;" > mysql-truncate.log

Related

Mysql schedule event in cpanel cronjob

I'm using a shared webhosting and there's no option to enable the MySQL event scheduler in PhpMyAdmin. The support told me to use the cpanel cron job section instead, but it requires a linux command and I have found nothing of help for this issue.
How can I make the cpanel cron job execute this mysql command every hour?
UPDATE `users` SET online = `0`
You need shell or php script with mysql command, smth like this:
#!/bin/sh
/usr/bin/mysql -h "server-name" -u "mysql_user" "-pSECRET" -e "UPDATE `users` SET online = `0`" database_name
Detailed CPanel cron job setup can be found here.

Inserting data in mysql with shell script

I have a problem with a shell script:
I am trying to inject data from a source file containing MySQL queries into a data base. Here are the relevant lines of my shell script:
mysql -u root
source /usr/local/insert.sql;
quit;
For example I am running the file as ./insertfile and it is running smoothly but when it comes to data insertion in MySQL it is logging into MySQL using the mysql -u root command but the remaining operations (source /usr/local/insert.sql; and quit;) are not being executed. When I quit MySQL manually it tries to execute the rest of the command from my insert.sql file.
So please help me use the right shell script so that I can insert the queries from the source file.
One way to do that would be
mysql -u root --execute="source /usr/local/insert.sql; quit;"
It seems that your import hangs !
Check for lock on your database.
show processlist;
Run FLUSH TABLES to release any possible locks and then run your import command.
if source command hangs again :
Enter your myslq server
drop database insert;
create database insert
exit the mysql server and run:
mysqldump -u -p database-name < dump.sql
Thanks for your help. I have tried adding your line in my script and it was primarily giving some errors then I changed the command like below -
mysql -u root --execute="source /usr/local/insert.sql; \q"
Above line helped me to execute my command.
Thanks to all for being this much helpful.
Regards,
Shah9il

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?

bash script command to inject a schema.sql into mysql db

i found this code but do not quite understand what the command is doing.
sudo -u test-user mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
i know the last part is using lib.model.schema.sql to create the tables and fields
the first part i dont quite understand: sudo -u test-user mysql -U test_traffic traffic
i know the command sudo and mysql
please explain?
thanks
Let's look at it bit by bit. Firstly the format
sudo -u username command
is an instruction to run command (which might be simple or complex) as the user username. So in your example, you are running the mysql command as the user test-user. You should note that this includes all the parameters to the mysql command - that's the entire rest of the line.
The command
mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
appears corrupt (certainly running it on 5.0.51a fails). It would make sense if the -U was a -u which would indicate that that the command was to be executed for mysql user test_traffic. If it was a -u you would then have an instruction to import the sql file into the traffic database.
So the combined instruction says, import the lib.model.schema.sql file into the database test_traffic using the mysql user test_traffic and executing the entire command as if you were logged-in as the user test-user.
Try Below steps for mysql:
mysql > -h hostname -u username -p password
mysql > use databasename;
mysql > source path/to/scriptfile
If you want to inject theschema.sql file into your database, with a shell script, simply use :
mysql -h [host] -u [username] -p[password] -D [database] < your_file
If you want to dynamicly tell which file should be loaded, replace your_file by $1 and pass the name of the file as an argument to your script.
Take care also to the -p option. There is no space between the -p and your password.

Connect to Mysql server to run tasks on two databases

I am writing a script to perform tasks between two MySql databases on the same server, i.e truncate tables on one db and import table rows from another db to this one.
The user who is doing the tasks has full permissions on both databases.
How do I connect to both databases from the command line?
Thanks in advance for any help.
You can use mysqlcommand line utility with the proper parameters:
mysql -u root -h your_host -p your_db
Here root is the privileged user and your_db is the database which is in use by default. You can always switch between databases by typing use another_db command from mysqlconsole.
Also note that you do not have to select dabase (use db_name) in order to execute query on it. You can for example write a query something like this:
SELECT a.id, b.title FROM db1.table1 AS a
LEFT JOIN db2.table AS b ON b.id = a.foreign_id
erm, well I would suggest you open up two terminal windows. the command to connect is:
mysql -u DBUSERNAME -h DBSERVER -p DBNAME
assuming you have mysql installed, which for ubuntu would be: sudo apt-get install mysql