I am trying to recover the content of MySQL binary log file using PHP from the virtual machine I am running. I am running CentOS on the virtual machine and LAMP.
I have 4 virtual machines running on a virtual network, they are all located on the same subnet and the MySQL databases are up and running and the replication is working.
I am trying to recover the content of the master’s bin log using PHP. Normally the content of the file can be recovered using the following command:
shell> mysqlbinlog [options] log_file
I have written a PHP script that executes a shell command on the local machine:
$cmd="mysqlbinlog /var/lib/mysql/provider-bin.000003";
echo "<pre>".shell_exec($cmd);
I have checked that the provider-bin.000003 exists and that the shell_exec function works and is not disabled.
The problem takes place when I execute the command on the console I get the content of the bin file, but the PHP script does not output the expected result.
It’s outputting the following instead:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
Update: exec
I have also tried using the exec function adn it outputs the same:
$cmd="mysqlbinlog /var/lib/mysql/provider-bin.000003";
print_r(exec($cmd,$ret_value)) . "<br />DONE";
echo "<pre>".shell_exec($cmd." 2>&1");
Much appreciated
I have figured it out a couple of days agon, so here is the solution.
Although mysqlbinlog gets executed by PHP it is still not able to read MySQL’s binary files if run from a browser, for some reason it shows file does not exists. That was never my plan, so a cron job and sh file did the trick for me.
Cron job will run the script as a root without explosing its content to the outside world.
Now my circular replication failover script is running happily.