{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/importing-data-into-docker-based-mysql","result":{"data":{"markdownRemark":{"html":"<p>Here is a \"cheatsheet\" for setting up a mysql server with Docker. If you're working with Wordpress, for example, the following should be fairly useful as you will need a working MySQL container for a typical local development setup for WP which usually involves importing an existing database. This article outlines a slight difference, depending on whether the mysql container is brand new or existing.</p>\n<h3>Setting-up a fresh instance of MySQL</h3>\n<p>The following section is applicable when we are starting from scratch - setting up a completely new database on our local machine where no prior data exists.</p>\n<p>When creating a MySQL container we can do some one-time init actions to import one or more <code class=\"language-text\">.sql</code> files, often useful when importing a <code class=\"language-text\">mysqldump</code> sql file and re-creating a production database on our local machine we would like to work with. </p>\n<p>The folder where this is run from contains the following:</p>\n<div class=\"gatsby-highlight\" data-language=\"docker\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-docker line-numbers\"><code class=\"language-docker\"><span class=\"token punctuation\">[</span>ROOT folder<span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">-</span> <span class=\"token punctuation\">...</span>\n<span class=\"token punctuation\">-</span> docker<span class=\"token punctuation\">-</span>entrypoint<span class=\"token punctuation\">-</span>initdb.d/\n  <span class=\"token punctuation\">|</span>\n  <span class=\"token punctuation\">|</span><span class=\"token punctuation\">-</span><span class=\"token punctuation\">-</span> 01<span class=\"token punctuation\">-</span>init.sql\n  <span class=\"token punctuation\">|</span>\n  <span class=\"token punctuation\">|</span><span class=\"token punctuation\">-</span><span class=\"token punctuation\">-</span> 02<span class=\"token punctuation\">-</span>additional<span class=\"token punctuation\">-</span>data.sql</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>The <code class=\"language-text\">docker-entrypoint-initdb.d</code> sub-directory contains a mysql dump file.</p>\n<p>When exporting a database using PhpMyAdmin, the resulting file does not pre-select or pre-defined a database. In other words, we have to specify the target database where the exported SQL is supposed to go into. </p>\n<p>Here is a one liner that will import all SQL files inside the local sub-directory <code class=\"language-text\">docker-entrypoint-initdb.d</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"docker\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-docker line-numbers\"><code class=\"language-docker\">docker run <span class=\"token punctuation\">-</span><span class=\"token punctuation\">-</span>name mysql<span class=\"token punctuation\">-</span>cont \n       <span class=\"token punctuation\">-</span>e MYSQL_ROOT_PASSWORD=password \\\n       <span class=\"token punctuation\">-</span>e MYSQL_DATABASE=wordpress \\\n       <span class=\"token punctuation\">-</span>p 3306<span class=\"token punctuation\">:</span>3306 <span class=\"token punctuation\">-</span>d \\\n       <span class=\"token punctuation\">-</span><span class=\"token punctuation\">-</span>mount type=bind<span class=\"token punctuation\">,</span>source=$(pwd)/docker<span class=\"token punctuation\">-</span>entrypoint<span class=\"token punctuation\">-</span>initdb.d<span class=\"token punctuation\">,</span>target=/docker<span class=\"token punctuation\">-</span>entrypoint<span class=\"token punctuation\">-</span>initdb.d \\\n       mysql<span class=\"token punctuation\">:</span>5.7</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>What happens in the above line is that:</p>\n<ul>\n<li>Run a <code class=\"language-text\">mysql:5.7</code> image from the official Docker image and create a container named <code class=\"language-text\">mysql-cont</code></li>\n<li>Automatically create a database called <code class=\"language-text\">wordpress</code>, using the <code class=\"language-text\">MYSQL_DATABASE</code> environment variable parameter passed into the container. Also set root password to <code class=\"language-text\">password</code></li>\n<li>Expose the MySQL on port 3306</li>\n<li>Mount a local sub directory <code class=\"language-text\">$(pwd)/docker-entrypoint-initdb.d</code> to container's <code class=\"language-text\">/docker-entrypoint-initdb.d</code>. Our local directory has several <code class=\"language-text\">.sql</code> files, alphabetically ordered to reflect the order in which we would like to execute them during initialization</li>\n</ul>\n<p>IMPORTANT: this is done on a fresh new non-existent container that does not have any data (no volume is monted to <code class=\"language-text\">/var/lib/mysql</code>). If there were data present (by using a volume), the <code class=\"language-text\">.sql</code> files would be ignored!</p>\n<h2>Importing data into existing Docker MySQL Container</h2>\n<p>If a MySQL container we're working with already exists and has a mounted <code class=\"language-text\">/var/lib/mysql</code> directory then the init process described above will not work. The init script will realize that our MySQL container has some data in the data files folder (<code class=\"language-text\">/var/lib/mysql</code>) and will use whatever it finds there, ignoring all the <code class=\"language-text\">.sql</code> files we may have passed into Docker's <code class=\"language-text\">/docker-entrypoint-initdb.d</code> directory in the container. </p>\n<p>To import data into such an environment we would then have two options:</p>\n<ol>\n<li>Use your favourite MySQL GUI admin tool to import data</li>\n<li>Connect to mysql via Docker and run database import commands. </li>\n</ol>\n<p>We'll focus on #2 here.</p>\n<div class=\"gatsby-highlight\" data-language=\"docker\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-docker line-numbers\"><code class=\"language-docker\">docker exec <span class=\"token punctuation\">-</span>i mysql<span class=\"token punctuation\">-</span>cont sh <span class=\"token punctuation\">-</span>c <span class=\"token string\">'exec mysql -uroot -p\"$MYSQL_ROOT_PASSWORD\"'</span> target_db_name &lt; /some/path/on/your/host/target_db.sql</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>What the above does is:</p>\n<ul>\n<li>Connects to a running mysql-cont docker container (which is our mysql container)</li>\n<li>runs the <code class=\"language-text\">mysql -uroot -p&quot;$MYSQL_ROOT_PASSWORD&quot;&#39; target_db_name &lt; /some/path/on/your/host/target_db.sql</code> on the shell inside that container which will use the <code class=\"language-text\">target_db_name</code> database (that should exist) and execute whatever SQL commands we have in the <code class=\"language-text\">/some/path/on/your/host/target_db.sql</code></li>\n</ul>","excerpt":"Here is a \"cheatsheet\" for setting up a mysql server with Docker. If you're working with Wordpress, for example, the following should be fairly useful as you…","frontmatter":{"date":"28 October, 2021","path":"/blog/importing-data-into-docker-based-mysql","title":"Importing data into Docker-based mysql container"},"fields":{"readingTime":{"text":"3 min read"}}}},"pageContext":{}}}