{"id":345,"date":"2012-04-25T12:22:30","date_gmt":"2012-04-25T12:22:30","guid":{"rendered":"http:\/\/www.plugged.in\/?p=345"},"modified":"2012-04-25T12:22:30","modified_gmt":"2012-04-25T12:22:30","slug":"how-to-change-storage-engine-to-innodb-in-mysql","status":"publish","type":"post","link":"https:\/\/www.veriteknik.net.tr\/en\/how-to-change-storage-engine-to-innodb-in-mysql\/","title":{"rendered":"How to change storage engine to InnoDB in MySQL ?"},"content":{"rendered":"<p>You&#8217;ve designed a database with MyISAM and suddenly realized that you need ACID\u00a0(<em>atomicity,\u00a0consistency,\u00a0isolation,\u00a0durability<\/em>) compliant features and transactions.<\/p>\n<p>Right, you should use <a title=\"wikipedia: InnoDB\" href=\"http:\/\/en.wikipedia.org\/wiki\/InnoDB\" target=\"_blank\">InnoDB<\/a>. But how to change storage engine for your previous tables ?<\/p>\n<p>As far as i know, changing the storage engine for tables is easy by alter command.<\/p>\n<p>But there&#8217;s one flaw. You should do it one by one.<\/p>\n<p>On the other hand, you could either write complex SQL queries, or you could write a php code for bulk changes.<\/p>\n<p>Today i&#8217;m gonna show you how to change your storage engine with a tiny code.<\/p>\n<p><span style=\"color: #e01e2a; font-size: small;\">Please notice that if you decide to use my code it is your responsibility.<\/span><\/p>\n<blockquote><p>$db = &#8216;your_database_name_here&#8217;;<\/p>\n<p>$dsn = &#8220;mysql:host=localhost;dbname=$db&#8221;;<\/p>\n<p>$username = &#8220;your_MySQL_username&#8221;;<\/p>\n<p>$password = &#8220;your_password&#8221;;<\/p>\n<p>$current = &#8216;MyISAM&#8217;; \/\/ Current storage engine<\/p>\n<p>$target = &#8216;INNODB&#8217;; \/\/ Target storage engine<\/p>\n<p>&nbsp;<\/p>\n<p>try {<\/p>\n<p style=\"padding-left: 30px;\">$pdo = new PDO($dsn, $username, $password);<\/p>\n<p>}<\/p>\n<p>catch(PDOException $e) {<\/p>\n<p style=\"padding-left: 30px;\">die(&#8220;Could not connect to the database\\n&#8221;);<\/p>\n<p>}<\/p>\n<p>$result = $pdo-&gt;query(&#8220;SELECT TABLE_NAME\u00a0FROM information_schema.TABLES\u00a0WHERE TABLE_SCHEMA = &#8216;$db&#8217;\u00a0AND ENGINE = &#8216;$current'&#8221;);<\/p>\n<p>foreach($result as $row) {<\/p>\n<p style=\"padding-left: 30px;\">$success = $pdo-&gt;exec(&#8220;ALTER TABLE {$row[&#8216;TABLE_NAME&#8217;]} ENGINE = $target&#8221;);<\/p>\n<p style=\"padding-left: 30px;\">if($success) {<\/p>\n<p style=\"padding-left: 60px;\">echo &#8220;{$row[&#8216;TABLE_NAME&#8217;]} &#8211; success\\n&#8221;;<\/p>\n<p style=\"padding-left: 30px;\">}\u00a0else {<\/p>\n<p style=\"padding-left: 60px;\">$info = $pdo-&gt;errorInfo();<\/p>\n<p style=\"padding-left: 60px;\">echo &#8220;{$row[&#8216;TABLE_NAME&#8217;]} &#8211; failed: $info[2]\\n&#8221;;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>That&#8217;s all.<\/p>\n<p><span style=\"color: #e01e2a;\">If you see &#8220;failed&#8221; messages, there&#8217;s probably a permission issue with information_schema table.<\/span><\/p>\n<p>In that case you should enter MySQL command line, and repeat the procedure there.<\/p>\n<p>To list storage engines for tables, you should execute;<\/p>\n<blockquote><p>SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = &#8216;your_database_name&#8217;;<\/p><\/blockquote>\n<p>This will show you which tables are using which storage engine.<\/p>\n<p>After that you should execute;<\/p>\n<blockquote><p>USE your_database_name;<\/p><\/blockquote>\n<p>When you see &#8220;Database changed&#8221; message, you should execute;<\/p>\n<blockquote><p>ALTER TABLE your_table_name ENGINE = INNODB;<\/p><\/blockquote>\n<p>Have a nice life with your magnificent database storage engine InnoDB !<\/p>\n<p>&nbsp;<\/p>\n<p>Please check out other articles about InnoDB and Transactions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve designed a database with MyISAM and suddenly realized that you need ACID\u00a0(atomicity,\u00a0consistency,\u00a0isolation,\u00a0durability) compliant features and transactions. Right, you should use InnoDB. But how to change storage engine for your previous tables ? As far as i know, changing the storage engine for tables is easy by alter command. But there&#8217;s one flaw. You should [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[363,364],"tags":[],"yst_prominent_words":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-databases","category-mysql-databases"],"jetpack_featured_media_url":"","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Ahmet Polat","author_link":"https:\/\/www.veriteknik.net.tr\/en\/author\/ahmet-polat\/"},"uagb_comment_info":0,"uagb_excerpt":"You&#8217;ve designed a database with MyISAM and suddenly realized that you need ACID\u00a0(atomicity,\u00a0consistency,\u00a0isolation,\u00a0durability) compliant features and transactions. Right, you should use InnoDB. But how to change storage engine for your previous tables ? As far as i know, changing the storage engine for tables is easy by alter command. But there&#8217;s one flaw. You should&hellip;","_links":{"self":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":0,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/tags?post=345"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/yst_prominent_words?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}