当前位置:   article > 正文

Mac M系列芯片(M1/M2)Docker安装MySQL并持久化数据与配置_mac m2 docker mysql

mac m2 docker mysql

本文基于MySQL5.7.42进行说明

在 Docker 镜像仓库查找 MySQL 镜像

DockerHub镜像仓库-mysql镜像

拉取MySQL镜像 

常规命令(不适用),提示no matching manifest for linux/arm64/v8 in the manifest list entries

docker pull mysql:5.7.42

 到DockerHub查看MySQL5.7.42的镜像,可以看到确实没有linux/arm64/v8版本的镜像

 因此我们要另辟蹊径,加入指定架构的参数进行拉取

docker pull --platform linux/amd64 mysql:5.7.42

--platform linux/amd64:Docker拉取镜像时指定拉取所选架构的版本

查看镜像

 在应用端的镜像界面和启动镜像的时候,Docker会检测到该镜像支持的架构与电脑的CPU架构不一致,忽略即可(M1/M2芯片也是支持linux/amd64版本的)

启动镜像并查看MySQL具体信息

  1. # 启动镜像并进入容器
  2. docker run -it mysql:5.7.42 /bin/bash
  3. # 查看MySQL版本
  4. mysql --version
  5. # 查找 Docker 内,MySQL 配置文件 my.cnf 的位置
  6. mysql --help | grep my.cnf

查看MySQL配置文件my.cnf内容(如下)

  1. bash-4.2# cat my.cnf
  2. # For advice on how to change settings please see
  3. # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
  4. [mysqld]
  5. #
  6. # Remove leading # and set to the amount of RAM for the most important data
  7. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  8. # innodb_buffer_pool_size = 128M
  9. #
  10. # Remove leading # to turn on a very important data integrity option: logging
  11. # changes to the binary log between backups.
  12. # log_bin
  13. #
  14. # Remove leading # to set options mainly useful for reporting servers.
  15. # The server defaults are faster for transactions and fast SELECTs.
  16. # Adjust sizes as needed, experiment to find the optimal values.
  17. # join_buffer_size = 128M
  18. # sort_buffer_size = 2M
  19. # read_rnd_buffer_size = 2M
  20. skip-host-cache
  21. skip-name-resolve
  22. datadir=/var/lib/mysql
  23. socket=/var/run/mysqld/mysqld.sock
  24. secure-file-priv=/var/lib/mysql-files
  25. user=mysql
  26. # Disabling symbolic-links is recommended to prevent assorted security risks
  27. symbolic-links=0
  28. #log-error=/var/log/mysqld.log
  29. pid-file=/var/run/mysqld/mysqld.pid
  30. [client]
  31. socket=/var/run/mysqld/mysqld.sock
  32. !includedir /etc/mysql/conf.d/
  33. !includedir /etc/mysql/mysql.conf.d/

默认配置并未指定字符集,此时默认字符集为latin1,可能会导致一些中文字符乱码,因此我们可以修改配置文件,添加默认字符集(见下节)

运行镜像并配置持久化

准备

1. 宿主机创建数据存储目录(可以是其他位置,/Users/leo笔者Mac上的用户目录,等价于~目录,读者自行调整目录)

  1. mkdir /Users/leo/docker/mysql
  2. mkdir /Users/leo/docker/mysql/conf
  3. mkdir /Users/leo/docker/mysql/data
  4. mkdir /Users/leo/docker/mysql/logs
  5. mkdir /Users/leo/docker/mysql/mysql-files

2. 新建my.cnf配置文件并放到/Users/leo/docker/mysql/conf目录下

my.cnf配置如下:

  1. [client]
  2. default_character_set=utf8
  3. [mysql]
  4. default_character_set=utf8
  5. [mysqld]
  6. character_set_server=utf8

运行容器

  1. docker run --restart=always -p 3306:3306 --name mysql \
  2. -e MYSQL_ROOT_PASSWORD="123456" \
  3. -v /Users/leo/docker/mysql/conf:/etc/mysql/conf.d \
  4. -v /Users/leo/docker/mysql/logs:/var/log/mysql \
  5. -v /Users/leo/docker/mysql/data:/var/lib/mysql \
  6. -v /Users/leo/docker/mysql/mysql-files:/var/lib/mysql-files \
  7. -d mysql:5.7.42

参数说明:

  • docker run:运行Docker镜像
  • –restart=always:当docker重启时,该容器也会重新启动
  • -p:映射端口号,宿主机端口映射到容器内部端口(即访问宿主机的3306端口即可访问到容器的3306端口)
  • –name mysql:设置容器名称为mysql
  • -e MYSQL_ROOT_PASSWORD="123456":指定环境变量(初始化 root 用户的密码为 123456
  • -v /Users/leo/docker/mysql/conf:/etc/mysql/conf.d:将宿主机的my.cnf文件映射到Docker容器中
  • -v /Users/leo/docker/mysql/logs:/var/log/mysql:将容器的/var/log/mysql目录挂载到宿主机的/Users/leo/docker/mysql/logs目录
  • -v /Users/leo/docker/mysql/data:/var/lib/mysql:同上
  • -v /Users/leo/docker/mysql/mysql-files:/var/lib/mysql-files:同上
  • -d mysql:5.7.42:选择运行的Docker镜像并指定Tag(不指定的话默认是latest)

测试

  1. # 进入名为mysql的容器
  2. docker exec -ti mysql bash
  3. # 命令行连接MySQL
  4. mysql -uroot -p'jY%kng8cc&'
  5. # 查看字符集
  6. show variables like 'char%';

参考文章

1. 使用Docker安装并启动MySQL

2. DockerHub-MySQL

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/266921
推荐阅读
相关标签
  

闽ICP备14008679号