云原生之使用Docker部署Mysql数据库
admin
2024-04-23 09:24:49
0

云原生之使用Docker部署Mysql数据库

  • 一、检查本地系统环境
    • 1.检查系统版本
    • 2.检查docker版本
    • 3.检查docker状态
  • 二、使用Docker部署mysql
    • 1.下载mysql镜像
    • 2.创建数据目录
    • 3.创建mysql容器
    • 4.查看mysql容器状态
  • 三、进入数据库内
    • 1.进入mysql容器
    • 2.进入mysql数据库
  • 四、Linux系统安装Mysql
    • 1.下载官方mysql的rpm包
    • 2.安装mysql的rpm包
    • 3.修改mysql-community.repo文件
    • 4.安装mysql
    • 5.修改my.conf
    • 6.启动mysql服务
    • 7.查看mysql状态
    • 8.本地连接mysql
  • 五、mysql客户端远程连接mysql容器服务

一、检查本地系统环境

1.检查系统版本


[root@jeven ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

2.检查docker版本

[root@jeven ~]# docker version
Client: Docker Engine - CommunityVersion:           20.10.17API version:       1.41Go version:        go1.17.11Git commit:        100c701Built:             Mon Jun  6 23:05:12 2022OS/Arch:           linux/amd64Context:           defaultExperimental:      trueServer: Docker Engine - CommunityEngine:Version:          20.10.17API version:      1.41 (minimum version 1.12)Go version:       go1.17.11Git commit:       a89b842Built:            Mon Jun  6 23:03:33 2022OS/Arch:          linux/amd64Experimental:     falsecontainerd:Version:          1.6.6GitCommit:        10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1runc:Version:          1.1.2GitCommit:        v1.1.2-0-ga916309docker-init:Version:          0.19.0GitCommit:        de40ad0

3.检查docker状态

[root@jeven ~]# systemctl status docker
● docker.service - Docker Application Container EngineLoaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)Active: active (running) since Tue 2022-11-22 18:55:55 CST; 1 day 22h agoDocs: https://docs.docker.comMain PID: 11080 (dockerd)Tasks: 26Memory: 1.7GCGroup: /system.slice/docker.service└─11080 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sockNov 23 19:15:33 jeven dockerd[11080]: time="2022-11-23T19:15:33.027720042+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:15:34 jeven dockerd[11080]: time="2022-11-23T19:15:34.051871774+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:15:34 jeven dockerd[11080]: time="2022-11-23T19:15:34.987822341+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:15:36 jeven dockerd[11080]: time="2022-11-23T19:15:36.117949794+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:15:37 jeven dockerd[11080]: time="2022-11-23T19:15:37.518220737+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:15:39 jeven dockerd[11080]: time="2022-11-23T19:15:39.670714951+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:15:43 jeven dockerd[11080]: time="2022-11-23T19:15:43.532403741+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:15:50 jeven dockerd[11080]: time="2022-11-23T19:15:50.592195925+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:16:04 jeven dockerd[11080]: time="2022-11-23T19:16:04.062121075+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Nov 23 19:16:30 jeven dockerd[11080]: time="2022-11-23T19:16:30.334182866+08:00" level=info msg="ignoring event" container=682123a5e3ab725...skDelete"
Hint: Some lines were ellipsized, use -l to show in full.

二、使用Docker部署mysql

1.下载mysql镜像


[root@jeven ~]#  docker pull mysql:5.7
5.7: Pulling from library/mysql
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Image is up to date for mysql:5.7
docker.io/library/mysql:5.7

2.创建数据目录

[root@jeven ~]# mkdir -p /data/mysql/db
[root@jeven ~]# cd /data/mysql/
[root@jeven mysql]# 

3.创建mysql容器


[root@jeven mysql]# docker run -d --name my-mysql --restart always -v /data/mysql/db:/var/lib/mysql -p 3666:3306 -e MYSQL_ROOT_PASSWORD=admin -e MYSQL_DATABASE=test  mysql:5.7
09a6697c529a1eb3e2dfe3bbaf6923ec81028eb7233b07b163df9dc8f9bc080b

4.查看mysql容器状态


[root@jeven mysql]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
09a6697c529a   mysql:5.7           "docker-entrypoint.s…"   56 seconds ago   Up 53 seconds   33060/tcp, 0.0.0.0:3666->3306/tcp, :::3666->3306/tcp   my-mysql

三、进入数据库内

1.进入mysql容器

[root@jeven mysql]# docker exec -it my-mysql /bin/bash
root@09a6697c529a:/# 

2.进入mysql数据库


root@09a6697c529a:/# mysql -h 127.0.0.1 -P3306 -uroot -padmin
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 3
Server version: 5.7.36 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> 

四、Linux系统安装Mysql

1.下载官方mysql的rpm包

[root@server ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
--2022-11-24 17:57:11--  http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 69.192.12.46, 2600:1417:e800:189::2e31, 2600:1417:e800:18a::2e31
Connecting to dev.mysql.com (dev.mysql.com)|69.192.12.46|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [following]
--2022-11-24 17:57:11--  https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|69.192.12.46|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm [following]
--2022-11-24 17:57:11--  https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 2.16.232.230
Connecting to repo.mysql.com (repo.mysql.com)|2.16.232.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25548 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-10.noarch.rpm’100%[============================================================================================================>] 25,548       127KB/s   in 0.2s   2022-11-24 17:57:12 (127 KB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]-c: No such file or directory
No URLs found in -c.
FINISHED --2022-11-24 17:57:12--
Total wall clock time: 1.3s
Downloaded: 1 files, 25K in 0.2s (127 KB/s)

2.安装mysql的rpm包

[root@server ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
Loaded plugins: fastestmirror
Examining mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
Marking mysql57-community-release-el7-10.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-10 will be installed
--> Finished Dependency ResolutionDependencies Resolved======================================================================================================================================================Package                                  Arch                  Version                 Repository                                               Size
======================================================================================================================================================
Installing:mysql57-community-release                noarch                el7-10                  /mysql57-community-release-el7-10.noarch                 30 kTransaction Summary
======================================================================================================================================================
Install  1 PackageTotal size: 30 k
Installed size: 30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transactionInstalling : mysql57-community-release-el7-10.noarch                                                                                            1/1 Verifying  : mysql57-community-release-el7-10.noarch                                                                                            1/1 Installed:mysql57-community-release.noarch 0:el7-10                                                                                                           Complete!

3.修改mysql-community.repo文件

sed -i "s/gpgcheck=1/gpgcheck=0/g" mysql-community.repo

4.安装mysql

yum -y install mysql-community-server

5.修改my.conf

[root@server yum.repos.d]# grep  skip /etc/my.cnf
skip-grant-tables

6.启动mysql服务


[root@server yum.repos.d]# systemctl start mysqld
[root@server yum.repos.d]# systemctl enable mysqld

7.查看mysql状态

[root@server yum.repos.d]# systemctl status mysqld
● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since Thu 2022-11-24 18:05:02 CST; 29s agoDocs: man:mysqld(8)http://dev.mysql.com/doc/refman/en/using-systemd.htmlMain PID: 21810 (mysqld)CGroup: /system.slice/mysqld.service└─21810 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pidNov 24 18:04:57 server systemd[1]: Starting MySQL Server...
Nov 24 18:05:02 server systemd[1]: Started MySQL Server.

8.本地连接mysql

[root@server yum.repos.d]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.40 MySQL Community Server (GPL)Copyright (c) 2000, 2022, 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> 

五、mysql客户端远程连接mysql容器服务


[root@server ~]# mysql -h 192.168.3.166 -P3666 -uroot -padmin
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 4
Server version: 5.7.36 MySQL Community Server (GPL)Copyright (c) 2000, 2018, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| data               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.12 sec)mysql> 

相关内容

热门资讯

原创 去... 走进面包店,阵阵麦香与奶油的甜香交织,让人垂涎欲滴。琳琅满目的面包,金黄酥脆,仿佛在向你招手。然而,...
原创 小... 老话说"小暑大暑,上蒸下煮",这节骨眼上,吃食最要紧——既得解了这缠人的暑湿,又得顶得住苦夏的消耗。...
好看又好吃到爆!广西果酱烧烤别... 在中国烧烤的版图上,广西果酱烧烤绝对是一道独特的风味。它不像东北烧烤那样豪迈粗犷,也不似川渝烧烤那般...
这几道面料理也太好吃了!哪怕是... 平时就爱吃面? 那下面这几道面料理 你可赶紧学起来~ 简单又好吃! 鱼香肉丝拌面 煮至汤汁浓稠关火...
天价面包刺客频频上热搜,幕后推... 胖东来面包“一包难求”,盒马因“面包刺客”话题冲上热搜——这波烘焙热潮的幕后推手,“酸面团”功不可没...
李煜词的鉴赏,越多越好 李煜词的鉴赏,越多越好楼主上高中?没买《古代诗词鉴赏》?
世界上所有的域名和对应的ip数... 世界上所有的域名和对应的ip数据有多大?就是说能不能用一台机器储存所有的DNS数据?那要看你的硬盘够...
形容很有耐心的成语 形容很有耐心的成语形容很有耐心的成语持之以恒、锲而不舍、百折不挠、坚韧不拔、循循善诱。1、持之以恒(...
刀枪不入古代电影,内容是白胡子... 刀枪不入古代电影,内容是白胡子老头是个刀枪不入的坏人,最后被英雄用手抓气门才打败的,求解是什么电?好...
谁知道一个男孩等女孩的故事 谁知道一个男孩等女孩的故事故事内容大概是一个男孩等一个女孩。经过了几世轮回。男孩变成树啊。蝴蝶啊什么...
孩子满月酒 孩子满月酒宝宝是八月初三出生,九月初五这天摆满月酒,日子好吗?... 宝宝是八月初三出生,九月初五...
全球军火市场由三部分组成,除了... 全球军火市场由三部分组成,除了合法和黑市,还有什么?还有地下交易。因为地下交易非常的隐秘,所以这种交...
足球进球数对应的叫法? 足球进球数对应的叫法?进一个球叫独中一元。进一个球叫独中一元。1、进一个球叫独中一元。2、进两个球叫...
故事中范闲人物形象特征+事件梗... 故事中范闲人物形象特征+事件梗概?一,人生而平等一次和若若小姐偷跑出去游玩,连累自己院中的婢女被管家...
父母在家里怎样培养4-5岁的孩... 父母在家里怎样培养4-5岁的孩子画画 1.买些儿童绘画的书。2.父母引导性的启发孩子的创造力和表...
有趣的故事英语怎么说 有趣的故事英语怎么说interesting things
米哈游一般什么人喜欢玩 米哈游一般什么人喜欢玩 一般20岁左右的年轻人喜欢玩,因为米哈游的产品多为二次元品类,并不是大众...
我说对不起的图片 我说对不起的图片
关于忍冬花的花语和诗歌 关于忍冬花的花语和诗歌忍冬被诗人切.米沃什运用在诗歌当中,觉得有一种漫不经心却又含似飞翔的诗意,希望...
好看的校园豪门小说 好看的校园豪门小说怎么发给你?何以萧笙墨。。老婆,吻你上瘾。 微微一笑很倾城