首先先下载mysql,我这里下载的是msyql5.6

docker pull mysql:5.6
docker run
-p 12345:3306 #绑定mysql的端口
--name mysql #重命名mysql服务 不加这个名称是随机
-v /danbro/mysql/conf:/etc/mysql/conf.d#绑定共享的配置文件 
-v /danbro/mysql/logs:/logs #绑定共享的日志文件
-v /danbro/mysql/data:/var/lib/mysql #绑定共享的数据文件
-e MYSQL_ROOT_PASSWORD=123456 #设置mysql的账号和密码
-d mysql:5.6#-d 表示daemon 后台运行

输入以上指令会返回一串id,输入docker ps可以看到mysql在运行

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
b755e7bdfc04        mysql:5.6           "docker-entrypoint.s…"   11 seconds ago      Up 9 seconds        0.0.0.0:12345->3306/tcp   mysql

进入mysql的容器,进入交互模式

docker exec -it b755e7bdfc04 /bin/bash

输入你的mysql账号和密码

顺利进入msyql

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 1
Server version: 5.6.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql> 

可以通过win10的宿主机访问docker上的mysql

用navicat连接宿docker的端口12345,虚拟机的ip(因为我的docker是在centos上跑的)账号密码为root和123456,连接就能成功。

例如我在宿主机的navicat创建一个数据库test,一个表test1

docker 安装mysql

你在docker上的mysql可以看到你创建的表和数据库

docker 安装mysql

 

docker上的mysql数据备份到宿主机的指定目录上

docker exec mysql服务容器ID sh -c ' exec mysqldump --all-databases -uroot -p"123456" ' > /danbro/all-databases.sql

 

相关文章:

  • 2021-12-29
  • 2021-11-21
  • 2021-05-02
  • 2021-05-07
  • 2021-09-29
  • 2022-02-04
猜你喜欢
  • 2022-01-20
  • 2021-07-23
  • 2021-11-28
  • 2022-01-06
  • 2021-09-22
  • 2021-09-15
相关资源
相似解决方案