docker-mysql-getting-started.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <h4 class="title"><a name="docker-mysql-getting-started"></a>2.5.6.1 Basic Steps for MySQL Server Deployment with Docker</h4>
  2. </div>
  3. </div>
  4. </div>
  5. <div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">
  6. <div class="admon-title">
  7. Warning
  8. </div>
  9. <p>
  10. The MySQL Docker images maintained by the MySQL team are built
  11. specifically for Linux platforms. Other platforms are not
  12. supported, and users using these MySQL Docker images on them
  13. are doing so at their own risk. See
  14. <a class="link" href="deploy-mysql-nonlinux-docker.html" title="2.5.6.3 Deploying MySQL on Windows and Other Non-Linux Platforms with Docker">the discussion
  15. here</a> for some known limitations for running these
  16. containers on non-Linux operating systems.
  17. </p>
  18. </div>
  19. <div class="itemizedlist">
  20. <ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><a class="xref" href="docker-mysql-getting-started.html#docker-download-image" title="Downloading a MySQL Server Docker Image">Downloading a MySQL Server Docker Image</a></p></li><li class="listitem"><p><a class="xref" href="docker-mysql-getting-started.html#docker-starting-mysql-server" title="Starting a MySQL Server Instance">Starting a MySQL Server Instance</a></p></li><li class="listitem"><p><a class="xref" href="docker-mysql-getting-started.html#docker-connecting-within-container" title="Connecting to MySQL Server from within the Container">Connecting to MySQL Server from within the Container</a></p></li><li class="listitem"><p><a class="xref" href="docker-mysql-getting-started.html#docker-shell-access" title="Container Shell Access">Container Shell Access</a></p></li><li class="listitem"><p><a class="xref" href="docker-mysql-getting-started.html#docker-stopping-deleting" title="Stopping and Deleting a MySQL Container">Stopping and Deleting a MySQL Container</a></p></li><li class="listitem"><p><a class="xref" href="docker-mysql-getting-started.html#docker-upgrading" title="Upgrading a MySQL Server Container">Upgrading a MySQL Server Container</a></p></li><li class="listitem"><p><a class="xref" href="docker-mysql-getting-started.html#docker-more-topics" title="More Topics on Deploying MySQL Server with Docker">More Topics on Deploying MySQL Server with Docker</a></p></li></ul>
  21. </div>
  22. <h5><a name="docker-download-image"></a>Downloading a MySQL Server Docker Image</h5>
  23. <p>
  24. Downloading the server image in a separate step is not strictly
  25. necessary; however, performing this step before you create your
  26. Docker container ensures your local image is up to date. To
  27. download the MySQL Community Edition image, run this command:
  28. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker pull mysql/mysql-server:<em class="replaceable">tag</em></code></pre><p>
  29. The <em class="replaceable"><code>tag</code></em> is the label for the image
  30. version you want to pull (for example, <code class="literal">5.5</code>,
  31. <code class="literal">5.6</code>, <code class="literal">5.7</code>,
  32. <code class="literal">8.0</code>, or <code class="literal">latest</code>). If
  33. <strong class="userinput"><code>:<em class="replaceable"><code>tag</code></em></code></strong> is
  34. omitted, the <code class="literal">latest</code> label is used, and the
  35. image for the latest GA version of MySQL Community Server is
  36. downloaded. Refer to the list of tags for available versions on
  37. the
  38. <a class="ulink" href="https://hub.docker.com/r/mysql/mysql-server/tags/" target="_top">mysql/mysql-server
  39. page in the Docker Hub</a>.
  40. </p><p>
  41. You can list downloaded Docker images with this command:
  42. </p><pre class="programlisting copytoclipboard line-numbers language-terminal"><code class="language-terminal">shell&gt; docker images
  43. REPOSITORY TAG IMAGE ID CREATED SIZE
  44. mysql/mysql-server latest 3157d7f55f8d 4 weeks ago 241MB</code></pre><p>
  45. To download the MySQL Enterprise Edition image from the
  46. <a class="ulink" href="https://support.oracle.com/" target="_top">My Oracle
  47. Support</a> website, sign in to your Oracle account,
  48. download from <span class="guimenu">Patches and Updates</span> the
  49. <code class="filename">tar.zip</code> file for the Docker image
  50. (<code class="filename">mysql-commercial-<em class="replaceable"><code>version</code></em>_linux_x86_64_docker_tar.zip</code>),
  51. unzip it to obtain the tarball inside
  52. (<code class="filename">mysql-enterprise-server-<em class="replaceable"><code>version</code></em>.tar</code>),
  53. and then load the image by running this command:
  54. </p><pre class="programlisting copytoclipboard line-numbers language-simple one-line"><code class="language-simple">docker load -i mysql-enterprise-server-<em class="replaceable">version</em>.tar</code></pre>
  55. <h5><a name="docker-starting-mysql-server"></a>Starting a MySQL Server Instance</h5>
  56. <p>
  57. To start a new Docker container for a MySQL Server, use the
  58. following command:
  59. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker run --name=<em class="replaceable">container_name</em> -d <em class="replaceable">image_name</em>:<em class="replaceable">tag</em></code></pre><p>
  60. The image name can be obtained using the <span class="command"><strong>docker
  61. images</strong></span> command, as explained in
  62. <a class="xref" href="docker-mysql-getting-started.html#docker-download-image" title="Downloading a MySQL Server Docker Image">Downloading a MySQL Server Docker Image</a>. The
  63. <code class="option">--name</code> option, for supplying a custom name for
  64. your server container, is optional; if no container name is
  65. supplied, a random one is generated.
  66. </p><p>
  67. For example, to start a new Docker container for the MySQL
  68. Community Server, use this command:
  69. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker run --name=mysql1 -d mysql/mysql-server:8.0</code></pre><p>
  70. To start a new Docker container for the MySQL Enterprise Server
  71. with a Docker image downloaded from My Oracle Support, use this
  72. command:
  73. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker run --name=mysql1 -d mysql/enterprise-server:8.0</code></pre><p>
  74. If the Docker image of the specified name and tag has not been
  75. downloaded by an earlier <span class="command"><strong>docker pull</strong></span> or
  76. <span class="command"><strong>docker run</strong></span> command, the image is now
  77. downloaded. Initialization for the container begins, and the
  78. container appears in the list of running containers when you run
  79. the <span class="command"><strong>docker ps</strong></span> command. For example:
  80. </p><pre class="programlisting copytoclipboard line-numbers language-terminal"><code class="language-terminal">shell&gt; docker ps
  81. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  82. a24888f0d6f4 mysql/mysql-server "/entrypoint.sh my..." 14 seconds ago Up 13 seconds (health: starting) 3306/tcp, 33060/tcp mysql1</code></pre><p>
  83. The container initialization might take some time. When the
  84. server is ready for use, the <code class="literal">STATUS</code> of the
  85. container in the output of the <span class="command"><strong>docker ps</strong></span>
  86. command changes from <code class="literal">(health: starting)</code> to
  87. <code class="literal">(healthy)</code>.
  88. </p><p>
  89. The <code class="option">-d</code> option used in the <span class="command"><strong>docker
  90. run</strong></span> command above makes the container run in the
  91. background. Use this command to monitor the output from the
  92. container:
  93. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker logs mysql1</code></pre><p>
  94. </p><p>
  95. Once initialization is finished, the command's output is going
  96. to contain the random password generated for the root user;
  97. check the password with, for example, this command:
  98. </p><pre class="programlisting copytoclipboard line-numbers language-terminal"><code class="language-terminal">shell&gt; docker logs mysql1 2&gt;&amp;1 | grep GENERATED
  99. GENERATED ROOT PASSWORD: Axegh3kAJyDLaRuBemecis&amp;EShOs</code></pre><p>
  100. </p>
  101. <h5><a name="docker-connecting-within-container"></a>Connecting to MySQL Server from within the Container</h5>
  102. <p>
  103. Once the server is ready, you can run the
  104. <a class="link" href="mysql.html" title="4.5.1 mysql — The MySQL Command-Line Client"><span class="command"><strong>mysql</strong></span></a> client within the MySQL Server
  105. container you just started, and connect it to the MySQL Server.
  106. Use the <span class="command"><strong>docker exec -it</strong></span> command to start a
  107. <a class="link" href="mysql.html" title="4.5.1 mysql — The MySQL Command-Line Client"><span class="command"><strong>mysql</strong></span></a> client inside the Docker container you
  108. have started, like the following:
  109. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker exec -it mysql1 mysql -uroot -p</code></pre><p>
  110. When asked, enter the generated root password (see the last step
  111. in <a class="xref" href="docker-mysql-getting-started.html#docker-starting-mysql-server" title="Starting a MySQL Server Instance">Starting a MySQL Server Instance</a> above on how
  112. to find the password). Because the
  113. <a class="link" href="docker-mysql-more-topics.html#docker_var_mysql_onetime_password"><code class="option">MYSQL_ONETIME_PASSWORD</code></a>
  114. option is true by default, after you have connected a
  115. <a class="link" href="mysql.html" title="4.5.1 mysql — The MySQL Command-Line Client"><span class="command"><strong>mysql</strong></span></a> client to the server, you must reset
  116. the server root password by issuing this statement:
  117. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">mysql&gt; ALTER USER 'root'@'localhost' IDENTIFIED BY '<em class="replaceable">password</em>';</code></pre><p>
  118. Substitute <em class="replaceable"><code>password</code></em> with the password
  119. of your choice. Once the password is reset, the server is ready
  120. for use.
  121. </p>
  122. <h5><a name="docker-shell-access"></a>Container Shell Access</h5>
  123. <p>
  124. To have shell access to your MySQL Server container, use the
  125. <span class="command"><strong>docker exec -it</strong></span> command to start a bash shell
  126. inside the container:
  127. </p><pre class="programlisting copytoclipboard line-numbers language-terminal"><code class="language-terminal">shell&gt; docker exec -it mysql1 bash
  128. bash-4.2#</code></pre><p>
  129. You can then run Linux commands inside the container. For
  130. example, to view contents in the server's data directory inside
  131. the container, use this command:
  132. </p><pre class="programlisting copytoclipboard line-numbers language-terminal"><code class="language-terminal">bash-4.2# ls /var/lib/mysql
  133. auto.cnf ca.pem client-key.pem ib_logfile0 ibdata1 mysql mysql.sock.lock private_key.pem server-cert.pem sys
  134. ca-key.pem client-cert.pem ib_buffer_pool ib_logfile1 ibtmp1 mysql.sock performance_schema public_key.pem server-key.pem</code></pre>
  135. <h5><a name="docker-stopping-deleting"></a>Stopping and Deleting a MySQL Container</h5>
  136. <p>
  137. To stop the MySQL Server container we have created, use this
  138. command:
  139. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker stop mysql1</code></pre><p>
  140. <span class="command"><strong>docker stop</strong></span> sends a SIGTERM signal to the
  141. <a class="link" href="mysqld.html" title="4.3.1 mysqld — The MySQL Server"><span class="command"><strong>mysqld</strong></span></a> process, so that the server is shut
  142. down gracefully.
  143. </p><p>
  144. Also notice that when the main process of a container
  145. (<a class="link" href="mysqld.html" title="4.3.1 mysqld — The MySQL Server"><span class="command"><strong>mysqld</strong></span></a> in the case of a MySQL Server
  146. container) is stopped, the Docker container stops automatically.
  147. </p><p>
  148. To start the MySQL Server container again:
  149. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker start mysql1</code></pre><p>
  150. To stop and start again the MySQL Server container with a single
  151. command:
  152. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker restart mysql1</code></pre><p>
  153. To delete the MySQL container, stop it first, and then use the
  154. <span class="command"><strong>docker rm</strong></span> command:
  155. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker stop mysql1</code></pre><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker rm mysql1</code></pre><p>
  156. If you want the
  157. <a class="link" href="docker-mysql-more-topics.html#docker-persisting-data-configuration" title="Persisting Data and Configuration Changes">Docker
  158. volume for the server's data directory</a> to be deleted at
  159. the same time, add the <code class="literal">-v</code> option to the
  160. <span class="command"><strong>docker rm</strong></span> command.
  161. </p>
  162. <h5><a name="docker-upgrading"></a>Upgrading a MySQL Server Container</h5>
  163. <div class="important" style="margin-left: 0.5in; margin-right: 0.5in;">
  164. <div class="admon-title">
  165. Important
  166. </div>
  167. <div class="itemizedlist">
  168. <ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  169. Before performing any upgrade to MySQL, follow carefully
  170. the instructions in <a class="xref" href="upgrading.html" title="2.11 Upgrading MySQL">Section 2.11, “Upgrading MySQL”</a>. Among
  171. other instructions discussed there, it is especially
  172. important to back up your database before the upgrade.
  173. </p></li><li class="listitem"><p>
  174. The instructions in this section require that the server's
  175. data and configuration have been persisted on the host.
  176. See <a class="xref" href="docker-mysql-more-topics.html#docker-persisting-data-configuration" title="Persisting Data and Configuration Changes">Persisting Data and Configuration Changes</a>
  177. for details.
  178. </p></li></ul>
  179. </div>
  180. </div>
  181. <p>
  182. Follow these steps to upgrade a Docker installation of MySQL 5.7
  183. to 8.0:
  184. </p>
  185. <div class="itemizedlist">
  186. <ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  187. Stop the MySQL 5.7 server (container name is
  188. <code class="literal">mysql57</code> in this example):
  189. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker stop mysql57</code></pre></li><li class="listitem"><p>
  190. Download the MySQL 8.0 Server Docker image. See instructions
  191. in <a class="xref" href="docker-mysql-getting-started.html#docker-download-image" title="Downloading a MySQL Server Docker Image">Downloading a MySQL Server Docker Image</a>; make sure you
  192. use the right tag for MySQL 8.0.
  193. </p></li><li class="listitem"><p>
  194. Start a new MySQL 8.0 Docker container (named
  195. <code class="literal">mysql80</code> in this example) with the old
  196. server data and configuration (with proper modifications if
  197. needed—see <a class="xref" href="upgrading.html" title="2.11 Upgrading MySQL">Section 2.11, “Upgrading MySQL”</a>) that have been
  198. persisted on the host (by
  199. <a class="ulink" href="https://docs.docker.com/engine/reference/commandline/service_create/#add-bind-mounts-or-volumes" target="_top">bind-mounting</a>
  200. in this example). For the MySQL Community Server, run this
  201. command:
  202. </p><pre class="programlisting copytoclipboard line-numbers language-terminal"><code class="language-terminal">docker run --name=mysql80 \
  203. --mount type=bind,src=/path-on-host-machine/my.cnf,dst=/etc/my.cnf \
  204. --mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql \
  205. -d mysql/mysql-server:8.0</code></pre><p>
  206. If needed, adjust <code class="literal">mysql/mysql-server</code> to
  207. the correct repository name—for example, replace it
  208. with
  209. <code class="literal">mysql/enterprise-server</code> for MySQL Enterprise Edition images
  210. downloaded from <a class="ulink" href="https://support.oracle.com/" target="_top">My
  211. Oracle Support</a>.
  212. </p></li><li class="listitem"><p>
  213. Wait for the server to finish startup. You can check the
  214. status of the server using the <span class="command"><strong>docker ps</strong></span>
  215. command (see <a class="xref" href="docker-mysql-getting-started.html#docker-starting-mysql-server" title="Starting a MySQL Server Instance">Starting a MySQL Server Instance</a>
  216. for how to do that).
  217. </p></li><li class="listitem"><p>
  218. <span class="emphasis"><em>For MySQL 8.0.15 and earlier:</em></span> Run the
  219. <a class="link" href="mysql-upgrade.html" title="4.4.5 mysql_upgrade — Check and Upgrade MySQL Tables">mysql_upgrade</a> utility
  220. in the MySQL 8.0 Server container (not required for MySQL
  221. 8.0.16 and later):
  222. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker exec -it mysql80 mysql_upgrade -uroot -p</code></pre><p>
  223. When prompted, enter the root password for your old MySQL
  224. 5.7 Server.
  225. </p></li><li class="listitem"><p>
  226. Finish the upgrade by restarting the MySQL 8.0 Server
  227. container:
  228. </p><pre class="programlisting copytoclipboard line-numbers language-terminal one-line"><code class="language-terminal">docker restart mysql80</code></pre></li></ul>
  229. </div>
  230. <h5><a name="docker-more-topics"></a>More Topics on Deploying MySQL Server with Docker</h5>
  231. <p>
  232. For more topics on deploying MySQL Server with Docker like
  233. server configuration, persisting data and configuration, server
  234. error log, and container environment variables, see
  235. <a class="xref" href="docker-mysql-more-topics.html" title="2.5.6.2 More Topics on Deploying MySQL Server with Docker">Section 2.5.6.2, “More Topics on Deploying MySQL Server with Docker”</a>.
  236. </p>