Docker和MySQL

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 在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:/#


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
30天前
|
存储 关系型数据库 MySQL
美团面试:MySQL为什么 不用 Docker部署?
45岁老架构师尼恩在读者交流群中分享了关于“MySQL为什么不推荐使用Docker部署”的深入分析。通过系统化的梳理,尼恩帮助读者理解为何大型MySQL数据库通常不使用Docker部署,主要涉及性能、管理复杂度和稳定性等方面的考量。文章详细解释了有状态容器的特点、Docker的资源隔离问题以及磁盘IO性能损耗,并提供了小型MySQL使用Docker的最佳实践。此外,尼恩还介绍了Share Nothing架构的优势及其应用场景,强调了配置管理和数据持久化的挑战。最后,尼恩建议读者参考《尼恩Java面试宝典PDF》以提升技术能力,更好地应对面试中的难题。
|
22天前
|
关系型数据库 MySQL 数据库
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
122 42
|
23天前
|
关系型数据库 MySQL 数据库
Docker下Mysql8数据备份与恢复
通过以上步骤,您可以在Docker环境下高效地备份和恢复MySQL 8数据库。备份数据时,使用 `mysqldump`工具生成逻辑备份文件,并存储到指定目录;恢复数据时,使用 `mysql`工具从备份文件中读取数据并恢复到数据库。自动化脚本和定时任务的配置可以进一步简化备份和恢复的管理过程。
83 41
|
1月前
|
关系型数据库 MySQL Docker
docker pull mysql:8.0.26提示Error response from daemon: Get “https://registry-1.docker.io/v2/“: EOF错误
docker pull mysql:8.0.26提示Error response from daemon: Get “https://registry-1.docker.io/v2/“: EOF错误
|
1月前
|
SQL 关系型数据库 MySQL
docker-compose部署mysql8
使用docker-compose容器化部署mysql8
|
2月前
|
NoSQL 关系型数据库 Redis
《docker高级篇(大厂进阶):1.Docker复杂安装详说》包括:安装mysql主从复制、安装redis集群
《docker高级篇(大厂进阶):1.Docker复杂安装详说》包括:安装mysql主从复制、安装redis集群
137 14
|
1月前
|
关系型数据库 MySQL 应用服务中间件
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
135 7
|
1月前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
|
13天前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
67 25
|
7天前
|
SQL 关系型数据库 MySQL
数据库数据恢复——MySQL简介和数据恢复案例
MySQL数据库数据恢复环境&故障: 本地服务器,安装的windows server操作系统。 操作系统上部署MySQL单实例,引擎类型为innodb,表空间类型为独立表空间。该MySQL数据库没有备份,未开启binlog。 人为误操作,在用Delete命令删除数据时未添加where子句进行筛选导致全表数据被删除,删除后未对该表进行任何操作。