Fork me on GitHub

Docker安装MySQL8.0

Docker安装MySQL8.0

本文中记录的是利用Docker安装MySQL8.0。因为工作经常要使用SQL,学习的MySQL5.7的语法已经不能完全满足需求,必须要x学习下最新的MySQL8.0,因为它有很多最近的函数和方法其老版本没有的,所以利用Docker安装了一个用来学习该版本的语法。

安装过程

首先需要下载和启动Docker,自行解决

查看Docker命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# docker的命令

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
deploy Deploy a new stack or update an existing stack
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

拉取mysql镜像

1
2
3
4
docker ps   # 查看镜像
docker pull mysql:8.0 # 拉取指定的MySQL版本8.0
docker pull mysql # 默认拉取最新mysql版本,肯定是8.0
docker search mysql # 搜索mysql相关的镜像

查看mysql镜像

第一次启动

1
docker run -p 3308:3306 --name Peter  -e MYSQL_ROOT_PASSWORD=yourpassword -d mysql:latest

解释下这条命令的参数:

  • -p 3308:3306:将容器内服务端口3306映射到本机的3308端口(本机端口:容器服务端口)
  • --name Peter:给容器取名字
  • -e MYSQL_ROOT_PASSWORD=yourpassworddocker的MySQL默认的root密码是随机的,改一下默认的root用户密码yourpassword
  • -d mysql:latest:在后台运行mysql:latest镜像产生的容器
1
docker run -p 本地主机端口号:容器服务端口号 --name 容器名字 [-e 配置信息修改] -d 镜像名字

之后的启动MySQL:docker start Peter

解决连接报错-修改密码

MySQL8.0之后更改了密码的加密规则,只要在命令窗口把加密方法改回去

进入mysql容器

1
2
docker exec -it Peter bash   # 1、进入创建的镜像
mysql —uroot -p # 2、登陆MySQL,需要输入自己设置的密码

修改密码

1
2
3
alter user 'root'@'%' identified by 'your_password' password expire never;
alter user 'root'@'%' identified with mysql_native_password by 'your_password';
flush privileges; -- 刷新权限

重启进入容器

先启动Docker,再运行下面的命令:

1
2
3
$ docker start Peter
$ docker exec -it Peter bash
$ mysql -uroot -p

进入之后的界面:

参考资料

https://www.jianshu.com/p/2f4bcf5bf418

https://blog.csdn.net/csdnssssss/article/details/105564148

本文标题:Docker安装MySQL8.0

发布时间:2020年12月02日 - 15:12

原始链接:http://www.renpeter.cn/2020/12/02/Docker%E5%AE%89%E8%A3%85MySQL8.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea