![]() |
Database Auto-Optimizer
This is a module to automatically optimize all of the tables in your database overnight through the use of a cronjob. Also performs repairs as needed.
First create a file in your Xcart root directory named: db_optimizer.php Put the following inside that file: Code:
<?php Next create a cronjob that runs sometime each day whenever your site has the least amount of traffic. Use the cron command: php /absolute-path-to-this-file/db_optimizer.php Substitute the data in the constants section of this file with your specific information. Also replace absolute-path-to's with the appropriate path. That's all there is to it. If there are any problems, let me know! :wink: |
Hi NuAlpha
Thank you for sharing this file. I've been looking for something like this for sometime. I ran the script from my browser without any problems. Next I'll try to set up the cron file. Just need to learn a bit more about doing this. Once again, thank you |
Here is an update of the code. Noticed a potential security issue. This fix might disable the script on some people's servers so if it does and you have another solution, then just remove the condition. The condition if (empty($_SERVER['REMOTE_ADDR']) && empty($_SERVER['SERVER_ADDR'])) below is meant to prevent anyone but the cronjob from running the script.
I have also added the ability to optimize more than one database. This is useful if, for instance, you have your stie stats recorded to a seperate database. Code:
<?php |
Not being one that is all that familiar with mysql (thats what I pay others for), what happens to a database when it's being optimised?? i.e what does it actually do?
|
Quote:
From the MySQL manual... "Deleted records are maintained in a linked list and subsequent INSERT operations reuse old record positions. You can use OPTIMIZE TABLE to reclaim the unused space and to defragment the data file." Basically, optimizing the tables keeps record access as fast as possible. This module also detects and auto-repairs any damaged tables as well. :) |
I guess I just run a bash script via cron that optimizes my tables and then backs them up.
Code:
#Change the 5 variables below to fit your computer/backup |
Quote:
Looks good. Some people can't access bash in that manner because of security restrictions set in place by the company which provides their servers. Would work fine for a dedicated server though. |
Great job NuAlpha 8)
And with regards to security mentioned further up in thread, if you can run cronjobs on your account then just put db_optimizer.php in a non web-accessible directory ie: one directory up from your public_html directory |
Quote:
HAH!! Good idea! And here I was rewriting the code to solve a simple problem. For some reason clever code always seems like the best solution in my mind even though there is probably some simpler way of taking care of the issue at hand. :lol: |
When I run db_optimizer.php, I get the following mySQL error:
Code:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/db_optimizer.php on line 21 I've tried the variations of db_optimizer.php given here, but they both result in the same error. I would prefer to run the bash script rather than the php anyway, and I have access to bash, but I have no idea how set up and run bash scripts. Any help on getting either method to work is much appreciated. Thanks! =========== mySQL: 4.1.11 Apache/1.3.33 (Unix) PHP/4.3.11 mod_perl/1.29 xcart: 4.0.15 |
All times are GMT -8. The time now is 01:25 PM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.