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