Docker安装MySQL

在Docker Hub上查询可用版本


MySQL可用版本

下载并安装8.0版本

(base) ArchitectRoad: joey$docker pull mysql:8.0
(base) ArchitectRoad: joey$docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        8         c1558761f285   4 days ago   519MB
mysql        8.0       c1558761f285   4 days ago   519MB

运行mySQL

#启动容器
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0  --lower_case_table_names=1

(base) ArchitectRoad: joey$docker stats

CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT    MEM %     NET I/O       BLOCK I/O        PIDS
b43a801ca528   mysql     144.24%   416.3MiB / 1.94GiB   20.96%    1.11kB / 0B   65.7MB / 201MB   44

可以从docker-cli进入进行命令行交互


docker-cli
(base) ArchitectRoad: joey$docker exec -it b43a801ca528f36bd67c1a9ae0d8b4703733ee31f33958d98f7e0378fd1db5aa /bin/sh
# mysql -u root -p
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.

用IDE连接时,如果遇到错误

2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found

这是因为mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。
解决问题方法有两种,一种是升级navicat驱动,一种是把mysql用户登录密码加密规则还原成mysql_native_password。

这里把加密规则改回为native方式

ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'root';

至此,数据库可成功连接


连接成功

你可能感兴趣的:(Docker安装MySQL)