docker_mysql

整体想法:我想学习vuejs做一个简单的增删改查,后台用springboot,db用mysql,所以现在要先安装mysql。
目的:安装mysql

  1. 在PC上任意目录建立mysql文件夹
sixdog@192 Docker % pwd #确认当前路径
/Users/sixdog/Documents/Docker
sixdog@192 Docker % ls -l #确认目前路径下的文件
total 24
-rw-r--r--  1 sixdog  staff   974 11 11 22:42 docker run -it --rm -p 8080:8080 tomcat.dockerfile
drwxr-xr-x  5 sixdog  staff   160 11 18 13:01 dockerfile
-rw-r--r--  1 sixdog  staff  4502 11 11 23:02 memo.txt
drwxr-xr-x  3 sixdog  staff    96 11 15 22:29 mojolicious
drwxr-xr-x  3 sixdog  staff    96 11 11 20:31 nginx
drwxr-xr-x  6 sixdog  staff   192 11 11 22:24 tomcat
sixdog@192 Docker % mkdir mysql-devconfig #创建mysql-devconfig 文件夹
  1. pull mysql镜像,并确认结果
sixdog@192 Docker % cd mysql-devconfig  #进入mysql-devconfig 文件夹        
sixdog@192 mysql-devconfig % docker pull mysql #pull mysql最新版本镜像
Using default tag: latest
latest: Pulling from library/mysql
a10c77af2613: Pull complete 
b76a7eb51ffd: Pull complete 
258223f927e4: Pull complete 
2d2c75386df9: Pull complete 
63e92e4046c9: Pull complete 
f5845c731544: Pull complete 
bd0401123a9b: Pull complete 
3ef07ec35f1a: Pull complete 
c93a31315089: Pull complete 
3349ed800d44: Pull complete 
6d01857ca4c1: Pull complete 
4cc13890eda8: Pull complete 
Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
sixdog@192 mysql-devconfig % 
  1. 镜像确认
sixdog@192 mysql-devconfig % docker images #镜像确认
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        latest    b05128b000dd   3 days ago   516MB
sixdog@192 mysql-devconfig % 

4.创建临时容器

sixdog@192 mysql-devconfig % docker run -itd --name mysql-tmp -P -e MYSQL_ROOT_PASSWORD=123456 mysql
03da1134f010a9f491839ed688a18b661141ac0ae8e070189c663d7ee73a2563
sixdog@192 mysql-devconfig % 

5.进入容器前当前目录内容确认

sixdog@192 mysql-devconfig % ls -l
total 0
sixdog@192 mysql-devconfig % 

6.进入容器确认要拷贝的文件

sixdog@192 mysql-devconfig % docker exec -it mysql-tmp bash #进入容器
root@03da1134f010:/# ls -l
total 72
drwxr-xr-x   2 root root 4096 Nov 15 00:00 bin
drwxr-xr-x   2 root root 4096 Oct  3 09:00 boot
drwxr-xr-x   5 root root  360 Nov 21 00:58 dev
drwxr-xr-x   2 root root 4096 Nov 17 10:33 docker-entrypoint-initdb.d
lrwxrwxrwx   1 root root   34 Nov 17 10:33 entrypoint.sh -> usr/local/bin/docker-entrypoint.sh
drwxr-xr-x   1 root root 4096 Nov 21 00:58 etc
drwxr-xr-x   2 root root 4096 Oct  3 09:00 home
drwxr-xr-x   1 root root 4096 Nov 17 10:32 lib
drwxr-xr-x   2 root root 4096 Nov 15 00:00 lib64
drwxr-xr-x   2 root root 4096 Nov 15 00:00 media
drwxr-xr-x   2 root root 4096 Nov 15 00:00 mnt
drwxr-xr-x   2 root root 4096 Nov 15 00:00 opt
dr-xr-xr-x 183 root root    0 Nov 21 00:58 proc
drwx------   1 root root 4096 Nov 17 10:33 root
drwxr-xr-x   1 root root 4096 Nov 17 10:33 run
drwxr-xr-x   2 root root 4096 Nov 15 00:00 sbin
drwxr-xr-x   2 root root 4096 Nov 15 00:00 srv
dr-xr-xr-x  13 root root    0 Nov 21 00:58 sys
drwxrwxrwt   1 root root 4096 Nov 21 00:59 tmp
drwxr-xr-x   1 root root 4096 Nov 15 00:00 usr
drwxr-xr-x   1 root root 4096 Nov 15 00:00 var
root@03da1134f010:/# 

6.1以下目录内容是实际要拷贝的内容

root@03da1134f010:/# ls -l var/lib/mysql/
total 198064
-rw-r----- 1 mysql mysql   196608 Nov 21 01:01 '#ib_16384_0.dblwr'
-rw-r----- 1 mysql mysql  8585216 Nov 21 00:58 '#ib_16384_1.dblwr'
drwxr-x--- 2 mysql mysql     4096 Nov 21 00:59 '#innodb_temp'
-rw-r----- 1 mysql mysql     1636 Nov 21 00:59  03da1134f010.err
-rw-r----- 1 mysql mysql       56 Nov 21 00:58  auto.cnf
-rw-r----- 1 mysql mysql  3116921 Nov 21 00:59  binlog.000001
-rw-r----- 1 mysql mysql      156 Nov 21 00:59  binlog.000002
-rw-r----- 1 mysql mysql       32 Nov 21 00:59  binlog.index
-rw------- 1 mysql mysql     1680 Nov 21 00:58  ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 Nov 21 00:58  ca.pem
-rw-r--r-- 1 mysql mysql     1112 Nov 21 00:58  client-cert.pem
-rw------- 1 mysql mysql     1680 Nov 21 00:58  client-key.pem
-rw-r----- 1 mysql mysql     5647 Nov 21 00:59  ib_buffer_pool
-rw-r----- 1 mysql mysql 50331648 Nov 21 01:01  ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Nov 21 00:58  ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Nov 21 00:59  ibdata1
-rw-r----- 1 mysql mysql 12582912 Nov 21 00:59  ibtmp1
drwxr-x--- 2 mysql mysql     4096 Nov 21 00:58  mysql
-rw-r----- 1 mysql mysql 31457280 Nov 21 00:59  mysql.ibd
drwxr-x--- 2 mysql mysql     4096 Nov 21 00:58  performance_schema
-rw------- 1 mysql mysql     1680 Nov 21 00:58  private_key.pem
-rw-r--r-- 1 mysql mysql      452 Nov 21 00:58  public_key.pem
-rw-r--r-- 1 mysql mysql     1112 Nov 21 00:58  server-cert.pem
-rw------- 1 mysql mysql     1680 Nov 21 00:58  server-key.pem
drwxr-x--- 2 mysql mysql     4096 Nov 21 00:58  sys
-rw-r----- 1 mysql mysql 16777216 Nov 21 01:01  undo_001
-rw-r----- 1 mysql mysql 16777216 Nov 21 01:01  undo_002
root@03da1134f010:/# 
root@03da1134f010:/# exit
exit

7.内容拷贝

sixdog@192 mysql-devconfig % docker cp mysql-tmp:var/lib/mysql/ $PWD #mysql-tmp是容器的名字,这个要指定;$PWD是当前目录
sixdog@192 mysql-devconfig % ls -l
total 0
drwxrwxrwt  29 sixdog  staff  928 11 21 08:59 mysql #var/lib/mysql/ 路径的mysql文件夹
sixdog@192 mysql-devconfig % ls -l mysql #确认mysql文件夹内容
total 396088
-rw-r-----    1 sixdog  staff    196608 11 21 09:01 #ib_16384_0.dblwr
-rw-r-----    1 sixdog  staff   8585216 11 21 08:58 #ib_16384_1.dblwr
drwxr-x---   12 sixdog  staff       384 11 21 08:59 #innodb_temp
-rw-r-----    1 sixdog  staff      1636 11 21 08:59 03da1134f010.err
-rw-r-----    1 sixdog  staff        56 11 21 08:58 auto.cnf
-rw-r-----    1 sixdog  staff   3116921 11 21 08:59 binlog.000001
-rw-r-----    1 sixdog  staff       156 11 21 08:59 binlog.000002
-rw-r-----    1 sixdog  staff        32 11 21 08:59 binlog.index
-rw-------    1 sixdog  staff      1680 11 21 08:58 ca-key.pem
-rw-r--r--    1 sixdog  staff      1112 11 21 08:58 ca.pem
-rw-r--r--    1 sixdog  staff      1112 11 21 08:58 client-cert.pem
-rw-------    1 sixdog  staff      1680 11 21 08:58 client-key.pem
-rw-r-----    1 sixdog  staff      5647 11 21 08:59 ib_buffer_pool
-rw-r-----    1 sixdog  staff  50331648 11 21 09:01 ib_logfile0
-rw-r-----    1 sixdog  staff  50331648 11 21 08:58 ib_logfile1
-rw-r-----    1 sixdog  staff  12582912 11 21 08:59 ibdata1
-rw-r-----    1 sixdog  staff  12582912 11 21 08:59 ibtmp1
drwxr-x---    8 sixdog  staff       256 11 21 08:58 mysql
-rw-r-----    1 sixdog  staff  31457280 11 21 08:59 mysql.ibd
drwxr-x---  112 sixdog  staff      3584 11 21 08:58 performance_schema
-rw-------    1 sixdog  staff      1680 11 21 08:58 private_key.pem
-rw-r--r--    1 sixdog  staff       452 11 21 08:58 public_key.pem
-rw-r--r--    1 sixdog  staff      1112 11 21 08:58 server-cert.pem
-rw-------    1 sixdog  staff      1680 11 21 08:58 server-key.pem
drwxr-x---    3 sixdog  staff        96 11 21 08:58 sys
-rw-r-----    1 sixdog  staff  16777216 11 21 09:01 undo_001
-rw-r-----    1 sixdog  staff  16777216 11 21 09:01 undo_002
sixdog@192 mysql-devconfig % 

8.创建实际使用的mysql容器
※这里特意将端口设置为不一致,因为之后我想确认容器之间通信问题,特意区分一下

sixdog@192 mysql-devconfig % docker run --name mysql-dev -p 3307:3306 -v $PWD/mysql-devconfig:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
465ae184cd56b17d4f6f82da61a44af1daa3453e2c0ed543b32cb7eb42ace700

8.1 查看现在的容器
※465ae184cd56即为新创立的容器

sixdog@192 mysql-devconfig % docker ps -a                                                                                                                 
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                               NAMES
465ae184cd56   mysql:latest   "docker-entrypoint.s…"   3 seconds ago   Up 2 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp                   mysql-dev
03da1134f010   mysql          "docker-entrypoint.s…"   2 hours ago     Up 2 hours     0.0.0.0:55003->3306/tcp, 0.0.0.0:55002->33060/tcp   mysql-tmp

9.进入mysql并登录

sixdog@192 mysql-devconfig % docker exec -it mysql-dev bash
root@465ae184cd56:/# mysql -h localhost -u root -p       
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

10.用MYSQLWorkbench连接

docker_mysql_第1张图片
docker_mysql_第2张图片

你可能感兴趣的:(docker,docker,mysql,database,容器)