Docker容器里一键装MySQL,超简单教程

Docker容器里一键装MySQL,超简单教程

四月 02, 2019 ubuntu code snippets docker knowledge base

装 Docker

Ubuntu 上装 Docker,看这篇教程就行:Ubuntu 怎么装 Docker?

跑个 MySQL 容器,后台运行,root 密码设成 "test"。

>> docker run --rm -it -e MYSQL_ROOT_PASSWORD=test -d mysql
  • -d:后台跑容器,只打印 ID。
  • --rm:容器停了自动删掉。

查看运行中的容器

>> docker ps
CONTAINER ID      IMAGE    COMMAND                  CREATED       STATUS     PORTS                   NAMES
8ee2e6c8174d      mysql    "docker-entrypoint.s…"    9秒前        Up 7秒     3306/tcp, 33060/tcp     condescending_snyder

进 MySQL 命令行

>> docker exec -it 8ee2e6c8174d mysql -u root -ptest
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 9
Server version: 8.0.15 MySQL Community Server - GPL

mysql> 

检查容器详情

看网络设置、挂载卷啥的。

>> docker inspect 8ee2e6c8174d
[
    {
        "Id": "8ee2e6c8174d4138026f1d54addf9aadc7fbe22727c7f1fb9ca996ce5f7601b2",
        "Created": "2019-03-28T20:53:46.84185469Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
        ...
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "4c9f618087d8f2e6924e7683cfb98055d8cc9a4ec8d0427bd188b5b7c34c0b3f",
                "EndpointID": "cd5d96ad898831d3cc2d0941633533fdf8dcc6cdb0564164c49cad1a2399e8bf",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:02",
                "DriverOpts": null
            }
        }
    }
]

用 IP 连 MySQL

从外面连容器 IP。

mysql -u root -ptest -h 172.17.0.2

查看所有容器(包括停的)

>> docker ps -a
CONTAINER ID     IMAGE     COMMAND                  CREATED      STATUS                    PORTS                  NAMES
8ee2e6c8174d      mysql     "docker-entrypoint.s…"    2分钟前     Up 2分钟                  3306/tcp, 33060/tcp    condescending_snyder
913f33f5a848      mysql     "docker-entrypoint.s…"    12分钟前    Exited (0) 11分钟前                          jovial_dubinsky
b36cf8b3b664      mysql     "docker-entrypoint.s…"    22分钟前    Exited (1) 22分钟前                          cocky_mcnulty

看所有镜像

>> docker images
REPOSITORY      TAG       IMAGE ID      CREATED       SIZE
mysql           latest    7bb2586065cd  45小时前      477MB

Read in other languages:

EN ES SV RO DE FR HU NL PL EL BG RU CS PT NB FI IT DA UZ TR