在 Docker 中使用 MySQL
提及虚拟化技术,我可是linuxContainer(LXC)的热爱者。但随着Docker技术的声名鹊起,我想在这展示一下如何使用带有Docker的MyS
提及虚拟化技术,我可是linuxContainer(LXC)的热爱者。但随着Docker技术的声名鹊起,我想在这展示一下如何使用带有Docker的MySQL。
Docker是什么?
实际上,Docker就是LXC的封装。使用起来很有意思。Docker采用LXC来虚拟化每个应用。所以在接下来的示例中,我们会启动chroot环境中一个被封装在自己命名空间内的mysql实例(你也可以设置Cgroups对应的资源)使用Docker的一个亮点就是统一文件系统(aufs)。所以当启动一个Docker容器后,它会记录其aufs总数并只会更新新写入的数据。
Aufs对于大多数应用来说十分有用,并且也能很好地支持数据库测试。在这我只想做个简单的示例-仅仅抛砖引玉,实用性也许并不高- Dockerfile. Dockerfile是Docker镜像的构建脚本
咱们来看看Dockerfile的内容:
FROM Ubuntu
MAINTAINER erkan yanar
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get install -y python-software-properties
RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
RUN add-apt-repository 'deb precise main'
RUN apt-get update
RUN apt-get install -y mariadb-server
RUN echo "[mysqld]" >/etc/mysql/conf.d/docker.cnf
RUN echo "bind-address = 0.0.0.0" >>/etc/mysql/conf.d/docker.cnf
RUN echo "innodb_flush_method = O_DSYNC" >>/etc/mysql/conf.d/docker.cnf
RUN echo "skip-name-resolve" >>/etc/mysql/conf.d/docker.cnf
RUN echo "init_file = /etc/mysql/init" >>/etc/mysql/conf.d/docker.cnf
RUN echo "GRANT ALL ON *.* TO supa@'%' IDENTIFIED BY 'supa';" >/etc/mysql/init
EXPOSE 3306
USER mysql
ENTRYPOINT mysqld
你可以按自己的需求更改。明白了大意后,,可以进一步优化代码。比如,较少运行步骤:)
运行一下看看 (命名为mysql)
> cat $DOCKERFILENAME | docker build -t mysql -
很好!启动51个容器看看:
> time for i in $(seq 10 60 ) ; do docker run -d -p 50$i:3306 mysql ; done
..
real 0m27.446s
user 0m0.264s
sys 0m0.211s
这都是在我笔记本上的结果. 如果使用KVM性能会更好 :)
> docker ps | grep mysqld |wc -l
51
> docker ps | head -2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d3a5181cd56 mysql:latest /bin/sh -c mysqld About a minute ago Up About a minute 0.0.0.0:5060->3306/tcp lonely_pare
快来试试吧 \o/
Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里
开源项目Docker,Red Hat新的虚拟化选择
dockerlite: 轻量级 Linux 虚拟化
Docker的搭建Gitlab CI 全过程详解
Docker 和一个正常的虚拟机有何区别?
Docker 将改变所有事情

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)
