mac上用docker装mysql的一些坑

  • 先在宿主机上创建容器mysql挂载目录
richardyang@:~/Downloads/docker_mysql$mkdir -p conf logs data  
  • 启动容器并挂载目录
richardyang@:~/Downloads/docker_mysql$docker run -p 3307:3306 --name mymysql -v \$PWD/conf:/etc/mysql/conf.d -v \$PWD/logs:/logs -v \$PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=853211 -d 91dadee7afee

-p代表宿主机的3307端口映射到容器的3306端口
-v代表挂载数据卷(volume),将本地的conf, logs, data目录分别挂载到容器中相应的位置,mysql服务产生的日志数据都会在这些m目录下。 -e MYSQL_ROOT_PASSWORD 配置置数据库root密码, -d 是镜像ID。

查看容器是否成功启动

richardyang@:~/Downloads/docker_mysql$docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
701f0aad8738        91dadee7afee        "docker-entrypoint.s…"   2 hours ago         Up 2 hours          33060/tcp, 0.0.0.0:3307->3306/tcp   mymysql

容器启动成功,进入容器中,看能否在容器中连接数据库

richardyang@:~/Downloads/docker_mysql$docker exec -it mymysql /bin/bash
root@701f0aad8738:/$ mysql -uroot -p
Enter password: 
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 8.0.15    |
+-----------+
1 row in set (0.00 sec)

到这里, 数据库已经成功启动了,接下来,试着在宿主机中通过3307端口连接容器中的mysql服务

开始用 mysql -uroot -h0.0.0.0 -P 3307 -p连接不上。

  • 查看权限,mysql默认的root用户绑定了localhost,于是开启root在所有IP端的连接权限
grant all privileges on *.* to root@'%' identified by 'passwd' with grant option;
  • 再次连接,还是有报错。

“Authentication plugin ‘caching_sha2_password’ cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found”

宿主机中的mysql还是5.7版本的,怀疑是本地的客户端和8.0版的mysql 加密方式不匹配导致的。于是更新宿主机中的mysql 版本,由于之前是用brew 安装的,直接运行

richardyang@:~/Downloads/docker_mysql$brew upgrade mysql
  • 更新完毕后进入宿主数据库,执行show databases出现错误

#1449 - The user specified as a definer (‘mysql.infoschema’@‘localhost’) does not exist

不知道原因, 求助了stackoverflow,解释如下

This is because after certain upgrades or downgrades, tables get changed and you need to (re)create some privileges.

mysql_upgrade examines all tables in all databases for incompatibilities with the current version of MySQL Server. mysql_upgrade also upgrades the system tables so that you can take advantage of new privileges or capabilities that might have been added.

大概是说每次更新mysql之后,数据库里的表会有些变化,需要重新创建。

  • 更新Mysql的表
richardyang@:~/Downloads/docker_mysql$mysql_upgrade -uroot -p
  • 再次连接3307端口
richardyang@:~/Downloads/docker_mysql$mysql -uroot  -P3307 -p
Enter password: 

结果成功进入mysql服务。

你可能感兴趣的:(数据库,docker,docker,数据库)