文档章节

MySQL Setup in Docker for Mac

群星纪元
 群星纪元
发布于 2018/08/03 15:37
字数 615
阅读 12
收藏 0

This article will provide step-by-step instructions on how to set up MySQL in Docker for Mac and how to access MySQL from the host (Mac) OS. This article assumes that you know what Docker is, understand how to use MySQL, and understand how to use SQL commands to create a user, create a database, and grant privileges.

Before we jump into action, let's learn the basics of Docker for Mac.

Why Docker For Mac?

Docker is an abstraction on top of Linux containers to simplify using and managing containers. Mac OS is Unix and it doesn't have containers directly, as in Linux OS (Ubuntu, Fedora, Core OS, etc.). Hence, a Virtual Machine is set up on top of Mac OS and has an instance (Guest OS) of Linux to run containers. To simplify Docker and Virtual Machine setup in Mac OS, Docker for Mac was created.

According to this documentation, Docker for Mac is:

...an easy-to-install desktop app for building, debugging, and testing Dockerized apps on a Mac. Docker for Mac is a complete development environment deeply integrated with the MacOS Hypervisor framework, networking, and filesystem. Docker for Mac is the fastest and most reliable way to run Docker on a Mac.

Now that we have background info, let's see how to set up MySQL in Docker for Mac.

Follow these instructions to install Docker for Mac. After installing Docker for Mac, please verify the information below.

Note$ is the prompt. Ignore it and copy the rest of the line.

$docker --version 
Docker version 17.09.0-ce, build afdb6d4

Image title

Run the MySQL image using the docker run command.

$docker run -p 3306:3306 -d --name mysql -e MYSQL_ROOT_PASSWORD=password mysql/mysql-server

This will install the latest version of the MySQL image in Docker Hub. As of this writing, it was 5.7.

If the image was not already available, this command will download the image and run it.

You can verify if MySQL has started using the docker ps command below.

$docker ps CONTAINER ID IMAGE COMMAND CREATED a3fb00c34877 mysql/mysql-server "/entrypoint.sh my..." 2 minutes ago STATUS PORTS NAMES Up 2 minutes (healthy) 0.0.0.0:3306->3306/tcp, 33060/tcp mysql

Log into MySQL within the docker container using the docker exec command:

$docker exec -it mysql bash 
bash-4.2# mysql -uroot -ppassword 
mysql>

Remember, when we created and ran the MySQL container, we provided MYSQL_ROOT_PASSWORD=password.

Create a database and user, and grant privileges in MySQL (from within the container).

Log into MySQL if you haven't already. After login, the mysql> prompt shows up:

bash-4.2# mysql -uarun -ppassword 
mysql>

I create a user named arun, grant all privileges, and quit. 

Important: This step is required to log into MySQL from outside the container. The root user will not be able to log in from the host OS (Mac OS). Use % instead of localhost in arun@localhost.

mysql> CREATE USER 'arun'@'%' IDENTIFIED BY 'password'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON * . * TO 'arun'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> quit

Connect to MySQL running in Docker from MySQL Workbench. If MySQL Workbench is not installed yet, follow these instructions to install it.

Open MySQL Workbench and click on + to add a new connection. Enter all the information as stated in the screenshot and click on Test Connection.

Image title

Image title

Enjoy creating and accessing your MySQL database running in a Docker container! For more MySQL docker run options, refer to here.

本文转载自:https://dzone.com/articles/docker-for-mac-mysql-setup

群星纪元
粉丝 43
博文 456
码字总数 44497
作品 0
朝阳
高级程序员
私信 提问
Kitematic 0.6.6 发布,Docker 容器管理程序

Kitematic 0.6.6 发布,此版本更新内容如下: Bug 修复 Fixed bug where setup would fail if VirtualBox startup services were not running 包含的二进制包 docker-machine docker 1.6.2 ......

oschina
2015/06/13
356
2
Kitematic 0.9.3 发布,Docker 容器管理程序

Kitematic 0.9.3 发布,此版本主要改进内容如下: Pagination in search Improved error reporting Container button is now more responsive About pane is no longer accessible during se......

oschina
2015/11/03
1K
2
Kitematic 0.6.0 发布,Docker 容器管理程序

Kitematic 0.6.0 发布,现已提供下载:Kitematic-0.6.0.zip 新特性: Sign Up and Login to Docker Hub through Kitematic. Listing of "my repositories" stored in Docker Hub Search and ......

sikkx
2015/05/28
1K
1
Kitematic 0.9.0 发布,Docker 容器管理程序

Kitematic 0.9.0 发布,此版本更新内容如下: Loading spinner when starting the app, instead of blank white screen. Fixed issues relating to error handling in Setup Fixed bug where......

oschina
2015/10/20
662
0
Docker安装mysql容器

操作系统:Mac OS X参考文档:https://hub.docker.com/_/mysql/ 方式一 ( 登录以下网页查找) 方式二 (使用docker search命令搜索) 本实例中拉取mysql 8.0版本的镜像 命令说明: -p:配置端口...

mycodetech
2018/07/10
99
0

没有更多内容

加载失败,请刷新页面

加载更多

redis 学习2

网站 启动 服务端 启动redis 服务端 在redis 安装目录下 src 里面 ./redis-server & 可以指定 配置文件或者端口 客户端 在 redis 的安装目录里面的 src 里面 ./redis-cli 可以指定 指定 连接...

之渊
25分钟前
0
0
Spring boot 静态资源访问

0. 两个配置 spring.mvc.static-path-patternspring.resources.static-locations 1. application中需要先行的两个配置项 1.1 spring.mvc.static-path-pattern 这个配置项是告诉springboo......

moon888
今天
3
0
hash slot(虚拟桶)

在分布式集群中,如何保证相同请求落到相同的机器上,并且后面的集群机器可以尽可能的均分请求,并且当扩容或down机的情况下能对原有集群影响最小。 round robin算法:是把数据mod后直接映射...

李朝强
今天
4
0
Kafka 原理和实战

本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/bV8AhqAjQp4a_iXRfobkCQ 作者简介:郑志彬,毕业于华南理工大学计算机科学与技术(双语班)。先后从事过电子商务、开放平...

vivo互联网技术
今天
21
0
java数据类型

基本类型: 整型:Byte,short,int,long 浮点型:float,double 字符型:char 布尔型:boolean 引用类型: 类类型: 接口类型: 数组类型: Byte 1字节 八位 -128 -------- 127 short 2字节...

audience_1
今天
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部