免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1641 | 回复: 1

php批量转换Mysql表引擎 [复制链接]

论坛徽章:
0
发表于 2011-12-27 16:39 |显示全部楼层
php批量转换Mysql表引擎







有些时候可能需要批量转换Mysql表的引擎,如下为PHP操作实现

Php代码
  1. 1.<?php   
  2. 2./**  
  3. 3. * 批量转换Mysql表引擎  
  4. 4. */  
  5. 5.  
  6. 6.error_reporting(E_ALL);   
  7. 7.  
  8. 8.// 数据库连接配置   
  9. 9.$host     = 'localhost';   
  10. 10.$username = 'root';   
  11. 11.$passwd   = '';   
  12. 12.$database = 'test';   
  13. 13.  
  14. 14.// 要转换的库名配置,多库转换增加配置元素即可   
  15. 15.$configs  = array($database);   
  16. 16.  
  17. 17.// 转换配置   
  18. 18.$convert_rule = array(   
  19. 19.    'from' => 'InnoDB',   
  20. 20.    'to'   => 'MyISAM'  
  21. 21.);   
  22. 22.  
  23. 23.mysql_engine_convert();   
  24. 24.  
  25. 25./**  
  26. 26. * 转换函数  
  27. 27. */  
  28. 28.function mysql_engine_convert()   
  29. 29.{   
  30. 30.    global $host,$username,$passwd,$configs,$convert_rule;   
  31. 31.  
  32. 32.    if ( ($conn = mysql_connect($host, $username, $passwd)) !== false)   
  33. 33.    {   
  34. 34.        foreach ($configs as $db_name)   
  35. 35.        {   
  36. 36.            mysql_select_db($db_name) or exit('Not found db: '. $db_name);   
  37. 37.            $tables = mysql_query("SHOW FULL TABLES");   
  38. 38.            while ($table = mysql_fetch_row($tables))   
  39. 39.            {   
  40. 40.                if ($table[1] === 'VIEW') continue;   
  41. 41.  
  42. 42.                $sql = "SHOW TABLE STATUS from {$db_name} where Name='{$table[0]}' ";   
  43. 43.                if ($result = mysql_query($sql))   
  44. 44.                {   
  45. 45.                    $table_status = mysql_fetch_row($result);   
  46. 46.                       
  47. 47.                    if (strtolower($table_status[1]) == strtolower($convert_rule['from']))   
  48. 48.                        mysql_query("ALTER TABLE {$table[0]} ENGINE = {$convert_rule['to']}");   
  49. 49.                }   
  50. 50.            }   
  51. 51.            echo $db_name,':All tables ENGINE is ',$convert_rule['to'],"\n";   
  52. 52.        }   
  53. 53.           
  54. 54.    } else {   
  55. 55.        echo "db error\n";   
  56. 56.    }   
  57. 57.  
  58. 58.}  
复制代码

论坛徽章:
0
发表于 2011-12-27 19:17 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP