ubuntu 22.04 LTS
docker 20.10.15
docker pull mysql:8.0.29
lyear@us:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04 LTS Release: 22.04 Codename: jammy lyear@us:~$ docker version Client: Docker Engine - Community Version: 20.10.15 API version: 1.41 Go version: go1.17.9 Git commit: fd82621 Built: Thu May 5 13:21:10 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.15 API version: 1.41 (minimum version 1.12) Go version: go1.17.9 Git commit: 4433bf6 Built: Thu May 5 13:19:15 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.4 GitCommit: 212e8b6fa2f44b9c21b2798135fc6fb7c53efc16 runc: Version: 1.1.1 GitCommit: v1.1.1-0-g52de29d docker-init: Version: 0.19.0 GitCommit: de40ad0讀二、拉取 pull mysql
通过docker pull mysql:8.0.29 直接拉取MySQL指定镜像版本,等待其下载
lyear@us:~$ docker pull mysql:8.0.29 # 拉取镜像 8.0.29: Pulling from library/mysql 4be315f6562f: Downloading [=====> ] 3.043MB/27.14MB 96e2eb237a1b: Downloading 8aa3ac85066b: Downloading ac7e524f6c89: Downloading f6a88631064f: Downloading 15bb3ec3ff50: Waiting ae65dc337dcb: Waiting 573c3c7fa18d: Waiting 9d10771b98b8: Waiting 3d8ef442614b: Waiting 7dc17a6cea26: Waiting 752752efdaea: Waiting
下载完成,通过 docker images 命令查看已下载镜像,里面已经有了mysql:8.0.29
lyear@us:~$ docker images # 查看镜像 REPOSITORY TAG IMAGE ID CREATED SIZE redis 7.0.0 993b4fdb4f95 12 hours ago 412MB mysql 8.0.29 96d0eae5ed60 12 days ago 524MB redis latest 7614ae9453d1 4 months ago 113MB debian latest 6f4986d78878 4 months ago 124MB ubuntu 22.04 9d28ccdc1fc7 5 months ago 76.3MB ubuntu latest ba6acccedd29 6 months ago 72.8MB讀三、通过刚才的镜像创建容器
docker run -itd -P -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root --name mysql8029 mysql:8.0.29
lyear@us:~$ docker run -itd -P -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root --name mysql8029 mysql:8.0.29 # 创建容器 4cbb72c53f36b95201f4dd484e028e61d1ce5ab70966e7432fbe7cb104b03e0b lyear@us:~$ docker ps # 查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4cbb72c53f36 mysql:8.0.29 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:49156->3306/tcp, :::49156->3306/tcp, 0.0.0.0:49155->33060/tcp, :::49155->33060/tcp mysql8029讀四、测试容器
docker run -it --rm mysql:8.0.29 mysql -h192.168.118.129 -uroot -P49156 -p
lyear@us:~$ docker run -it --rm mysql:8.0.29 mysql -h192.168.118.129 -uroot -P49156 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 8.0.29 MySQL Community Server - GPL Copyright (c) 2000, 2022, 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> select @@hostname,@@port,@@version,@@basedir,@@datadir; +--------------+--------+-----------+-----------+-----------------+ | @@hostname | @@port | @@version | @@basedir | @@datadir | +--------------+--------+-----------+-----------+-----------------+ | 4cbb72c53f36 | 3306 | 8.0.29 | /usr/ | /var/lib/mysql/ | +--------------+--------+-----------+-----------+-----------------+ 1 row in set (0.00 sec)
在外面用windows也可以访问
mysql -h 192.168.118.129 -uroot -p -P49156
讀五、mysql容器主机配置信息进入容器:docker exec -it 4 /bin/bash,4为容器id开头一位
lyear@us:~$ docker ps # 查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4cbb72c53f36 mysql:8.0.29 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:49156->3306/tcp, :::49156->3306/tcp, 0.0.0.0:49155->33060/tcp, :::49155->33060/tcp mysql8029 lyear@us:~$ docker exec -it 4 /bin/bash # 进入容器 root@4cbb72c53f36:/# cat /etc/os-release # 查看配置信息 PRETTY_NAME="Debian GNU/Linux 10 (buster)" NAME="Debian GNU/Linux" VERSION_ID="10" VERSION="10 (buster)" VERSION_CODENAME=buster ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"
基本的配置文件/etc/mysql/my.cnf
/etc/mysql/mysql.d目录
root@4cbb72c53f36:/# ls /etc/mysql conf.d my.cnf my.cnf.fallback root@4cbb72c53f36:/# ls /etc/mysql/conf.d docker.cnf mysql.cnf root@4cbb72c53f36:/# ls /etc/mysql/my.cnf /etc/mysql/my.cnf root@4cbb72c53f36:/# cat /etc/mysql/my.cnf # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Custom config should go here !includedir /etc/mysql/conf.d/
数据库目录 /var/lib/mysql
root@4cbb72c53f36:/# ls /var/lib/mysql '#ib_16384_0.dblwr' auto.cnf ca-key.pem ib_buffer_pool ibtmp1 private_key.pem sys '#ib_16384_1.dblwr' binlog.000001 ca.pem ib_logfile0 mysql public_key.pem undo_001 '#innodb_temp' binlog.000002 client-cert.pem ib_logfile1 mysql.ibd server-cert.pem undo_002 4cbb72c53f36.err binlog.index client-key.pem ibdata1 performance_schema server-key.pem



