Docker和MySQL

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


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
关系型数据库 MySQL Linux
Docker安装Mysql5.7,解决无法访问DockerHub问题
当 Docker Hub 无法访问时,可以通过配置国内镜像加速来解决应用安装失败和镜像拉取超时的问题。本文介绍了如何在 CentOS 上一键配置国内镜像加速,并成功拉取 MySQL 5.7 镜像。
310 2
Docker安装Mysql5.7,解决无法访问DockerHub问题
|
19天前
|
关系型数据库 MySQL Docker
docker环境下mysql镜像启动后权限更改问题的解决
在Docker环境下运行MySQL容器时,权限问题是一个常见的困扰。通过正确设置目录和文件的权限,可以确保MySQL容器顺利启动并正常运行。本文提供了多种解决方案,包括在主机上设置正确的权限、使用Dockerfile和Docker Compose进行配置、在容器启动后手动更改权限以及使用 `init`脚本自动更改权限。根据实际情况选择合适的方法,可以有效解决MySQL容器启动后的权限问题。希望本文对您在Docker环境下运行MySQL容器有所帮助。
33 1
|
1月前
|
关系型数据库 MySQL 数据库
使用Docker部署的MySQL数据库,数据表里的中文读取之后变成问号,如何处理?
【10月更文挑战第1天】使用Docker部署的MySQL数据库,数据表里的中文读取之后变成问号,如何处理?
59 3
|
1月前
|
关系型数据库 MySQL 数据库
使用Docker部署的MySQL数据库如何设置忽略表名大小写?
【10月更文挑战第1天】使用Docker部署的MySQL数据库如何设置忽略表名大小写?
158 1
|
1月前
|
弹性计算 关系型数据库 MySQL
Docker安装MySQL
这篇文章详细介绍了如何使用Docker安装MySQL数据库服务,包括拉取镜像、配置数据卷以及启动容器的步骤。
302 0
Docker安装MySQL
|
1月前
|
关系型数据库 MySQL 数据库
如何使用Docker部署MySQL数据库?
【10月更文挑战第1天】如何使用Docker部署MySQL数据库?
174 0
|
1月前
|
关系型数据库 MySQL 数据库
docker mysql表名和数据库名不区分大小写
docker mysql表名和数据库名不区分大小写
19 0
|
11天前
|
SQL 关系型数据库 MySQL
12 PHP配置数据库MySQL
路老师分享了PHP操作MySQL数据库的方法,包括安装并连接MySQL服务器、选择数据库、执行SQL语句(如插入、更新、删除和查询),以及将结果集返回到数组。通过具体示例代码,详细介绍了每一步的操作流程,帮助读者快速入门PHP与MySQL的交互。
26 1
|
13天前
|
SQL 关系型数据库 MySQL
go语言数据库中mysql驱动安装
【11月更文挑战第2天】
29 4
|
20天前
|
监控 关系型数据库 MySQL
数据库优化:MySQL索引策略与查询性能调优实战
【10月更文挑战第27天】本文深入探讨了MySQL的索引策略和查询性能调优技巧。通过介绍B-Tree索引、哈希索引和全文索引等不同类型,以及如何创建和维护索引,结合实战案例分析查询执行计划,帮助读者掌握提升查询性能的方法。定期优化索引和调整查询语句是提高数据库性能的关键。
96 1
下一篇
无影云桌面