欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker安装mysql

程序员文章站 2024-03-25 23:43:22
...

一、查找Docker Hub上的mysql镜像

[[email protected] docker]# docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   6976                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2227                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   513                                     [OK]
percona                                                Percona Server is a fork of the MySQL relati…   369                 [OK]                
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       126                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          96                                      
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   67                                      [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be link…   59                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          44                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   39                                      
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   34                                      
tutum/mysql                                            Base docker image to run a MySQL database se…   32                                      
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   23                                      [OK]
bitnami/mysql                                          Bitnami MySQL Docker Image                      19                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   16                                      
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        15                                      [OK]
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   10                                      
circleci/mysql                                         MySQL is a widely used, open-source relation…   6                                       
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
mysql/mysql-router                                     MySQL Router provides transparent routing be…   4                                       
jelastic/mysql                                         An image of the MySQL database server mainta…   1                                       
openzipkin/zipkin-mysql                                Mirror of https://quay.io/repository/openzip…   1                                       
cloudfoundry/cf-mysql-ci                               Image used in CI of cf-mysql-release            0                                       
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]

 二、这里我们拉取官方的镜像,标签为5.6

[[email protected] mysql]# docker pull mysql:5.6
5.6: Pulling from library/mysql
802b00ed6f79: Pull complete 
30f19a05b898: Pull complete 
3e43303be5e9: Pull complete 
94b281824ae2: Pull complete 
51eb397095b1: Pull complete 
3f6fe5e46bae: Pull complete 
b5a334ca6427: Pull complete 
115764d35d7a: Pull complete 
719bba2efabc: Pull complete 
284e66788ee1: Pull complete 
0f085ade122c: Pull complete 
Digest: sha256:4c44f46efaff3ebe7cdc7b35a616c77aa003dc5de4b26c80d0ccae1f9db4a372
Status: Downloaded newer image for mysql:5.6

三、等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.6的镜像。

[[email protected] mysql]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        13 days ago         1.84kB
mysql               5.6                 1f47fade220d        2 weeks ago         256MB

 四、使用mysql镜像--运行容器


[[email protected] mysql]# docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
1b4671904bfa1bcdaae36ea5947eea13ef3b52ea5e37f089ac45913b8c2e6e96

命令说明:

-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。

-v -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。

-v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。

-v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。

-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。

 五、查看容器启动情况

[[email protected] mysql]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
1b4671904bfa        mysql:5.6           "docker-entrypoint.s…"   13 seconds ago      Up 11 seconds       0.0.0.0:3306->3306/tcp   mymysql

六、进入容器

[[email protected] mysql]# docker exec -it mymysql bash
[email protected]:/#

七、使用root登录mysql

[email protected]:/# mysql -uroot -p
Enter password: 

 输入在第四步设置的密码,登录成功!