Ubuntu18.04 使用Docker安装MySQL并在宿主机登录

Ubuntu18.04 使用Docker安装MySQL并在宿主机登录

  • 安装Docker
  • 拉取MySQL
  • 启动Docker中的MySQL
  • 连接MySQL

安装Docker

  • 如果有旧版本的话,需要先卸载旧版本

按照官方文档的教程:

  1. 准备工作
# 先卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc

# 更新软件包索引,嫌弃国外软件源慢的可以自行更换软件源
sudo apt-get update

# 安装依赖
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

# 添加Docker的官方GPG密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 验证安装:9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88通过搜索指纹的最后8个字符,验证您现在拥有带指纹的密钥。
sudo apt-key fingerprint 0EBFCD88
    
# 输出这个结果表示添加成功
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <[email protected]>
sub   rsa4096 2017-02-22 [S]

  1. 开始安装
# 更新apt包索引
sudo apt-get update

# 安装最新版docker
sudo apt-get install docker-ce

  • 至此Docker已经安装完成

拉取MySQL

  1. 查找Docker Hub上的mysql镜像
docker search mysql

返回结果,按星级排序:

NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   7960                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2662                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   598                                     [OK]
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       176                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          110                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   92                                      [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be link…   60                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   50                                      
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   41                                      
tutum/mysql                                            Base docker image to run a MySQL database se…   31                                      
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   25                                      [OK]
bitnami/mysql                                          Bitnami MySQL Docker Image                      25                                      [OK]
zabbix/zabbix-web-apache-mysql                         Zabbix frontend based on Apache web-server w…   24                                      [OK]
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        20                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   20                                      
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   13                                      
circleci/mysql                                         MySQL is a widely used, open-source relation…   12                                      
mysql/mysql-router                                     MySQL Router provides transparent routing be…   8                                       
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                6                                       [OK]
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
jelastic/mysql                                         An image of the MySQL database server mainta…   1                                       
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]
widdpim/mysql-client                                   Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]
  1. 选择安装版本
# 5.6是版本号
docker pull mysql:5.6
  1. 查看刚安装的MySQL
docker images | mysql

# 返回结果
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.6                 bc788a6a2745        33 hours ago        256MB

启动Docker中的MySQL

  1. 启动MySQL
# 要执行的命令,将Docker中的3306端口映射到宿主机的3306
docker run -p 3306:3306 --name mysql56 -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
# 返回结果
0d9288bf06fba3d67d75c1a1f3c4ac74182c185e33c3c4a428401686ef5fb818

# 命令释义:
-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
-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 用户的密码。

  1. 查看启动状态
# 要执行的命令
docker ps

# 返回结果
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
0d9288bf06fb        mysql:5.6           "docker-entrypoint.s…"   19 seconds ago      Up 18 seconds       0.0.0.0:3306->3306/tcp   mysql56

连接MySQL

  • 如果宿主机中没有安装MySQL客户端,需要先安装MySQL客户端
# 二选一,装一个就好
apt install mysql-client-core-5.7   
apt install mariadb-client-core-10.1

# 连接
mysql -u root -p 123456

# 不出以外会报错
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

在Docker中安装的MySQL和宿主机是两个概念,需要走tcp连接。
所以原来的socket连接的命令就会报错。

  • 需要使用tcp连接命令来连接Docker中安装的MySQL
# 这样就可以连接到MySQL了
mysql -h 127.0.0.1 -P 3306 -u root -p

你可能感兴趣的:(Linux,Docker,MySQL)