{"id":254394,"date":"2021-05-20T13:00:13","date_gmt":"2021-05-20T10:00:13","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/"},"modified":"2021-05-20T13:00:13","modified_gmt":"2021-05-20T10:00:13","slug":"how-to-run-mysql-in-a-docker-container-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/","title":{"rendered":"#How to Run MySQL In A Docker Container \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a24964559472\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a24964559472\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/#Getting_Started\" >Getting Started<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/#Docker_Compose\" >Docker Compose<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/#Connecting_to_MySQL\" >Connecting to MySQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/#Automatically_Creating_A_Database_and_User\" >Automatically Creating A Database and User<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/#First-Run_Seed_Files\" >First-Run Seed Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/#Tweaking_Server_Settings\" >Tweaking Server Settings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-mysql-in-a-docker-container-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Run MySQL In A Docker Container \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-10705\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/a6ea91c5.jpg?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Docker and MySQL logos\" width=\"1600\" height=\"900\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker containers let you quickly spin up new <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lications without polluting your system with dependencies. A MySQL database is a common requirement for web-based software. Here\u2019s how to get a new MySQL instance running in Docker in only a few minutes.<\/p>\n<h2 id=\"getting-started\"><span class=\"ez-toc-section\" id=\"Getting_Started\"><\/span>Getting Started<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/hub.docker.com\/_\/mysql\">official MySQL image<\/a> on Docker Hub provides everything needed to get started. Images are available for MySQL versions 5.6, 5.7 and 8.0. Specify the one you want to use as the image tag:<\/p>\n<pre><code>docker pull mysql:8.0<\/code><\/pre>\n<p>Before deploying, you\u2019ll need to setup a Docker volume or bind mount to persist your database in. Otherwise, your data will be lost when the container restarts. The mount should be made to <code>\/var\/lib\/mysql<\/code> within the container.<\/p>\n<p>You also need to set a password for the MySQL root user. Use the <code>MYSQL_ROOT_PASSWORD<\/code> environment variable. MySQL will refuse to initialise if this variable isn\u2019t set. Try to choose a secure password.<\/p>\n<p>Here\u2019s how to run MySQL while satisfying these prerequisites:<\/p>\n<pre><code>docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=secret -v $HOME\/mysql-data:\/var\/lib\/mysql -d mysql:8.0<\/code><\/pre>\n<p>The MySQL first-run routine will take a few seconds to complete. You can check if the database is up by running <code>docker logs my-mysql<\/code>. Check for a line that contains <code>ready for connections<\/code>.<\/p>\n<h2 id=\"docker-compose\"><span class=\"ez-toc-section\" id=\"Docker_Compose\"><\/span>Docker Compose<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>docker run<\/code> command can quickly become lengthy, especially if you have complex configuration requirements. Writing a <code>docker-compose.yml<\/code> file is more maintainable. Here\u2019s an example:<\/p>\n<pre class=\"yml\"><code>version: \"3\"&#13;\n&#13;\nservices:&#13;\n  mysql:&#13;\n    image: mysql:8.0&#13;\n    environment:&#13;\n      - MYSQL_ROOT_PASSWORD&#13;\n    volumes:&#13;\n      - mysql:\/var\/lib\/mysql&#13;\n&#13;\nvolumes:&#13;\n  mysql:<\/code><\/pre>\n<p>You can start MySQL using <code>MYSQL_ROOT_PASSWORD=secure docker-compose up -d<\/code>.<\/p>\n<h2 id=\"connecting-to-mysql\"><span class=\"ez-toc-section\" id=\"Connecting_to_MySQL\"><\/span>Connecting to MySQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can gain an interactive MySQL shell by attaching to the container and running the <code>mysql<\/code> command. This provides the full MySQL command-line interface, so you can use all the familiar commands and flags.<\/p>\n<pre><code>docker exec -it my-mysql mysql -p<\/code><\/pre>\n<p>This command opens a shell as the root user and prompts for a password.<\/p>\n<p>To import a SQL file from your filesystem, you can pipe it into the command:<\/p>\n<pre><code>docker exec -it my-mysql mysql -psecret database_name &lt; path-to-file.sql<\/code><\/pre>\n<p>You must setup a port binding if you want to access MySQL from your host. Bind a host port to the container\u2019s port 3306. Add the following to your <code>docker-compose.yml<\/code> file, within the <code>mysql<\/code> service definition:<\/p>\n<pre class=\"yml\"><code>ports:&#13;\n - 33060:3306<\/code><\/pre>\n<p>This will bind port 33060 on your host machine to the container\u2019s port 3306. If you\u2019re not using Docker Compose, pass <code>-p 33060:3306<\/code> to <code>docker run<\/code> when you start your container. You\u2019ll now be able to connect to <code>localhost:33060<\/code> using MySQL clients running on your host.<\/p>\n<p>If you need to connect from <em>another<\/em> Docker container, it\u2019s best to use Docker Compose. Define another service for the new container. Both containers will exist in the same Docker network. You\u2019ll be able to connect using port 3306 on the MySQL container\u2019s hostname (this matches the service name defined in your Compose file).<\/p>\n<h2 id=\"automatically-creating-a-database-and-user\"><span class=\"ez-toc-section\" id=\"Automatically_Creating_A_Database_and_User\"><\/span>Automatically Creating A Database and User<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The MySQL Docker images support optional environment variables that let you manage the first-run database setup. Set these variables to automatically provision a new empty database and user account:<\/p>\n<ul>\n<li><strong><code>MYSQL_DATABASE<\/code><\/strong> \u2013 The name of a database schema to create when the server initialises for the first time.<\/li>\n<li><strong><code>MYSQL_USER<\/code> and <code>MYSQL_PASSWORD<\/code><\/strong> \u2013 Create a new initial user account, separately to the <code>root<\/code> user. If you also set the <code>MYSQL_DATABASE<\/code> variable, your new user account will <em>automatically<\/em> be granted all privileges on the newly created database.<\/li>\n<li><strong><code>MYSQL_RANDOM_ROOT_PASSWORD<\/code><\/strong> \u2013 Set this variable to have MySQL automatically generate a new password for the <code>root<\/code> user. This replaces the <code>MYSQL_ROOT_PASSWORD<\/code> variable. The generated password will be emitted to the standard output stream on first run. Because of this, you should take care when using this variable in CI environments \u2013 the password may be visible to anyone who has access to the job logs!<\/li>\n<\/ul>\n<h2 id=\"first-run-seed-files\"><span class=\"ez-toc-section\" id=\"First-Run_Seed_Files\"><\/span>First-Run Seed Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You\u2019ll often have tables and data you want to seed into the database as soon as it\u2019s created. The MySQL images are configured to look for <code>.sh<\/code>, <code>.sql<\/code> and <code>.sql.gz<\/code> files in the container\u2019s <code>\/docker-entrypoint-initdb.d<\/code> directory.<\/p>\n<p>Any files found will be executed sequentially. The ordering will always be alphabetical. You can apply filename prefixes (e.g.\u00a0<code>1-seed_tables.sql<\/code> \/ <code>2-seed_data.sql<\/code>) to make sure operations complete in the intended order.<\/p>\n<p>This mechanism lets you easily seed your new database instance with initial data. You can write your own Dockerfile, using MySQL as the base image, and add your seed files into <code>\/docker-entrypoint-initdb.d<\/code>. Alternatively, bind mount a directory on your host into the container directory.<\/p>\n<p>MySQL will ignore initialisation files unless there is no database in the data directory. Wiping the contents of your data directory will trigger a fresh initialisation when you restart the container.<\/p>\n<h2 id=\"tweaking-server-settings\"><span class=\"ez-toc-section\" id=\"Tweaking_Server_Settings\"><\/span>Tweaking Server Settings<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>MySQL server configuration files will be loaded from <code>\/etc\/mysql\/conf.d<\/code>. Add your own configuration files to this directory, using a Dockerfile override or a bind mount, to adjust server settings. You can get the list of available options from the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/option-files.html\">MySQL Server manual<\/a>.<\/p>\n<p>Using an options file makes it easy to view and edit your configuration in the future. You check your file into version control alongside your project. However, command-line flags are also available for one-off tweaks and experimentation.<\/p>\n<pre><code>docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql:latest --<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>-log=on<\/code><\/pre>\n<p>This variant of <code>docker run<\/code> will start MySQL with the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/query-log.html\">general query log<\/a> enabled. The <code>mysql<\/code> binary is the image\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/#entrypoint\">entrypoint<\/a>, so flags passed to <code>docker run<\/code> after the image name will be forwarded to <code>mysql<\/code>.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Running MySQL within Docker is much quicker and easier than installing MySQL Server \u201cbare-metal.\u201d You can run multiple MySQL instances without the risk of conflict.<\/p>\n<p>The official Docker images come with useful configuration options and built-in support for first-run database seeds. This lets you get off-the-ground quickly, whether you\u2019re starting from scratch or using an existing schema.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/10703\/how-to-run-mysql-in-a-docker-container\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Run MySQL In A Docker Container \u2013 CloudSavvy IT&#8221; Docker containers let you quickly spin up new applications without polluting your system with dependencies. A MySQL database is a common requirement for web-based software. Here\u2019s how to get a new MySQL instance running in Docker in only a few minutes. Getting Started The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":254395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/a6ea91c5.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-254394","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/254394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=254394"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/254394\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/254395"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=254394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=254394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=254394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}