Docker和MySQL

简介: 在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:/#


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
关系型数据库 应用服务中间件 nginx
Docker一键安装中间件(RocketMq、Nginx、MySql、Minio、Jenkins、Redis)
本系列脚本提供RocketMQ、Nginx、MySQL、MinIO、Jenkins和Redis的Docker一键安装与配置方案,适用于快速部署微服务基础环境。
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
1296 6
|
存储 关系型数据库 MySQL
MySQL Docker 容器化部署全指南
MySQL是一款开源关系型数据库,广泛用于Web及企业应用。Docker容器化部署可解决环境不一致、依赖冲突问题,实现高效、隔离、轻量的MySQL服务运行,支持数据持久化与快速迁移,适用于开发、测试及生产环境。
1627 4
|
关系型数据库 MySQL Linux
在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾
以上就是在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾的步骤。这个过程就像是一场接力赛,数据从MySQL数据库中接力棒一样传递到备份文件,再从备份文件传递到其他服务器,最后再传递回MySQL数据库。这样,即使在灾难发生时,我们也可以快速恢复数据,保证业务的正常运行。
807 28
|
关系型数据库 MySQL 数据库
为什么 MySQL 不推荐用 Docker 部署?
本文探讨了MySQL是否适合容器化的问题,分析了Docker容器在数据安全、性能瓶颈、状态管理及资源隔离等方面的挑战,并指出目前主流分布式数据库如TDSQL和OceanBase仍倾向于部署在物理机或KVM上。
599 0
|
Ubuntu 关系型数据库 MySQL
容器技术实践:在Ubuntu上使用Docker安装MySQL的步骤。
通过以上的操作,你已经步入了Docker和MySQL的世界,享受了容器技术给你带来的便利。这个旅程中你可能会遇到各种挑战,但是只要你沿着我们划定的路线行进,你就一定可以达到目的地。这就是Ubuntu、Docker和MySQL的灵魂所在,它们为你开辟了一条通往新探索的道路,带你亲身感受到了技术的力量。欢迎在Ubuntu的广阔大海中探索,用Docker技术引领你的航行,随时准备感受新技术带来的震撼和乐趣。
758 16
|
Ubuntu 关系型数据库 MySQL
在Ubuntu系统的Docker上安装MySQL的方法
以上的步骤就是在Ubuntu系统的Docker上安装MySQL的详细方法,希望对你有所帮助!
1630 12
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
813 158
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
2115 152