1. 安装mysql
https://hub.docker.com/_/mysql?tab=tags
可以到上方地址查看mysql的所有版本,默认下载的最新的版本。tag为latest
docker pull mysql
2.查看镜像信息
docker inspect mysql
会得到如下信息,显示的是镜像的所有信息
[
{
"Id": "fda31b21b86da33a38227aa233453e6e10e76112009cc3f694a9152c5dd3ea8c",
"Created": "2020-09-10T07:16:44.72774931Z",
"Path": "docker-entrypoint.sh",
"Args": [
"mysqld"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4023,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-09-10T07:16:45.472079944Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:3646af3dc14aa17367bce05b955d29783c394d751c3d9e85eb34bd60f0d76624",
"ResolvConfPath": "/var/lib/docker/containers/fda31b21b86da33a38227aa233453e6e10e76112009cc3f694a9152c5dd3ea8c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/fda31b21b86da33a38227aa233453e6e10e76112009cc3f694a9152c5dd3ea8c/hostname",
"HostsPath": "/var/lib/docker/containers/fda31b21b86da33a38227aa233453e6e10e76112009cc3f694a9152c5dd3ea8c/hosts",
"LogPath": "/var/lib/docker/containers/fda31b21b86da33a38227aa233453e6e10e76112009cc3f694a9152c5dd3ea8c/fda31b21b86da33a38227aa233453e6e10e76112009cc3f694a9152c5dd3ea8c-json.log",
"Name": "/mysql",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/test/mysql:/var/lib/mysql"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "3306"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/c9d34f8297bf1ac80fdd66ecbec1aa74e7cbe913b8dd313efb7abe50572d7269-init/diff:/var/lib/docker/overlay2/085843cfa59df7eb9a5ce7e96dbb5c05484bd661e34afdcd4c54ce3b3b8fe9e7/diff:/var/lib/docker/overlay2/ae74d5c2abbbd5380975fe17c8d6b9f5554fc8d3fc9c1a19567d4371f7bd08f2/diff:/var/lib/docker/overlay2/4b92c3117f0996f11542f82ff1ab1e897f3926d3f283e5eaf7092d0af3e4f177/diff:/var/lib/docker/overlay2/e3ba26e14372700b7da10c4f1ffa294440dd564d9d1d045db1181963f5fec290/diff:/var/lib/docker/overlay2/e742cd87b2a5f15c3683a2f9c8f3b1ce78137d3df4ca09770c3bb60976e1ae6e/diff:/var/lib/docker/overlay2/78974be15fb6d313ef1b91e3ff355e50bb1bdebfb96f11a2de31cf0e527413de/diff:/var/lib/docker/overlay2/8431147c0e55e28c4baddc2f679c9855d283a5a3be5651608f22908b1dde7ae5/diff:/var/lib/docker/overlay2/a6d1735c0dc5fe6fadf34bd9222df6ffbf3464ee7db818086ceeee7c396553ce/diff:/var/lib/docker/overlay2/d983cdfe8b810f3807e6f68287acc58a8aded308d7ed852c1ba0670ba4e45189/diff:/var/lib/docker/overlay2/75dfd95ca51a4a59c3b545c201f00ff606ed5abd2a01b83d16755a709de5f5e0/diff:/var/lib/docker/overlay2/8fbd71e9f0a91dbd5293be1b8ed0fd4c7ddfacd2e87547c053c4123c6bf2c9c7/diff:/var/lib/docker/overlay2/c0519f1ab8b549dcfacb85b683b1a86789e2e54503cbe0e1711861cd4ce00e31/diff",
"MergedDir": "/var/lib/docker/overlay2/c9d34f8297bf1ac80fdd66ecbec1aa74e7cbe913b8dd313efb7abe50572d7269/merged",
"UpperDir": "/var/lib/docker/overlay2/c9d34f8297bf1ac80fdd66ecbec1aa74e7cbe913b8dd313efb7abe50572d7269/diff",
"WorkDir": "/var/lib/docker/overlay2/c9d34f8297bf1ac80fdd66ecbec1aa74e7cbe913b8dd313efb7abe50572d7269/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/test/mysql",
"Destination": "/var/lib/mysql",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "fda31b21b86d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.12",
"MYSQL_MAJOR=8.0",
"MYSQL_VERSION=8.0.21-1debian10"
],
"Cmd": [
"mysqld"
],
"Image": "mysql",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "aba230174ac8ff6b30d96b4dd6531c29e2119eaed7fb3a75a2f434148f63d220",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/aba230174ac8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f2369b3bc5eec1c3eec1b44b2156f714efa43ce857436e365fe55d6e30f2585a",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "b9af030a25416cddef94851a39b76a81238a0e611415e8bd20a822a064355433",
"EndpointID": "f2369b3bc5eec1c3eec1b44b2156f714efa43ce857436e365fe55d6e30f2585a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
3.过滤镜像信息
inspect 命令可以使用 -f 参数来过滤指定的信息
docker inspect -f "{{.key}}" mysql
我们需要的是mysql对外暴露的端口和挂载数据的目录
取得端口
docker inspect -f "{{.Config.ExposedPorts}}" mysql
# 输出
map[3306/tcp:{} 33060/tcp:{}]
可以得到端口为3306或者33060
取得挂载目录
docker inspect -f "{{.Config.Volumes}}" mysql
# 输出
map[/var/lib/mysql:{}]
可以得到端口为 /var/lib/mysql
4.启动mysql
docker run --name mysql -d -p 3306:3306 -v ${PWD}/mysql:/val/lib/mysql -e MYSQL_ROOT_PASSWORD="123456" mysql
-p 指定端口映射
-v 指定目录映射
-e 指定环境变量
mysql支持的环境变量可以从下网址查到
https://hub.docker.com/_/mysql?tab=description
5.连接方式
使用宿主机ip:端口号的方式可以连接到数据库
192.168.1.123:3306
5.使用python测试数据库连接
需要下载pymysql模块
import pymysql
if __name__ == "__main__":
conn = pymysql.Connect(host='192.168.1.123',
user='root',
password='123456',
port=3306,
db='mysql')
cur = conn.cursor()
print(cur.execute("show tables"))
print(*cur.fetchall())
conn.close()
能输出mysql的表名即连接成功