{"id":1431,"date":"2016-02-25T05:21:49","date_gmt":"2016-02-25T05:21:49","guid":{"rendered":"http:\/\/elbsolutions.com\/projects\/?p=1431"},"modified":"2022-02-03T11:24:34","modified_gmt":"2022-02-03T17:24:34","slug":"scripting-mysql-on-unix-to-clean-tables","status":"publish","type":"post","link":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/","title":{"rendered":"scripting mysql on unix to clean tables"},"content":{"rendered":"<p>One could clean the table, but on hostgator&#8217;s phpmyadmin in cpanel which is REALLY REALLY REALLY SLOW, it times out and loses connection before it is done cleaning. So &#8211; on another server which is lickity split, I have root access. To overcome the slowness, I first, copy the table which cleans it, then rename the original and rename the copy. This allows me to also have a backup table. Below\u00a0is my unix bash script:<\/p>\n<p>Here is a list of my heros who helped get me to my script below.<\/p>\n<ul>\n<li><a href=\"http:\/\/www.gensale.net\/537\/\">Passing Parameters To A MYSQL Query Inside A Shell Script<\/a><\/li>\n<li><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/batch-mode.html\">Using mysql in batch mode<\/a><\/li>\n<li><a href=\"http:\/\/databobjr.blogspot.ca\/2011\/07\/passing-parameters-to-mysql-query.html\">Passing Parameters To A MYSQL Query Inside A Shell Script<\/a><\/li>\n<li><span><a href=\"http:\/\/www.avajava.com\/tutorials\/lessons\/how-do-i-execute-an-sql-script-in-mysql.html\">How do I execute an SQL script in MySQL?<\/a><\/span><\/li>\n<\/ul>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\r\n#!\/bin\/bash\r\n\r\n## script name; cleanDirtyTable.sh\r\n## wrapper script to execute mysql script with variables\r\n\r\nARGS=2\r\n\r\nif &#x5B; $# -ne &quot;$ARGS&quot; ]\r\nthen\r\n\r\necho &quot;you passed $# parameters&quot;\r\n echo &quot;Usage: `basename $0` sql_table sql_password&quot;\r\n\r\nexit\r\nfi\r\n\r\nsqltable=$1\r\nsqlpassword=$2\r\nsql_script=cleanDirtyTable.sql\r\ndata_file=cleanDirtyTable_results.txt\r\n\r\n#run mysql query with paramenters\r\n\r\necho &quot;set @origTable=${sqltable}&quot;\r\n\r\n#\/usr\/bin\/mysql \u2013uUserName -pXXXXXXXXXX \u2013h localhost -D my_dBname -A -e &quot;set @origTable='${sqltable}'; source ${sql_script};&quot; #&amp;amp;amp;gt;${data_file};\r\n\r\n\/usr\/bin\/mysql -uUsername -p${sqlpassword} -h localhost -D my_dBname -A -e &quot;set @origTable='${sqltable}'; SOURCE ${sql_script};&quot;\r\nexit\r\n\r\n<\/pre>\n<p>Now here is the sql portion<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUSE fcs_jo151;\r\n\r\n-- SET @origTable = 'jos_fcls_masterquotewjobs';\r\n\r\nSET @ds1 = CONCAT('CREATE table `',@origTable, '_copy` LIKE `',@origTable,'`');\r\nSET @b =1;\r\nSET @ds2 = CONCAT('INSERT `', @origTable, '_copy` SELECT * FROM `',@origTable,'`;');\r\nSET @rename1 = CONCAT('RENAME TABLE `',@origTable,'` TO `', @origTable ,'_bak_',CURDATE(),'`;');\r\n\r\nSET @rename2 = CONCAT('RENAME TABLE `',@origTable,'_copy` TO `', @origTable ,'`;');\r\n\r\n\r\n\r\nPREPARE stmt FROM @ds1;\r\nEXECUTE stmt;\r\nDEALLOCATE PREPARE stmt;\r\n\r\nPREPARE stmt FROM @ds2;\r\nEXECUTE stmt;\r\nDEALLOCATE PREPARE stmt;\r\n\r\n\r\nPREPARE stmt FROM @rename1;\r\nEXECUTE stmt;\r\nDEALLOCATE PREPARE stmt;\r\n\r\n\r\nPREPARE stmt FROM @rename2;\r\nEXECUTE stmt;\r\nDEALLOCATE PREPARE stmt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One could clean the table, but on hostgator&#8217;s phpmyadmin in cpanel which is REALLY REALLY REALLY SLOW, it times out and loses connection before it is done cleaning. So &#8211; on another server which is lickity split, I have root access. To overcome the slowness, I first, copy the table which cleans it, then rename [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1431","post","type-post","status-publish","format-standard","hentry","category-general"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>scripting mysql on unix to clean tables - ELB Solutions.com Inc.<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"scripting mysql on unix to clean tables - ELB Solutions.com Inc.\" \/>\n<meta property=\"og:description\" content=\"One could clean the table, but on hostgator&#8217;s phpmyadmin in cpanel which is REALLY REALLY REALLY SLOW, it times out and loses connection before it is done cleaning. So &#8211; on another server which is lickity split, I have root access. To overcome the slowness, I first, copy the table which cleans it, then rename [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"ELB Solutions.com Inc.\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-25T05:21:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-03T17:24:34+00:00\" \/>\n<meta name=\"author\" content=\"Etienne Bley\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Etienne Bley\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/\"},\"author\":{\"name\":\"Etienne Bley\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"headline\":\"scripting mysql on unix to clean tables\",\"datePublished\":\"2016-02-25T05:21:49+00:00\",\"dateModified\":\"2022-02-03T17:24:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/\"},\"wordCount\":335,\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/\",\"name\":\"scripting mysql on unix to clean tables - ELB Solutions.com Inc.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\"},\"datePublished\":\"2016-02-25T05:21:49+00:00\",\"dateModified\":\"2022-02-03T17:24:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/scripting-mysql-on-unix-to-clean-tables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"scripting mysql on unix to clean tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\",\"name\":\"ELB Solutions.com Inc.\",\"description\":\"Bringing all your IT Pieces together\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\",\"name\":\"Etienne Bley\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"caption\":\"Etienne Bley\"},\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/author\\\/etienne-bley\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"scripting mysql on unix to clean tables - ELB Solutions.com Inc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/","og_locale":"en_US","og_type":"article","og_title":"scripting mysql on unix to clean tables - ELB Solutions.com Inc.","og_description":"One could clean the table, but on hostgator&#8217;s phpmyadmin in cpanel which is REALLY REALLY REALLY SLOW, it times out and loses connection before it is done cleaning. So &#8211; on another server which is lickity split, I have root access. To overcome the slowness, I first, copy the table which cleans it, then rename [&hellip;]","og_url":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/","og_site_name":"ELB Solutions.com Inc.","article_published_time":"2016-02-25T05:21:49+00:00","article_modified_time":"2022-02-03T17:24:34+00:00","author":"Etienne Bley","twitter_misc":{"Written by":"Etienne Bley","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/#article","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/"},"author":{"name":"Etienne Bley","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"headline":"scripting mysql on unix to clean tables","datePublished":"2016-02-25T05:21:49+00:00","dateModified":"2022-02-03T17:24:34+00:00","mainEntityOfPage":{"@id":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/"},"wordCount":335,"articleSection":["General"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/","url":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/","name":"scripting mysql on unix to clean tables - ELB Solutions.com Inc.","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/#website"},"datePublished":"2016-02-25T05:21:49+00:00","dateModified":"2022-02-03T17:24:34+00:00","author":{"@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"breadcrumb":{"@id":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/elbsolutions.com\/projects\/scripting-mysql-on-unix-to-clean-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/elbsolutions.com\/projects\/"},{"@type":"ListItem","position":2,"name":"scripting mysql on unix to clean tables"}]},{"@type":"WebSite","@id":"https:\/\/elbsolutions.com\/projects\/#website","url":"https:\/\/elbsolutions.com\/projects\/","name":"ELB Solutions.com Inc.","description":"Bringing all your IT Pieces together","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/elbsolutions.com\/projects\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39","name":"Etienne Bley","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","caption":"Etienne Bley"},"url":"https:\/\/elbsolutions.com\/projects\/author\/etienne-bley\/"}]}},"_links":{"self":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/1431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/comments?post=1431"}],"version-history":[{"count":3,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/1431\/revisions"}],"predecessor-version":[{"id":1434,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/1431\/revisions\/1434"}],"wp:attachment":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/media?parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/categories?post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/tags?post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}