How can I automate a SQL query via PHPMyadmin? - mysql

Because of a buggy wordpress plugin I am using, I have to manually run the following query many times during a single day:
update table_name set column_name="";
It simply drops the contents of the given column name.
So how can I make my server do it automatically every 30 minutes for instance, through PHPMyadmin?
I am not a PHP nor a SQL SAVVY. So please take that into consideration :)
I am on Hostmonster.com , and they have MySQL client version: 5.1.60 and phpMyAdmin v. 3.4.9

If are using UNIX and like you can put the following into your crontab:
30 * * * * "/<path_to_mysql>/mysql -u <username> -p<password> -e "update table set column=\"\""
This will do every 30 minutes what you need to do.
For Windows you can create a Batch file that does the same and execute it using Windows Scheduler

While it is a bit of work to do this every N minutes, it is quite easy to do it statistically every N requests. Put
<?php
define('MYPLUGIN_CLEANUP_PROBABILITY',5);
function myplugin_cleanup_db($postid) {
global $wpdb;
if (rand(0,99)<MYPLUGIN_CLEANUP_PROBABILITY))
$wpdb->query('update table_name set column_name=""');
}
add_action('shutdown', 'myplugin_cleanup_db');
?>
into a .php file and put it into your plugins directory after adapting your cleanup probability. Then activate this plugin and off you go.

To my knowledge, PHPMyAdmin can not automate it directly.
You have a few options:
Create a cron job to run the SQL statement (too advanced?)
Utilize WP Cron to schedule an event to run the SQL statement through WordPress. (possibly easier)
Fix the plugin (my vote, but could be difficult)

Related

How to delete expired data with cron job

I want to auto delete my data on mysql database which has expired date with cron job every 5 minutes.
For example I have a table (TableName) with one of column (ExpDate) as "2015-04-10 21:30:00", it's timestamp format type and UTC time zone.
I have tried creat a cron job command like this:
*/5 * * * * delete * from TableName where ExpDate > NOW();
But it didn't work, I've searched around in SO and elsewhere but have not found a direct answer to this. So, I created this question and hope someone can help me. I am a beginner in cron job, I would be very grateful if you could help me.
Cron doesn't know about SQL commands. Cron knows about running executable commands. This leads us to the answer - you need to either directly call the MySQL command line client in the crontab (probably a bad idea), or you need to write a script that does what you want, and then get cron to execute it.
A sample script would be:
USERNAME=[username]
PASSWORD=[password]
DATABASE=[database]
MYSQL=/usr/bin/mysql
$MYSQL -u $USERNAME -p$PASSWORD $DATABASE -e "DELETE from ....."
You would put this script somewhere safe, because it contains your password. Let's say you called it purge_expired.sh, you would need to make it executable like this chmod +x purge_expired.sh, and then tell cron to execute that script instead of trying to make it run the SQL commands itself.
Somehow I missed the memo where MySQL got its own event scheduler, you might want to use that instead.
An example specific to you would be something like:
CREATE EVENT DeleteExpired
ON SCHEDULE EVERY 5 MINUTE
DO
DELETE FROM TableName WHERE ExpDate < NOW();
You also need to make sure the event_scheduler variable is set to on in order for this to run.

Execute script when date matches mysql cell

I have a debian machine with a mysql server.
On mysql I have a table that contains a number of rows with a datetime field.
How can I execute a php script when the date and time of the machine match with those specified in any mysql record?
a) Mysql triggers ?
b) Deamon that runs in background and checks time every n seconds ?
c) Cron?
Let me know!
Polling the MySQL will work but surely is not efficient, especially when there are a lot of "triggers".
So use a cronjob. Note: MySQL-Queries cannot schedule or cancel cronjobs. So the (i guess) PHP-Script updating that date field also has to schedule the cronjobs. Also, every time the column changes you will have to cancel the previously scheduled cronjob and schedule a new one.
Since you can't run shell command from within Mysql database the MySql Triggers are no good. The MySQL Scheduler is therefore useless too. So you need an external script to help.
I'd suggest to create a (php) script and add it to the crontab to run every minute. Its task would be to check for matching dates in the database and run whatever command you need.
I think that the easiest solution is to use , if possible , the at command.
Basically after inserting the data into mysql do the following:
1)Create (using PHP) a BASH script that runs the php file
script_1.sh should look like this:
!#/bin/bash
php /path/to/file.php
and make it executable with :
exec("chmod +x script_1.sh");
2)create a second BASH script that executes the first at the desired time:
script_2.sh should look like this:
!#/bin/bash
at H:M Y:M:D < script_1.sh
Make script_2 executable and run it with the exec command.

sending terminal/shell command from mysql to terminal and retrieve answer while looping cursor

I'm using php with MySQL on macOS.
I would like to select a large amount of emails from a database and perform a dns lookup for each email in my selection using a dig command from the terminal/shell, something like: "dig gmail.com" .
Of course, I can loop this select through php but it will be very slow compared to looping cursor on MySQL.
How to send terminal commands from mysql to the terminal and retrieve answer on macOS?
You can't execute shell commands from within an SQL query (thank god), or else it would be a horrible security vulnerability... You would have to do it from php.
P.S. It is however possible to execute shell commands from the MySQL command line utility
\! ls
...but if I understand your question, it won't help solve your current problem.
(I'm assuming that you really mean ADDR_SPEC when you're talking about email addresses)
but it will be very slow in compare with looping cursor on mysql
No not really. The only difference is that depending on how you implement this the PHP approach requires that you retrieve the entire result set before you start iterating through it. However breaking this up into smaller result sets is trivial.
Also, the limitation on the performance of your algorithm is the speed of DNS lookups - and that's all about latency - if your objective is to make this go faster then you should be running multple requests in parallel.
The next thing you should consider is that you've probably got multiple mailboxes for each MX, e.g. user1#gmail.com, user2#gmail.com.... While if you've got DNS caching setup properly there will be less overhead than going to the source each time, if you're working with a very large data set or will be doing this more than once, it makes a lot more sense to just work with unique MX host values, e.g.
SELECT DISTINCT SUBSTR(addr_spec FROM LOCATE('#', addr_spec)) AS mx2chk
FROM yourtable
WHERE addr_spec LIKE '%#%'
AND (email_checked IS NULL
OR email_checked<NOW() - INTERVAL 300 DAY )
;
Indeed, if your flagging the data then you can use your own database to verify the MX.
using a dig command from terminal/shell
Please don't tell me that you're running a shell from a PHP controlling process to do a DNS lookup?

linux 'at' command like functionality for mysql database

I need to update a particular field in the database based on some timestamps in the same table. Now are there time based triggers which can be scripted to look at the value and do things? much like the linux 'at' command.
I know one way is to execute a script using the at command. I am asking about something at the database level itself.
MySQL has an event scheduler.
See
http://dev.mysql.com/doc/refman/5.5/en/events.html

SQL Server to MySQL data transfer

I am trying to transfer bulk data on a constant and continuous based from a SQL Server database to a MYSQL database. I wanted to use SQL Server's SSMS's replication but this apparently is only for SQL Server to Oracle or IBM DB2 connection. Currently we are using SSIS to transform data and push it to a temporary location at the MYSQL database where it is copied over. I would like the fastest way to transfer data and am complication several methods.
I have a new way I plan on transforming the data which I am sure will solve most time issues but I want to make sure we do not run into time problems in the future. I have set up a linked server that uses a MYSQL ODBC driver to talk between SQL Server and MYSQL. This seems VERY slow. I have some code that also uses Microsoft's ODBC driver but is used so little that I cannot gauge the performance. Does anyone know of lightening fast ways to communicate between these two databases? I have been researching MYSQL's data providers that seem to communicate with a OleDB layer. Im not too sure what to believe and which way to steer towards, any ideas?
I used the jdbc-odbc bridge in Java to do just this in the past, but performance through ODBC is not great. I would suggest looking at something like http://jtds.sourceforge.net/ which is a pure Java driver that you can drop into a simple Groovy script like the following:
import groovy.sql.Sql
sql = Sql.newInstance( 'jdbc:jtds:sqlserver://serverName/dbName-CLASS;domain=domainName',
'username', 'password', 'net.sourceforge.jtds.jdbc.Driver' )
sql.eachRow( 'select * from tableName' ) {
println "$it.id -- ${it.firstName} --"
// probably write to mysql connection here or write to file, compress, transfer, load
}
The following performance numbers give you a feel for how it might perform:
http://jtds.sourceforge.net/benchTest.html
You may find some performance advantages to dumping data to a mysql dumpfile format and using mysql loaddata instead of writing row by row. MySQL has some significant performance improvements for large data sets if you load infile's and doing things like atomic table swaps.
We use something like this to quickly load large datafiles into mysql from one system to another e.g. This is the fastest mechanism to load data into mysql. But real time row by row might be a simple loop to do in groovy + some table to keep track of what row had been moved.
mysql> select * from table into outfile 'tablename.dat';
shell> myisamchk --keys-used=0 -rq '/data/mysql/schema_name/tablename'
mysql> load data infile 'tablename.dat' into table tablename;
shell> myisamchk -rq /data/mysql/schema_name/tablename
mysql> flush tables;
mysql> exit;
shell> rm tablename.dat
The best way I have found to transfer SQL data (if you have the space) is a SQL dump in one language and then to use a converting software tool (or perl script, both are prevalent) to convert the SQL dump from MSSQL to MySQL. See my answer to this question about what converter you may be interested in :) .
We've used the ado.net driver for mysql in ssis with quite a bit of success. Basically, install the driver on the machine with integration services installed, restart bids, and it should show up in the driver list when you create an ado.net connection manager.
As for replication, what exactly are you trying to accomplish?
If you are monitoring changes, treat it as a type 1 slowly changing dimension (data warehouse terminology, but same principal applies). Insert new records, update changed records.
If you are only interested in new records and have no plans to update previously loaded data, try an incremental load strategy. Insert records where source.id > max(destination.id).
After you've tested the package, schedule a job in the sql server agent to run the package every x minutes.
Cou can also try the following.
http://kofler.info/english/mssql2mysql/
I tried this a longer time before and it worked for me. But I woudn't recommend it to you.
What is the real problem, what you try to do?
DonĀ“t you get a MSSQL DB Connection, for example from Linux?