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规则详解