mac通过docker一键部署MySQL8

目录

  • mac通过docker一键部署MySQL8
    • 一、前言
    • 二、系统配置
    • 三、安装步骤
      • Dockerhub查看镜像地址
      • 1、一键安装
        • 1.1、克隆脚本
        • 1.2、安装程序
          • 1.2.1、安装程序详情
          • 1.3、初始化用户
          • 1.3.1、安装程序详情
        • 1.4、测试数据库是否连接成功
    • 四、总结
    • 五、后记
mac通过docker一键部署MySQL8一、前言程序猿开发任务本来就比较繁锁,如果搭建各种开源的软件还需要搜索这样那样的配置,那基本上两鬓斑白的头发基本上都快脱光了,如何找女朋友呢?难道一直和五姑娘为伴?所以自己当第一个吃螃蟹的人摸索了一些套路,编写一些简易的 shell 脚本帮我们快速的搭建服务器,猿们只要按着套路“一步一步”的操作,基本上都可以快速部署服务,也有《MySQL8自动安装脚本》,大家根据自己的实际情况进行操作 。
二、系统配置mac os:Montereymac version:12.4 (21F79)mac type: MacBook Air (M1, 2020), Apple M1docker desktop version:v4.10.0docker version:v20.10.17docker-compose version: v1.29.2三、安装步骤Dockerhub查看镜像地址# 如果你比较懒可以使用最后的“MySQL8自动安装脚本”,mac电脑通用https://hub.docker.com/_/mysql/tags1、一键安装1.1、克隆脚本git clone git@gitee.com:xiaoyaojiugui/technical.git1.2、安装程序# 1.跳转到下载脚本的根目录,执行命令:cd /technical/docker/foundations/database/mysql# 2.执行安装脚本,执行命令:sh ./mysql.sh# 3.选择待安装的 MySQL 序号1.2.1、安装程序详情---------------函数开始执行------------------------------环境变量初始化开始---------------1.1、请选择要安装的 MySQL 版本,例如:MySQL8,输入“1”回车 。1、MySQL8 (Mac with Apple chip or Intel chip)2、MySQL8 (Linux)3、MySQL5请输入序号:11.2、你选择了安装:MySQL8 (Mac with Apple chip or Intel chip)1.3、初始化 MySQL 环境变量image_alias=mysql8image_name=mysql/mysql-server:latestmysql_port=33306os_path=/Users/jason.zhuo/data/docker/volumes/mysql8---------------环境变量初始化完毕---------------1.1、执行创建容器操作,跳过此步骤,镜像已存在[mysql/mysql-server:latest]1.2、执行创建容器操作,创建目录并授权,执行命令:sudo mkdir -p -v /Users/jason.zhuo/data/docker/volumes/mysql8/{data,logs,conf} && sudo chown -R jason.zhuo /Users/jason.zhuo/data/docker/volumes/mysql81.3、执行创建容器操作,创建文件my.cnf并将配置写到文件中[/Users/jason.zhuo/data/docker/volumes/mysql8/conf/my.cnf]1.4、执行创建容器操作,执行命令:docker run -d -p 33306:3306 -d --restart=always --privileged=true -e MYSQL_ROOT_PASSWORD=root123456 -v /Users/jason.zhuo/data/docker/volumes/mysql8/conf/:/etc/mysql/conf.d/ -v /Users/jason.zhuo/data/docker/volumes/mysql8/data:/var/lib/mysql -v /Users/jason.zhuo/data/docker/volumes/mysql8/logs:/var/log/mysql --name mysql8 mysql/mysql-server:latest1.5.1、查看容器状态,执行命令:docker inspect --format='{{.State.Status}}' mysql81.5.2、查看容器状态,[running]1.5.3、查看容器详情,执行命令:docker ps | grep mysql/mysql-server:latest2.1、其他待执行命令,登录容器其命令为:docker exec -it mysql8 mysql -uroot -proot---------------函数执行完毕---------------1.3、初始化用户
  • 初始化用户(admin)默认密码(admin123456);
  • 请在脚本中(create-user.sh),自行更改密码;
# 1.跳转到下载脚本的根目录,执行命令:cd /technical/docker/foundations/database/mysql# 2.执行安装脚本,执行命令:sh ./create-user.sh# 3.选择待安装的 MySQL 序号1.3.1、安装程序详情---------------函数开始执行------------------------------环境变量初始化开始---------------1.1、请选择要安装的 MySQL 版本,例如:MySQL8,输入“1” 回车 。1、MySQL8 (Mac with Apple chip or Intel chip)2、MySQL8 (Linux)3、MySQL5 (通用)请输入序号:11.2、你选择了安装:MySQL8 (Mac with Apple chip or Intel chip)1.3、初始化 MySQL 环境变量image_alias=mysql8image_name=mysql/mysql-server:latestmysql_port=33306os_path=/Users/jason.zhuo/data/docker/volumes/mysql8---------------环境变量初始化完毕---------------1、创建数据库用户(admin)并授权,执行命令:docker exec -it mysql8 mysql -uroot -proot123456spawn docker exec -it mysql8 mysql -uroot -proot123456mysql: [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 10Server version: 8.0.27 MySQL Community Server - GPLCopyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database|+--------------------+| information_schema || mysql|| performance_schema || sys|+--------------------+4 rows in set (0.01 sec)mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> create user 'admin'@'localhost' identified by 'admin123456';Query OK, 0 rows affected (0.02 sec)mysql> grant all privileges on *.* to 'admin'@'localhost';Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> create user 'admin'@'%' identified by 'admin123456';Query OK, 0 rows affected (0.00 sec)mysql> grant all privileges on *.* to 'admin'@'%';Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> select host, user, plugin from user;+-----------+------------------+-----------------------+| host| user| plugin|+-----------+------------------+-----------------------+| %| admin| caching_sha2_password || localhost | admin| caching_sha2_password || localhost | healthchecker| caching_sha2_password || localhost | mysql.infoschema | caching_sha2_password || localhost | mysql.session| caching_sha2_password || localhost | mysql.sys| caching_sha2_password || localhost | root| caching_sha2_password |+-----------+------------------+-----------------------+7 rows in set (0.00 sec)mysql> exitBye2、成功创建数据库用户:admin---------------函数执行完毕---------------

经验总结扩展阅读