php批量轉換mysql表引擎
添加時間:2015-1-3 21:00:25
添加:
思海網絡
有些時候可能需要批量轉換mysql表的引擎,如下為php操作實現
<?php
/**
* 批量轉換mysql表引擎
*/
error_reporting(e_all);
// 數據庫連接配置
$host = 'localhost';
$username = 'root';
$passwd = '';
$database = 'test';
// 要轉換的庫名配置,多庫轉換增加配置元素即可
$configs = array($database);
// 轉換配置
$convert_rule = array(
'from' => 'innodb',
'to' => 'myisam'
);
mysql_engine_convert();
/**
* 轉換函數
*/
function mysql_engine_convert()
{
global $host,$username,$passwd,$configs,$convert_rule;
if ( ($conn = mysql_connect($host, $username, $passwd)) !== false)
{
foreach ($configs as $db_name)
{
mysql_select_db($db_name) or exit('not found db: '. $db_name);
$tables = mysql_query("show full tables");
while ($table = mysql_fetch_row($tables))
{
if ($table[1] === 'view') continue;
$sql = "show table status from {$db_name} where name='{$table[0]}' ";
if ($result = mysql_query($sql))
{
$table_status = mysql_fetch_row($result);
if (strtolower($table_status[1]) == strtolower($convert_rule['from']))
mysql_query("alter table {$table[0]} engine = {$convert_rule['to']}");
}
}
echo $db_name,':all tables engine is ',$convert_rule['to'],"\n";
}
} else {
echo "db error\n";
}
<?php
/**
* 批量轉換mysql表引擎
*/
error_reporting(e_all);
// 數據庫連接配置
$host = 'localhost';
$username = 'root';
$passwd = '';
$database = 'test';
// 要轉換的庫名配置,多庫轉換增加配置元素即可
$configs = array($database);
// 轉換配置
$convert_rule = array(
'from' => 'innodb',
'to' => 'myisam'
);
mysql_engine_convert();
/**
* 轉換函數
*/
function mysql_engine_convert()
{
global $host,$username,$passwd,$configs,$convert_rule;
if ( ($conn = mysql_connect($host, $username, $passwd)) !== false)
{
foreach ($configs as $db_name)
{
mysql_select_db($db_name) or exit('not found db: '. $db_name);
$tables = mysql_query("show full tables");
while ($table = mysql_fetch_row($tables))
{
if ($table[1] === 'view') continue;
$sql = "show table status from {$db_name} where name='{$table[0]}' ";
if ($result = mysql_query($sql))
{
$table_status = mysql_fetch_row($result);
if (strtolower($table_status[1]) == strtolower($convert_rule['from']))
mysql_query("alter table {$table[0]} engine = {$convert_rule['to']}");
}
}
echo $db_name,':all tables engine is ',$convert_rule['to'],"\n";
}
} else {
echo "db error\n";
}
} 詳情可參考http://www.cxybl.com/html/wlbc/Php/20120607/28510.html
關鍵字:mysql、表引擎、數據庫、連接
新文章:
- CentOS7下圖形配置網絡的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統后丟失windows啟動項
- CentOS單網卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網打印機IP講解
- CentOS7使用hostapd實現無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網絡重啟出錯
- 解決Centos7雙系統后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統有什么不同呢
- Centos 6.6默認iptable規則詳解