Docker和MySQL

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 在Ubuntu上使用apt源安装docker

在Ubuntu上使用apt源安装docker

参考文档:https://docs.docker.com/install/linux/docker-ce/ubuntu/


配置源

$ sudo apt-get update
Install packages to allow apt to use a repository over HTTPS:
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.
$ 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) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]
Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.
Note: The lsb_release -cs sub-command below returns the name of your Ubuntu distribution, such as xenial. Sometimes, in a distribution like Linux Mint, you might need to change $(lsb_release -cs) to your parent Ubuntu distribution. For example, if you are using Linux Mint Tessa, you could use bionic. Docker does not offer any guarantees on untested and unsupported Ubuntu distributions.
x86_64 / amd64
armhf
arm64
ppc64le (IBM Power)
s390x (IBM Z)
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
INSTALL DOCKER ENGINE - COMMUNITY
Update the apt package index.


安装docker

$ sudo apt-get update
Install the latest version of Docker Engine - Community and containerd, or go to the next step to install a specific version:
$ sudo apt-get install docker-ce docker-ce-cli containerd.io


安装后

Create the docker group.
$ sudo groupadd docker
Add your user to the docker group.
$ sudo usermod -aG docker $USER
$ newgrp docker
Verify that you can run docker commands without sudo.
scutech@scutech:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/


检查服务是否激活(自动启动),如果没有激活就enable。


# systemctl is-enabled docker
enabled


docker安装mysql

下载pull myql


root@scutech:~# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
Digest: sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2c
Status: Image is up to date for mysql:latest
docker.io/library/mysql:latest
root@scutech:~# 
root@scutech:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              9b51d9275906        44 hours ago        547MB
ubuntu              latest              72300a873c2c        13 days ago         64.2MB
hello-world         latest              fce289e99eb9        14 months ago       1.84kB


运行mysql

docker run -i -t -e MYSQL_ROOT_PASSWORD=dingjia --name=mysql1 mysql


# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
cee4988f659a        mysql               "docker-entrypoint.s…"   13 hours ago        Up 13 hours         3306/tcp, 33060/tcp   friendly_aryabhata

连接进入docker中的mysql的两种方式


docker exec -it mysql1 mysql -uroot -p

docker exec -it mysql bash


挂载主机的目录/infokist/mysql8到mysql的数据目录,将主机的33306映射到docker的3306端口:

docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql8:/var/lib/mysql -p 33306:3306  --name=mysql8 mysql


创建用户


create USER 'scutech'@'%' IDENTIFIED WITH mysql_native_password by 'dingjia';
grant all privileges on *.* to 'scutech'@'%';


客户端连接


root@infokist:/infokist# mysql -uroot -pdingjia -P 33306  --protocol=tcp
mysql: [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 13
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> show processlist;
+----+-----------------+------------------+------+---------+------+------------------------+------------------+
| Id | User            | Host             | db   | Command | Time | State                  | Info             |
+----+-----------------+------------------+------+---------+------+------------------------+------------------+
|  4 | event_scheduler | localhost        | NULL | Daemon  | 1297 | Waiting on empty queue | NULL             |
| 13 | root            | 172.17.0.1:60128 | NULL | Query   |    0 | starting               | show processlist |
+----+-----------------+------------------+------+---------+------+------------------------+------------------+
2 rows in set (0.00 sec)
mysql>


在运行一个5.7的mysql数据库

root@infokist:/infokist# docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql57:/var/lib/mysql -p 33357:3306  --name=mysql57 mysql:5.7
193c9d6af33b54970021e0d5b5efe7eecd5c9c04f3cb1ae7271b8a2cc98bb71c
root@infokist:/infokist# 
root@infokist:/infokist# mysql mysql -uroot -pdingjia -P 33357  --protocol=tcp^C
root@infokist:/infokist# mysql -uroot -pdingjia -P 33357  --protocol=tcp
mysql: [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 2
Server version: 5.7.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> show processlist
    -> ;
+----+------+------------------+------+---------+------+----------+------------------+
| Id | User | Host             | db   | Command | Time | State    | Info             |
+----+------+------------------+------+---------+------+----------+------------------+
|  2 | root | 172.17.0.1:44616 | NULL | Query   |    0 | starting | show processlist |
+----+------+------------------+------+---------+------+----------+------------------+
1 row in set (0.00 sec)
mysql>



创建网络

创建一个mysql-replicatio的网络


scutech@infokist:~$ docker network create -d bridge mysql-replication
a9c563ffbe7306758700aaebeb4a7816599ddba8fce1543b1a644fe9fed2b40a
scutech@infokist:~$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
1075ff680429        bridge              bridge              local
caf15dbbb6aa        host                host                local
a9c563ffbe73        mysql-replication   bridge              local
9f9ca1e119a5        none                null                local

分布运行三个版本的mysql,测试之间的互联

docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql56:/var/lib/mysql -p 33356:3306  --network mysql-replication --name=mysql56 mysql:5.6
docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql57:/var/lib/mysql -p 33357:3306  --network mysql-replication --name=mysql57 mysql:5.7
docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql8:/var/lib/mysql -p 33380:3306  --network mysql-replication --name=mysql80 mysql
docker exec -it mysql57 bash
root@18c98aab21f4:/# mysql -uroot -pdingjia --protocol=tcp  -hmysql80
mysql: [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 8
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> quit

ping试一下


root@da2ac5ce6f38:/# ping mysql57
PING mysql57 (172.18.0.3) 56(84) bytes of data.
64 bytes from mysql57.mysql-replication (172.18.0.3): icmp_seq=1 ttl=64 time=0.179 ms
64 bytes from mysql57.mysql-replication (172.18.0.3): icmp_seq=2 ttl=64 time=0.047 ms
^C
--- mysql57 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1020ms
rtt min/avg/max/mdev = 0.047/0.113/0.179/0.066 ms
root@da2ac5ce6f38:/# ping mysql80
PING mysql80 (172.18.0.4) 56(84) bytes of data.
64 bytes from mysql80.mysql-replication (172.18.0.4): icmp_seq=1 ttl=64 time=0.110 ms
64 bytes from mysql80.mysql-replication (172.18.0.4): icmp_seq=2 ttl=64 time=0.050 ms
^C
--- mysql80 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1029ms
rtt min/avg/max/mdev = 0.050/0.080/0.110/0.030 ms
root@da2ac5ce6f38:/# ping mysql56
PING mysql56 (172.18.0.2) 56(84) bytes of data.
64 bytes from da2ac5ce6f38 (172.18.0.2): icmp_seq=1 ttl=64 time=0.037 ms
64 bytes from da2ac5ce6f38 (172.18.0.2): icmp_seq=2 ttl=64 time=0.028 ms
^C
--- mysql56 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1021ms
rtt min/avg/max/mdev = 0.028/0.032/0.037/0.007 ms
root@da2ac5ce6f38:/#


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9天前
|
关系型数据库 MySQL Docker
Docker 安装 MySQL
Docker 安装 MySQL
39 1
|
1天前
|
关系型数据库 MySQL Linux
一文教会你如何在Linux系统中使用Docker安装Mysql 5.7版本 【详细过程+图解】
这篇文章提供了在Linux系统中使用Docker安装Mysql 5.7版本的详细过程和图解,包括安装指定版本、创建实例、启动、使用Navicat连接测试、文件挂载与端口映射、进入容器、配置文件修改以及重新启动容器等步骤。
一文教会你如何在Linux系统中使用Docker安装Mysql 5.7版本 【详细过程+图解】
|
1天前
|
关系型数据库 MySQL 数据库
成功解决:Navicat 连接虚拟机Docker中的mysql数据库失败(仅限某些特殊情况)
这篇文章介绍了在Ubuntu环境中使用Docker部署Flask项目的方法,包括创建测试项目、设置数据库、构建Flask和Nginx镜像以及容器编排,其中使用了MySQL 5.7作为数据库,Flask 2.0.2作为Web框架,Gunicorn 20.1.0作为应用服务器,Nginx 1.21.4作为反向代理,并解决了Navicat连接Docker中的MySQL数据库失败的问题。
|
8天前
|
NoSQL 关系型数据库 MySQL
无法访问Docker 里的 mysql, redis
无法访问Docker 里的 mysql, redis
9 0
|
12天前
|
SQL 关系型数据库 MySQL
MySQL运行在docker容器中会损失多少性能
MySQL运行在docker容器中会损失多少性能
|
12天前
|
搜索推荐 关系型数据库 MySQL
[mysql]定制封装MySQL的docker镜像
[mysql]定制封装MySQL的docker镜像
|
22天前
|
关系型数据库 MySQL 数据安全/隐私保护
docker安装mysql
docker安装mysql
41 0
|
1月前
|
关系型数据库 MySQL Linux
Docker安装mysql详细教程, mysqld: Can‘t read dir of ‘/etc/mysql/conf.d/‘(报错已解决)
Docker安装mysql详细教程, mysqld: Can't read dir of '/etc/mysql/conf.d/' (Errcode: 2 - No such file or directory) 已解决
|
2月前
|
Ubuntu 关系型数据库 MySQL
docker 安装最新mysql
docker 安装最新mysql
73 3
|
2月前
|
关系型数据库 MySQL 数据库
轻松入门:使用Docker安装MySQL数据库的完全指南
轻松入门:使用Docker安装MySQL数据库的完全指南

热门文章

最新文章