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

云原生之使用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> 

相关内容

热门资讯

小雪降温,别错过这几道滋补菜,... 小雪降温,别错过这几道滋补菜,孩子一吃就两碗,御寒强免疫 小雪时节天气渐冷,下面分享几道滋补家常菜,...
原创 它... 在探讨美食的海洋中,有一种食物以其独特的魅力,成为了众多男性心中的挚爱。它不仅满足了味蕾的极致享受,...
原创 它... 标题:快餐文化下的童年记忆 在快节奏的现代生活中,快餐以其便捷、快速的特点成为了人们餐桌上的常客。...
从“蟹季限定”到“四季常热” 味觉公路 度假区(阳澄湖镇)供图 □ 本报记者 陈诚 王俊杰 入冬后,阳澄湖畔有了阵阵凉意,苏州“村...
佳木斯市再添新景 四丰山水库打... 本文转自:人民网-黑龙江频道人民网哈尔滨11月19日电 近日,佳木斯四丰山水库再添新景——沿岸休闲步...