Docker 安装Mysql8.0
Docker 安装Mysql
访问 MySQL 镜像库地址:https://hub.docker.com/_/mysql?tab=tags。
可以通过 Sort by 查看其他版本的 MySQL,默认是最新版本 mysql:latest 。
此外,我们还可以用 docker search mysql 命令来查看可用版本(windows下无法显示tags):
NAME # 镜像仓库
DESCRIPTION # 镜像描述信息
STARS # 镜像收藏数
OFFICIAL # 是否为docker官方发布的镜像
AUTOMATED # 是否为自动化构建的镜像,关于自动化构建,可以查看官方文档:https://docs.docker.com/docker-hub/builds/#how-automated-builds-work
拉取Mysql镜像
C:\Users\wang>docker pull mysql:8.0.28
8.0.28: Pulling from library/mysql
6552179c3509: Pull complete
d69aa66e4482: Pull complete
3b19465b002b: Pull complete
7b0d0cfe99a1: Pull complete
9ccd5a5c8987: Pull complete
2dab00d7d232: Pull complete
64d3afdccd4a: Pull complete
82148d50b16c: Pull complete
8bb7d73a7d0c: Pull complete
74778cd68a75: Pull complete
d7e5f9309140: Pull complete
f2e376ecd59f: Pull complete
Digest: sha256:92d27b8222bbcf53bc42c70ca7cd1010d6c0527efc61f14980ce77c50932bef4
Status: Downloaded newer image for mysql:8.0.28
docker.io/library/mysql:8.0.28
查看本地仓库镜像是否下载成功
C:\Users\wang>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.28 d1dc36cf8d9e 2 days ago 519MB
hello-world latest feb5d9fea6a5 4 months ago 13.3kB
C:\Users\wang>docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.28 d1dc36cf8d9e 2 days ago 519MB
运行docker mysql镜像
docker run -p 3308:3306 --name zsdmysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.28
上述命令的参数,有如下含义:
--name
指定了你要取的名字。-p
对应,需要映射出来的端口。比如:3308:3306,意识表示为zsdmysql的容器里面的3306端口对应我外面这个虚拟机的3308端口。-e
是mysql的命令,设置root的密码为123456-d
是运行的镜像,这里是mysql
容器镜像
查看目前运行的容器
C:\Users\wang>docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
622fe2b1bf2a mysql:8.0.28 "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp zsdmysql
进入MySQL
C:\Users\wang>docker exec -it zsdmysql bash
root@622fe2b1bf2a:/# mysql -uroot -p123456
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 8
Server version: 8.0.28 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> show tables
-> ;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
使用datatrip连接数据库
目录映射
docker run -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456
-v d:/docker/mysql/data:/var/lib/mysql:rw
-v d:/docker/mysql/log:/var/log/mysql:rw
-v d:/docker/mysql/mysql-files:/var/lib/mysql-files/
--name mysql8.0 --restart=always -d mysql:8.0.28
–restart=always参数能够使我们在重启docker时,自动启动相关容器。
新建d:/docker/mysql/conf/,拷贝配置文件到宿主机:
C:\Users\wang>docker cp mysql8.0:/etc/mysql/my.cnf d:/docker/mysql/conf/
停止容器:
docker stop mysql8.0
删除容器:
docker rm mysql8.0
重新执行生成容器(增加了对配置文件my.cnf的映射,直接映射,容器启动会有问题,通过执行下面-v后面的映射,最终实现数据持久化,数据存放在宿主机而不是容器内):
docker run -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456
-v d:/docker/mysql/data:/var/lib/mysql:rw
-v d:/docker/mysql/log:/var/log/mysql:rw
-v d:/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf:rw
-v d:/docker/mysql/mysql-files:/var/lib/mysql-files/
--name mysql8.0 --restart=always -d mysql:8.0.28
查看mysql8.0容器运行情况
docker ps
进入mysql
根据上面查询到的容器id执行
docker exec -it mysql8.0 bash
连接mysql
mysql -uroot -p
C:\Users\wang>docker exec -it mysql8.0 bash
root@c1192171b549:/# mysql -uroot -p
mysql: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28 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> create database todou
-> ;
Query OK, 1 row affected (0.05 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| todou |
+--------------------+
5 rows in set (0.05 sec)
mysql> use todou
Database changed
mysql> create person (id int(11) not null )
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'person (id int(11) not null )' at line 1
mysql> create table person (id int(11) not null );
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> show tables;
+-----------------+
| Tables_in_todou |
+-----------------+
| person |
+-----------------+
1 row in set (0.00 sec)
使用datatrip连接
引用