Penner Blog

Sarah & Penner’s happy life ;)

Archive for 5月, 2008

Linux 64位, MySQL, Swap & Memory 优化

星期日
5 25,2008

MySQL的性能优化文章有很多,这里介绍个通过优化Swap & Memory来提高性能的方法。

The VM for Linux prefers system cache over application memory. What does this mean? The best way I can explain is by example.

Imagine you have 32 GB of RAM
MySQL is set to take 20 GB of RAM for a process based buffer and up to 6M for the various thread buffers.

Over a period of time the box swaps. The only thing that is running is mysql and its memory size is around 21GB for resident memory. Why does swap grow when there is plenty of memory? The reason is when a memory alloc is needed (thread based buffer is tickled) the VM will choose to use swap over allocating from the system cache, when there is not enough free memory.

DO NOT TURN OFF SWAP to prevent this. Your box will crawl, kswapd will chew up a lot of the processor, Linux needs swap enabled, lets just hope its not used.

So how do you stop Nagios pages because of swap usage? Well if you have a few choices.

reboot the box

or

stop mysql && swapoff -a;swapon -a;

or just

swapoff -a;swapon -a;
(注意!如果你在MySQL正在使用Swap时执行,会把MySQL搞死,所以执行前一定要反复确认Swap没有被使用。)

Doing the latter command is rather scary and fun at the same time. Because you can either crash mysql or not. I just did the swap* commands live, I was very certain nothing was using swap and it worked. YAY no more pages and I didn’t have to shut down the service!

参考文章:
http://mysqldba.blogspot.com/2008/05/linux-64-bit-mysql-swap-and-memory.html

PHP Download File

  • Filed under: PHP, Tech
星期日
5 25,2008
  1. <?php
  2. $filename = $_GET['filename'];
  3.  
  4. // Modify this line to indicate the location of the files you want people to be able to download
  5. // This path must not contain a trailing slash. ie. /temp/files/download
  6. $download_path = "ficheros/";
  7.  
  8. // Make sure we can't download files above the current directory location.
  9. if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
  10. $file = str_replace("..", "", $filename);
  11.  
  12. // Make sure we can't download .ht control files.
  13. if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
  14.  
  15. // Combine the download path and the filename to create the full path to the file.
  16. $file = "$download_path$file";
  17.  
  18. // Test to ensure that the file exists.
  19. if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
  20.  
  21. // Extract the type of file which will be sent to the browser as a header
  22. $type = filetype($file);
  23.  
  24. // Get a date and timestamp
  25. $today = date("F j, Y, g:i a");
  26. $time = time();
  27.  
  28. // Send file headers
  29. header("Content-type: $type");
  30. header("Content-Disposition: attachment;filename=$filename");
  31. header("Content-Transfer-Encoding: binary");
  32. header('Pragma: no-cache');
  33. header('Expires: 0');
  34. // Send the file contents.
  35. set_time_limit(0);
  36. readfile($file);
  37. ?>