Docker上運行MySQL服務


1.搜索MySQL鏡像

$ docker search mysql
INDEX       NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                   MySQL is a widely used, open-source relati...   6008      [OK]       
docker.io   docker.io/mariadb                 MariaDB is a community-developed fork of M...   1891      [OK]       
docker.io   docker.io/mysql/mysql-server      Optimized MySQL Server Docker images. Crea...   427                  [OK]
docker.io   docker.io/percona                  Percona Server is a fork of the MySQL rela...   335       [OK]       

備注:STARS數最多,OFFICIAL是[OK]的這個就是官方的centos鏡像。

2.下載MySQL鏡像

$ docker pull docker.io/mysql
$ docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
docker.io/mysql                  latest              5195076672a7        4 weeks ago         371.4 MB

 

3.運行容器

$ docker run -d --name liying-mysql -e MYSQL_ROOT_PASSWORD=attack docker.io/mysql
$ docker exec -it liying-mysql /bin/bash ##進入容器

 

4.進入mysql

root@3d21d8918d31:/# mysql -u root -pattack
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

以上就創建了一個mysql的docker容器,可以看到版本為5.7.21。但是這樣創建的容器有兩個問題,一是容器刪除后,數據就丟失了,二是要訪問數據庫,必須進入到容器里面才可以。

5.持久化數據,映射開放mysql端口
a、創建宿主機數據存放目錄
$ mkdir -p /opt/data/mysql

b、啟動容器
$ docker run -d -v /opt/data/mysql/:/var/lib/mysql -p 3306:3306 --name liying-mysql -e MYSQL_ROOT_PASSWORD=attack docker.io/mysql
$ docker logs liying-mysql ##查看日志
$ docker ps #查看容器

c、查看端口
$ lsof -i:3306

d、查看宿主機上的mysql數據

$ cd /opt/data/mysql
$ ll
總用量 188484
-rw-r-----. 1 systemd-bus-proxy input       56 4月  17 11:53 auto.cnf
-rw-------. 1 systemd-bus-proxy input     1679 4月  17 11:53 ca-key.pem
-rw-r--r--. 1 systemd-bus-proxy input     1107 4月  17 11:53 ca.pem
-rw-r--r--. 1 systemd-bus-proxy input     1107 4月  17 11:53 client-cert.pem
-rw-------. 1 systemd-bus-proxy input     1679 4月  17 11:53 client-key.pem
-rw-r-----. 1 systemd-bus-proxy input     1335 4月  17 11:54 ib_buffer_pool
-rw-r-----. 1 systemd-bus-proxy input 79691776 4月  17 11:54 ibdata1
-rw-r-----. 1 systemd-bus-proxy input 50331648 4月  17 11:54 ib_logfile0
-rw-r-----. 1 systemd-bus-proxy input 50331648 4月  17 11:53 ib_logfile1
-rw-r-----. 1 systemd-bus-proxy input 12582912 4月  17 11:54 ibtmp1
drwxr-x---. 2 systemd-bus-proxy input     4096 4月  17 11:53 mysql
drwxr-x---. 2 systemd-bus-proxy input     8192 4月  17 11:53 performance_schema
-rw-------. 1 systemd-bus-proxy input     1679 4月  17 11:53 private_key.pem
-rw-r--r--. 1 systemd-bus-proxy input      451 4月  17 11:53 public_key.pem
-rw-r--r--. 1 systemd-bus-proxy input     1107 4月  17 11:53 server-cert.pem
-rw-------. 1 systemd-bus-proxy input     1679 4月  17 11:53 server-key.pem
drwxr-x---. 2 systemd-bus-proxy input     8192 4月  17 11:53 sys

-p 3306:3306->把容器的mysql端口3306映射到宿主機的3306端口,這樣想訪問mysql就可以直接訪問宿主機的3306端口。
-v /opt/data/mysql:/var/lib/mysql->把宿主機/opt/data/mysql/目錄映射到容器的/var/lib/mysql目錄

注意事項:
在使用-v選項映射目錄時,宿主機需關閉SElinux:
$ setenforce 0

6、Navicat for MySQL客戶端訪問mysql


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM