文章目录
- 一、云耀云服务器产品优势
- 1、智能不卡顿
- 2、价优随心用
- 3、上手更简单
- 4、管理更省心
- 二、远程连接云耀云服务器L实例
- 三、安装Docker、docker-compse
- 1、docker安装
- 2、docker-compose安装
- 四、方法① 使用Docker安装部署MySQL服务
- 五、方法② 使用docker-compse安装部署MySQL服务(可选)
- 六、连接MySQL—phpMyAdmin管理工具
一、云耀云服务器产品优势
1、智能不卡顿
- 华为云擎天架构加持,软硬协同结合AI算法智能调度,为您提供优质性能。
- 华为云全球存算一张网,数据传输时延比眨眼还短,满足游戏、音视频等低网络时延场景的高要求。
2、价优随心用
- 超高性价比,超低门槛,敞开随心用。
- 多种实例规格,平衡性能与成本。
- 技术升级优化用云成本,普惠更多初创企业和开发者。
3、上手更简单
- 多种镜像、资源组合一键下单。
- 严选多款高品质镜像,经过华为云官方严格测试,覆盖七大场景,预置模板,快速搭建,安全可信。
- 自动化编排,资源一键开通,业务分钟级上线。
- 平滑学习曲线,向导式应用搭建,应用轻松启用。
4、管理更省心
- 拓扑、资源、镜像可视化管理,业务架构透明化管理。
- 贴心服务状态提醒,资源续退一键式操作,实现极简管理。
- 资源负载、安全,使用率实时监控,保障服务永远在线。
- 可选配丰富的安全服务,包括丰富的备份、主机安全等服务,提供最高等级的安全防护。
二、远程连接云耀云服务器L实例
- 查看所创建的云耀云服务器L实例详细配置参数信息。
CPU核数 | 内存大小 | 磁盘空间 | 带宽 | 公网IP地址 | 操作系统 |
---|---|---|---|---|---|
CPU 2核 | 内存 2GiB | 系统盘 40GiB | 带宽 3Mbit/s | 121.37.153.196 | CentOS 7.9 64bit |
- 通过SSH命令远程登录云服务器
ssh root@121.37.153.196
三、安装Docker、docker-compse
1、docker安装
docker安装命令汇总
# sudo yum update -y
# sudo yum install -y docker
# systemctl start docker
# systemctl enable docker
# systemctl status docker
# docker version
- docker版本查询
[root@hcss-ecs-2d95 ~]# docker version
Client:Version: 1.13.1API version: 1.26Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64Go version: go1.10.3Git commit: 7d71120/1.13.1Built: Wed Mar 2 15:25:43 2022OS/Arch: linux/amd64Server:Version: 1.13.1API version: 1.26 (minimum version 1.12)Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64Go version: go1.10.3Git commit: 7d71120/1.13.1Built: Wed Mar 2 15:25:43 2022OS/Arch: linux/amd64Experimental: false
[root@hcss-ecs-2d95 ~]# docker-compose version
Docker Compose version v2.20.3
2、docker-compose安装
# 下载插件
sudo curl -L "https://github.com/docker/compose/releases/download/2.20.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composePS:若直接在云服务器中下载受阻,可以先将软件包下载到本地后,再上传到云服务器中。# 授权
sudo chmod +x /usr/local/bin/docker-compose# 查看docker-compose版本
docker-compose version
四、方法① 使用Docker安装部署MySQL服务
# 拉取mysql镜像
sudo docker pull mysql:5.7.33# 查看拉取的镜像
sudo docker image ls# 启动MySQL服务
sudo docker run -it --rm --name xybdb -e MYSQL_ROOT_PASSWORD=12345678 -d mysql:5.7.33# 进入容器bash-shell
sudo docker exec -it xybdb bash -p# 查看容器内目录结构
ls -al
# drwxr-xr-x 2 root root 6 Apr 10 2021 docker-entrypoint-initdb.d# 在shell中连接数据库
mysql -u root -p -h 127.0.0.1# 显示当前数据库
mysql> show databases;# 停止xybdb容器
sudo docker container stop xybdb# 查看是否删除
sudo docker container ls -a
具体操作过程演示:
[root@hcss-ecs-2d95 ~]# docker pull mysql:5.7.33
Trying to pull repository docker.io/library/mysql ...
5.7.33: Pulling from docker.io/library/mysql
f7ec5a41d630: Pull complete
9444bb562699: Pull complete
6a4207b96940: Pull complete
181cefd361ce: Pull complete
8a2090759d8a: Pull complete
15f235e0d7ee: Pull complete
d870539cd9db: Pull complete
7310c448ab4f: Pull complete
4a72aac2e800: Pull complete
b1ab932f17c4: Pull complete
1a985de740ee: Pull complete
Digest: sha256:e42a18d0bd0aa746a734a49cbbcc079ccdf6681c474a238d38e79dc0884e0ecc
Status: Downloaded newer image for docker.io/mysql:5.7.33[root@hcss-ecs-2d95 ~]# sudo docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest f5a6b296b8a2 9 days ago 187 MB
docker.io/nginx stable-alpine 6dae3976ee05 5 weeks ago 41.1 MB
docker.io/mysql 5.7.33 450379344707 2 years ago 449 MB[root@hcss-ecs-2d95 ~]# sudo docker run -it --rm --name xybdb -e MYSQL_ROOT_PASSWORD=12345678 -d mysql:5.7.33
8d2a077a12b70528e62267902eedf61f3132a91018696e395bc17c26ab40855a[root@hcss-ecs-2d95 ~]# sudo docker exec -it xybdb bash -proot@8d2a077a12b7:/# ls -al
total 80
drwxr-xr-x 1 root root 4096 Sep 17 02:15 .
drwxr-xr-x 1 root root 4096 Sep 17 02:15 ..
-rwxr-xr-x 1 root root 0 Sep 17 02:15 .dockerenv
drwxr-xr-x 1 root root 4096 Apr 10 2021 bin
drwxr-xr-x 2 root root 4096 Mar 19 2021 boot
drwxr-xr-x 5 root root 360 Sep 17 02:15 dev
drwxr-xr-x 2 root root 4096 Apr 10 2021 docker-entrypoint-initdb.d
lrwxrwxrwx 1 root root 34 Apr 10 2021 entrypoint.sh -> usr/local/bin/docker-entrypoint.sh
drwxr-xr-x 1 root root 4096 Sep 17 02:15 etc
drwxr-xr-x 2 root root 4096 Mar 19 2021 home
drwxr-xr-x 1 root root 4096 Apr 10 2021 lib
drwxr-xr-x 2 root root 4096 Apr 8 2021 lib64
drwxr-xr-x 2 root root 4096 Apr 8 2021 media
drwxr-xr-x 2 root root 4096 Apr 8 2021 mnt
drwxr-xr-x 2 root root 4096 Apr 8 2021 opt
dr-xr-xr-x 110 root root 0 Sep 17 02:15 proc
drwx------ 1 root root 4096 Apr 10 2021 root
drwxr-xr-x 1 root root 4096 Sep 17 02:15 run
drwxr-xr-x 2 root root 4096 Apr 8 2021 sbin
drwxr-xr-x 2 root root 4096 Apr 8 2021 srv
dr-xr-xr-x 13 root root 0 Sep 13 14:24 sys
drwxrwxrwt 1 root root 4096 Sep 17 02:15 tmp
drwxr-xr-x 1 root root 4096 Apr 8 2021 usr
drwxr-xr-x 1 root root 4096 Apr 8 2021 varroot@8d2a077a12b7:/# mysql -u root -p -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> exit;
Bye
root@8d2a077a12b7:/# exit
exit
[root@hcss-ecs-2d95 ~]#[root@hcss-ecs-2d95 ~]# sudo docker container stop xybdb
xybdb
[root@hcss-ecs-2d95 ~]# sudo docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f20c79f487c nginx:stable-alpine "/docker-entrypoin..." 2 days ago Up 2 days 0.0.0.0:80->80/tcp root-mynginx-1
[root@hcss-ecs-2d95 ~]#
五、方法② 使用docker-compse安装部署MySQL服务(可选)
## 编辑文件目录
# 创建mysql目录
[root@hcss-ecs-2d95 ~]# mkdir mysql# 数据库初始化脚本目录
[root@hcss-ecs-2d95 ~]# mkdir mysql/initdb# 数据库数据存储目录
[root@hcss-ecs-2d95 ~]# mkdir mysql/datadir# 创建docker-compose.yml
version: "3"
services: mysql:restart: alwaysimage: mysql:5.7.33 # 指定Docker镜像名ports:- 3306:3306container_name: xyb_mysql-servervolumes:- ./mysql/initdb:/initdb:/docker-entrypoint-initdb.d # 启动初期数据绑定位置- ./mysql/datadir:/var/lib/mysql # 数据库数据存储位置指定environment:- MYSQL_ROOT_PASSWORD=12345678 # 设定数据库root用户名密码# 编译服务
[root@hcss-ecs-2d95 ~]# docker-compose build# 容器启动
[root@hcss-ecs-2d95 ~]# docker-compose up -d# 查询容器状态
[root@hcss-ecs-2d95 ~]# docker-compose ps
[root@hcss-ecs-2d95 ~]# nmap 127.0.0.1Starting Nmap 6.40 ( http://nmap.org ) at 2023-09-17 15:11 CST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000040s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
22/tcp open ssh
25/tcp open smtp
3306/tcp open mysqlNmap done: 1 IP address (1 host up) scanned in 1.59 seconds# 连接mysql client
[root@hcss-ecs-2d95 ~]# docker exec -it xyb_mysql-server -p# 执行myweb容器内的命令
[root@hcss-ecs-2d95 ~]# docker-compose run mysql ls# 查看容器输出日志
[root@hcss-ecs-2d95 ~]# docker-compose logs -f mysql# 容器停止
[root@hcss-ecs-2d95 ~]# docker-compose stop# 容器停止+容器删除(容器+网络)
[root@hcss-ecs-2d95 ~]# docker-compose down# 容器停止+消除(容器+网络+镜像)
[root@hcss-ecs-2d95 ~]# docker-compose down --rmi all# 查看docker-compose服务
[root@hcss-ecs-2d95 ~]# docker-compose ps
具体操作演示步骤如下:
[root@hcss-ecs-2d95 ~]# docker-compose up -d
[+] Running 2/2✔ Network root_default Created 0.0s✔ Container xyb_mysql-server Started 0.0s[root@hcss-ecs-2d95 ~]# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
xyb_mysql-server mysql:5.7.33 "docker-entrypoint.s…" mysql 47 seconds ago Up 47 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp[root@hcss-ecs-2d95 ~]# docker exec -it xyb_mysql-server bash -p
root@1f06e6313fe4:/# mysql -u root -p -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> exit;
Bye
root@1f06e6313fe4:/# exit
exit
[root@hcss-ecs-2d95 ~]#
六、连接MySQL—phpMyAdmin管理工具
- 编辑
docker-compose.yml
配置文件
version: '3'
services: phpmyadmin: image: phpmyadmin:latestcontainer_name: web_phpmyadminports: - 80:80environment: - PMA_HOST=121.37.153.196- PMA_USER=root- PMA_PASSWORD=12345678
- 启动phpmyAdmin管理工具
[root@hcss-ecs-2d95 ~]# docker-compose build[root@hcss-ecs-2d95 ~]# docker-compose up -d[root@hcss-ecs-2d95 ~]# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUSPORTS
web_phpmyadmin phpmyadmin:latest "/docker-entrypoint.…" phpmyadmin 14 seconds ago Up 12 seconds 0.0.0.0:80->80/tcp
xyb_mysql-server mysql:5.7.33 "docker-entrypoint.s…" mysql 14 minutes ago Up 14 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp
具体操作演示如下:
[root@hcss-ecs-2d95 ~]# docker-compose build
[+] Building 0.0s (0/0) docker-container:default
[root@hcss-ecs-2d95 ~]# docker-compose up -d
[+] Running 19/19✔ phpmyadmin 18 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 164.5s✔ 360eba32fa65 Pull complete 140.8s✔ 19de89a948b7 Pull complete 2.0s✔ be0f9df298ac Pull complete 152.3s✔ cda134203c51 Pull complete 3.6s✔ 299efc6dd925 Pull complete 24.1s✔ a2655c36e196 Pull complete 25.9s✔ 4b4b90a10a72 Pull complete 27.5s✔ 3e9322c2e08a Pull complete 42.1s✔ d146bc20b686 Pull complete 44.8s✔ a4cf3c661f4b Pull complete 56.0s✔ b5041d3c27cb Pull complete 57.9s✔ 16c26bb3efa9 Pull complete 59.5s✔ bfc8d5385a83 Pull complete 61.3s✔ daa3dd07c059 Pull complete 64.9s✔ 4ee1192433cd Pull complete 66.8s✔ d9d45f7ae99e Pull complete 84.2s✔ 9d47aac644f3 Pull complete 86.1s✔ f9290a237af1 Pull complete 87.7s
WARN[0164] Found orphan containers ([root-mysql-run-5e8f0fa753d1 xyb_mysql-server root-mysql-run-bdafb44722d5]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
[+] Running 1/1✔ Container web_phpmyadmin Started 0.3s
[root@hcss-ecs-2d95 ~]# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUSPORTS
web_phpmyadmin phpmyadmin:latest "/docker-entrypoint.…" phpmyadmin 14 seconds ago Up 12 seconds 0.0.0.0:80->80/tcp
xyb_mysql-server mysql:5.7.33 "docker-entrypoint.s…" mysql 14 minutes ago Up 14 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp
[root@hcss-ecs-2d95 ~]#