Answers:
<?php
// connect your database here first
//
// Actual code starts here
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your_database_name'
AND ENGINE = 'MyISAM'";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
$tbl = $row[0];
$sql = "ALTER TABLE `$tbl` ENGINE=INNODB";
mysql_query($sql);
}
?>
mysql_*
界面已从版本7中弃用并删除。请勿按原样使用此代码。
运行此SQL语句(在MySQL客户端,phpMyAdmin或任何地方)以检索数据库中的所有MyISAM表。
用name_of_your_db
数据库名称替换变量的值。
SET @DATABASE_NAME = 'name_of_your_db';
SELECT CONCAT('ALTER TABLE `', table_name, '` ENGINE=InnoDB;') AS sql_statements
FROM information_schema.tables AS tb
WHERE table_schema = @DATABASE_NAME
AND `ENGINE` = 'MyISAM'
AND `TABLE_TYPE` = 'BASE TABLE'
ORDER BY table_name DESC;
然后,复制输出并作为新的SQL查询运行。
ORDER BY table_name DESC
)
CONCAT('ALTER TABLE ', table_name, ' ENGINE=InnoDB;')
为CONCAT('ALTER TABLE ',@DATABASE_NAME,'.', table_name, ' ENGINE=InnoDB;')
SELECT CONCAT('ALTER TABLE `', table_schema, '`.`', table_name, '` ENGINE=InnoDB;') AS sql_statements FROM information_schema.tables WHERE table_schema NOT IN ('information_schema', 'performance_schema', 'mysql') AND engine = 'MyISAM' AND table_type = 'BASE TABLE' ORDER BY table_schema,table_name
SELECT CONCAT('ALTER TABLE ',TABLE_NAME,' ENGINE=InnoDB;')
FROM INFORMATION_SCHEMA.TABLES
WHERE ENGINE='MyISAM'
AND table_schema = 'mydatabase';
奇迹般有效。
这将为您提供所有表的列表以及您可以批量运行的alter查询
在下面的脚本中,将<username>,<password>和<schema>替换为您的特定数据。
为了显示可以复制粘贴到mysql客户端会话中的语句,请输入以下内容:
echo 'SHOW TABLES;' \
| mysql -u <username> --password=<password> -D <schema> \
| awk '!/^Tables_in_/ {print "ALTER TABLE `"$0"` ENGINE = InnoDB;"}' \
| column -t \
要简单地执行更改,请使用以下命令:
echo 'SHOW TABLES;' \
| mysql -u <username> --password=<password> -D <schema> \
| awk '!/^Tables_in_/ {print "ALTER TABLE `"$0"` ENGINE = InnoDB;"}' \
| column -t \
| mysql -u <username> --password=<password> -D <schema>
信用:这是本文概述的内容的变体。
使用它作为phpMyAdmin中的sql查询
SELECT CONCAT('ALTER TABLE ',table_schema,'.',table_name,' engine=InnoDB;')
FROM information_schema.tables
WHERE engine = 'MyISAM';
您可以在mysql命令行工具中执行以下语句:
echo "SELECT concat('ALTER TABLE `',TABLE_NAME,'` ENGINE=InnoDB;')
FROM Information_schema.TABLES
WHERE ENGINE != 'InnoDB' AND TABLE_TYPE='BASE TABLE'
AND TABLE_SCHEMA='name-of-database'" | mysql > convert.sql
您可能需要使用以下命令指定用户名和密码:mysql -u username -p结果是一个SQL脚本,您可以将其通过管道传送回mysql:
mysql name-of-database < convert.sql
在上面的语句和命令行中替换“数据库名称”。
-bash: ,TABLE_NAME,: command not found
非常简单,只需复制和粘贴两个步骤:
第1步。
SET @DATABASE_NAME = 'name_of_your_db';
SELECT CONCAT('ALTER TABLE `', table_name, '` ENGINE=InnoDB;') AS sql_statements FROM information_schema.tables AS tb WHERE table_schema = @DATABASE_NAME AND `ENGINE` = 'MyISAM' AND `TABLE_TYPE` = 'BASE TABLE' ORDER BY table_name DESC;
(将所有结果复制并粘贴到sql选项卡中)
第2步:(将所有结果复制到sql选项卡中)并粘贴到以下行中
开始交易;
承诺;
例如。开始交易;
ALTER TABLE admin_files
ENGINE = InnoDB;
承诺;
要为所有非系统模式中的所有表生成ALTER语句,请按这些模式/表的顺序运行以下命令:
SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.', table_name, ' ENGINE=InnoDB;') AS sql_statements
FROM information_schema.tables
WHERE TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'innodb', 'sys', 'tmp')
AND `ENGINE` = 'MyISAM'
AND `TABLE_TYPE` = 'BASE TABLE'
ORDER BY TABLE_SCHEMA, table_name DESC;
之后,通过客户端运行这些查询以执行更改。
它尚未被提及,因此我为后代编写:
如果您要在数据库服务器之间迁移(或者有其他原因要转储并重新加载dta),则可以修改以下命令的输出mysqldump
:
mysqldump --no-data DBNAME | sed 's/ENGINE=MyISAM/ENGINE=InnoDB/' > my_schema.sql;
mysqldump --no-create-info DBNAME > my_data.sql;
然后再次加载:
mysql DBNAME < my_schema.sql && mysql DBNAME < my_data.sql
(此外,根据我有限的经验,这可能比更改表“实时”的过程要快得多。这可能取决于数据和索引的类型。)
这是针对Django用户的一种方法:
from django.core.management.base import BaseCommand
from django.db import connections
class Command(BaseCommand):
def handle(self, database="default", *args, **options):
cursor = connections[database].cursor()
cursor.execute("SHOW TABLE STATUS");
for row in cursor.fetchall():
if row[1] != "InnoDB":
print "Converting %s" % row[0],
result = cursor.execute("ALTER TABLE %s ENGINE=INNODB" % row[0])
print result
将其添加到文件夹management / commands /下的应用中,然后可以使用manage.py命令转换所有表:
python manage.py convert_to_innodb
在mysql内部,您可以使用文本编辑器使用搜索/替换:
SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'myisam';
注意:您可能应该忽略information_schema和mysql,因为“实现某些MySQL内部结构的mysql和information_schema数据库仍然使用MyISAM。特别是,您不能将授予表切换为使用InnoDB。” (http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se.html)
无论如何,请注意要忽略和运行的表:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'myisam';
现在,只需将列表复制/粘贴到文本编辑器中,然后搜索/替换“ |” 与“ ALTER TABLE”等
然后,您将获得一个像这样的列表,您可以将其简单地粘贴到mysql终端中:
ALTER TABLE arth_commentmeta ENGINE=Innodb;
ALTER TABLE arth_comments ENGINE=Innodb;
ALTER TABLE arth_links ENGINE=Innodb;
ALTER TABLE arth_options ENGINE=Innodb;
ALTER TABLE arth_postmeta ENGINE=Innodb;
ALTER TABLE arth_posts ENGINE=Innodb;
ALTER TABLE arth_term_relationships ENGINE=Innodb;
ALTER TABLE arth_term_taxonomy ENGINE=Innodb;
ALTER TABLE arth_terms ENGINE=Innodb;
ALTER TABLE arth_usermeta ENGINE=Innodb;
如果您的文本编辑器不能轻松做到这一点,那么这是另一种解决方案,可以从linux终端获取数据库的一个前缀的类似列表(可以粘贴到mysql中):
mysql -u [username] -p[password] -B -N -e 'show tables like "arth_%"' [database name] | xargs -I '{}' echo "ALTER TABLE {} ENGINE=INNODB;"
一个普通的MySQL版本。
您可以简单地启动mysql可执行文件,使用数据库并复制粘贴查询。
这会将当前数据库中的所有MyISAM表转换为INNODB表。
DROP PROCEDURE IF EXISTS convertToInnodb;
DELIMITER //
CREATE PROCEDURE convertToInnodb()
BEGIN
mainloop: LOOP
SELECT TABLE_NAME INTO @convertTable FROM information_schema.TABLES
WHERE `TABLE_SCHEMA` LIKE DATABASE()
AND `ENGINE` LIKE 'MyISAM' ORDER BY TABLE_NAME LIMIT 1;
IF @convertTable IS NULL THEN
LEAVE mainloop;
END IF;
SET @sqltext := CONCAT('ALTER TABLE `', DATABASE(), '`.`', @convertTable, '` ENGINE = INNODB');
PREPARE convertTables FROM @sqltext;
EXECUTE convertTables;
DEALLOCATE PREPARE convertTables;
SET @convertTable = NULL;
END LOOP mainloop;
END//
DELIMITER ;
CALL convertToInnodb();
DROP PROCEDURE IF EXISTS convertToInnodb;
我是新手,必须找到自己的解决方案,因为Web上的mysql命令通常充斥着拼写错误,这对刚起步的人们来说是一个现实的噩梦。这是我的解决方案。
我不是使用每个表一个命令,而是使用excel一次准备了几十个命令(准备复制和粘贴)。
怎么样?扩展您的腻子窗口并输入mysql,然后运行命令“ SHOW TABLE STATUS;”。并将输出复制/粘贴到Microsoft Excel。转到“数据”选项卡,然后使用“文本到列”功能通过空格键分隔列。然后,按显示表类型的列对列进行排序,并删除表已采用InnoDb格式的所有行(因为我们不需要针对它们运行命令,因此它们已经完成)。然后在表格列的左侧添加2列,在右侧添加2列。然后将命令的第一部分粘贴到第1列中(请参见下文)。第2列应仅包含一个空格。第3列是您的表格列。第4列应仅包含一个空格。第5列是命令的最后一部分。它看起来应该像这样:
column-1 column-2 column-3 column-4 column-5
ALTER TABLE t_lade_tr ENGINE=InnoDB;
ALTER TABLE t_foro_detail_ms ENGINE=InnoDB;
ALTER TABLE t_ljk_ms ENGINE=InnoDB;
然后一次复制并粘贴大约5行到mysql中。这将一次转换约5。我注意到如果一次执行的操作多于一次,则命令将失败。
就我而言,我正在从默认MyISAM的MySQL实例迁移到默认InnoDB的MariaDB实例。
在旧服务器上运行:
mysqldump -u root -p --skip-create-options --all-databases > migration.sql
--skip-create-options可确保数据库服务器在加载数据时使用默认存储引擎,而不是MyISAM。
mysql -u root -p < migration.sql
这引发了有关创建mysql.db的错误,但是现在一切正常:)
试试这个shell脚本
DBENGINE='InnoDB' ;
DBUSER='your_db_user' ;
DBNAME='your_db_name' ;
DBHOST='your_db_host'
DBPASS='your_db_pass' ;
mysqldump --add-drop-table -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME > mtest.sql; mysql -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME -Nse "SHOW TABLES;" | while read TABLE ; do mysql -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME -Nse "ALTER TABLE $TABLE ENGINE=$DBENGINE;" ; done
此实用程序脚本的一些修复
SET @DATABASE_NAME = 'Integradb';
SELECT CONCAT('ALTER TABLE ', table_schema, '.', table_name, ' ENGINE=InnoDB;') AS sql_statements
FROM information_schema.tables AS tb
WHERE table_schema = @DATABASE_NAME
AND `ENGINE` = 'MyISAM'
AND `TABLE_TYPE` = 'BASE TABLE'
ORDER BY table_name DESC;
<?php
// connect your database here first
mysql_connect('host', 'user', 'pass');
$databases = mysql_query('SHOW databases');
while($db = mysql_fetch_array($databases)) {
echo "database => {$db[0]}\n";
mysql_select_db($db[0]);
$tables = mysql_query('SHOW tables');
while($tbl = mysql_fetch_array($tables)) {
echo "table => {$tbl[0]}\n";
mysql_query("ALTER TABLE {$tbl[0]} ENGINE=MyISAM");
}
}
这是一个简单的php脚本。
<?php
@error_reporting(E_ALL | E_STRICT);
@ini_set('display_errors', '1');
$con = mysql_connect('server', 'user', 'pass');
$dbName = 'moodle2014';
$sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '".$dbName."';";
$rs = mysql_query($sql, $con);
$count = 0;
$ok = 0;
while($row = mysql_fetch_array($rs)){
$count ++;
$tbl = $row[0];
$sql = "ALTER TABLE ".$dbName.".".$tbl." ENGINE=INNODB;";
$resultado = mysql_query($sql);
if ($resultado){
$ok ++;
echo $sql."<hr/>";
}
}
if ($count == $ok){
echo '<div style="color: green"><b>ALL OK</b></div>';
}else{
echo '<div style="color: red"><b>ERRORS</b>Total tables: '.$count.', updated tables:'.$ok.'</div>';
}
<?php
// Convert all MyISAM tables to INNODB tables in all non-special databases.
// Note: With MySQL less than 5.6, tables with a fulltext search index cannot be converted to INNODB and will be skipped.
if($argc < 4)
exit("Usage: {$argv[0]} <host> <username> <password>\n");
$host = $argv[1];
$username = $argv[2];
$password = $argv[3];
// Connect to the database.
if(!mysql_connect($host, $username, $password))
exit("Error opening database. " . mysql_error() . "\n");
// Get all databases except special ones that shouldn't be converted.
$databases = mysql_query("SHOW databases WHERE `Database` NOT IN ('mysql', 'information_schema', 'performance_schema')");
if($databases === false)
exit("Error showing databases. " . mysql_error() . "\n");
while($db = mysql_fetch_array($databases))
{
// Select the database.
if(!mysql_select_db($db[0]))
exit("Error selecting database: {$db[0]}. " . mysql_error() . "\n");
printf("Database: %s\n", $db[0]);
// Get all MyISAM tables in the database.
$tables = mysql_query("SHOW table status WHERE Engine = 'MyISAM'");
if($tables === false)
exit("Error showing tables. " . mysql_error() . "\n");
while($tbl = mysql_fetch_array($tables))
{
// Convert the table to INNODB.
printf("--- Converting %s\n", $tbl[0]);
if(mysql_query("ALTER TABLE `{$tbl[0]}` ENGINE = INNODB") === false)
printf("--- --- Error altering table: {$tbl[0]}. " . mysql_error() . "\n");
}
}
mysql_close();
?>
对于mysqli connect;
<?php
$host = "host";
$user = "user";
$pass = "pss";
$database = "db_name";
$connect = new mysqli($host, $user, $pass, $database);
// Actual code starts here Dont forget to change db_name !!
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'db_name'
AND ENGINE = 'MyISAM'";
$rs = $connect->query($sql);
while($row = $rs->fetch_array())
{
$tbl = $row[0];
$sql = "ALTER TABLE `$tbl` ENGINE=INNODB";
$connect->query($sql);
} ?>
还有另一种选择...这是ansible中的操作方法。它假定数据库名称在其中dbname
,并且您已经配置了访问权限。
- name: Get list of DB tables that need converting to InnoDB
command: >
mysql --batch --skip-column-names --execute="SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = '{{ dbname }}' AND ENGINE = 'MyISAM';"
register: converttables
check_mode: no
changed_when: False
- name: Convert any unconverted tables
command: >
mysql --batch --skip-column-names --execute="ALTER TABLE `{{ dbname }}`.`{{ item }}` ENGINE = InnoDB;"
with_items: "{{ converttables.stdout_lines }}"