{"id":42,"date":"2012-06-08T13:23:39","date_gmt":"2012-06-08T13:23:39","guid":{"rendered":"http:\/\/vickistan.com\/?p=42"},"modified":"2014-11-20T15:44:27","modified_gmt":"2014-11-20T15:44:27","slug":"cool-mysql-command-list","status":"publish","type":"post","link":"https:\/\/vickistan.com\/?p=42","title":{"rendered":"Cool MySQL command list"},"content":{"rendered":"<p># to see the full CREATE TABLE statement and show table engine<br \/>\nshow create table table_name\\G<\/p>\n<p>#To see full CREATE TABLE statement on all tables in a database<br \/>\nexport database=&#8221;dbname&#8221;;for table in `mysql -s &#8211;column-name=false -e &#8220;SHOW TABLES&#8221; $database`;do mysql -e &#8220;SHOW CREATE TABLE $table&#8221; $database;done<\/p>\n<p>#convert a table from MyISAM to InnoDB<br \/>\nALTER TABLE table_name ENGINE=InnoDB;<\/p>\n<p>#watch the processlist fly by (from the command line\/not a MySQL command)<br \/>\nwatch &#8220;mysql -e &#8216;show processlist;'&#8221;<\/p>\n<p>#List mysql users from the command line.<br \/>\nmysql -u root -B -N -p -e &#8220;SELECT user, host FROM user&#8221; mysql<\/p>\n<p>#Show grants for particular myaql user instead of the one you are logged in as<br \/>\nSHOW GRANTS [FOR user]<\/p>\n<p>#Add a new user with only SELECT and UPDATE on a specific database<br \/>\ngrant SELECT,UPDATE on database.* to &#8216;user&#8217;@&#8217;%&#8217; IDENTIFIED BY &#8216;PASSWORD&#8217;;<\/p>\n<p>#Add a new user with all privileges on a specific database<br \/>\ngrant all on database.* to user@&#8217;IP_ADDRESS&#8217; IDENTIFIED BY &#8216;PASSWORD&#8217;;<\/p>\n<p>#List all tables in all databases<br \/>\nUSE information_schema;<br \/>\nSELECT TABLE_SCHEMA, TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA NOT IN (&#8216;mysql&#8217;, &#8216;information_schema&#8217;, &#8216;performance_schema&#8217;) ORDER BY TABLE_SCHEMA, TABLE_NAME;<\/p>\n<p>#Dump all databases to a file<br \/>\nmysqldump &#8211;all-databases > \/tmp\/all.sql<\/p>\n<p>#To import a database<br \/>\nmysql -u username -p -h localhost DATA-BASE-NAME &lt; data.sql<\/p>\n<p>#To turn on slow query logging from the mysql prompt<\/p>\n<p>#Set the long query time to whatever is appropriate, the default is 10 seconds<br \/>\nmysql> SET GLOBAL long_query_time=1;<br \/>\n#Set the location that the slow query log should go to, make sure the folder path exists and that the mysql user has write permissions to it<br \/>\nmysql> SET GLOBAL slow_query_log_file=&#8217;\/var\/lib\/mysqllogs\/slow-log&#8217;;<br \/>\n#And finally set a variables to enable logging<br \/>\nmysql> SET GLOBAL slow_query_log=1;<\/p>\n<p>#To Repair and Optimize all MySql tables:<br \/>\nmysqlcheck \u2013u root \u2013p \u2013auto-repair \u2013check \u2013optimize \u2013all-databases<\/p>\n<p>#To get the sizes of your databases<br \/>\nSELECT table_schema &#8220;Data Base Name&#8221;, sum( data_length + index_length ) \/ 1024 \/ 1024 &#8220;Data Base Size in MB&#8221; FROM information_schema.TABLES GROUP BY table_schema;<br \/>\n#For extra points, tune your Innodb Buffer Pool accordingly.<\/p>\n<p>#To dump tables t1, t2, t3, t7 from the database test:<br \/>\nmysqldump test t1 t3 t7 > tabledump.sql<\/p>\n<p>#Determine whether a table is indexed:<br \/>\nshow index from [table name]<\/p>\n","protected":false},"excerpt":{"rendered":"<p># to see the full CREATE TABLE statement and show table engine show create table table_name\\G #To see full CREATE TABLE statement on all tables in a database export database=&#8221;dbname&#8221;;for table in `mysql -s &#8211;column-name=false -e &#8220;SHOW TABLES&#8221; $database`;do mysql &hellip; <a href=\"https:\/\/vickistan.com\/?p=42\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":33,"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":623,"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions\/623"}],"wp:attachment":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}