云原生之使用Docker部署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 - Community
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:05:12 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.17
API version: 1.41 (minimum version 1.12)
Go version: go1.17.11
Git commit: a89b842
Built: Mon Jun 6 23:03:33 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.6
GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc:
Version: 1.1.2
GitCommit: v1.1.2-0-ga916309
docker-init:
Version: 0.19.0
GitCommit: de40ad0
3.检查docker状态
[root@jeven ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: 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 ago
Docs: https://docs.docker.com
Main PID: 11080 (dockerd)
Tasks: 26
Memory: 1.7G
CGroup: /system.slice/docker.service
└─11080 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Nov 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 Resolution
Dependencies Resolved
======================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================
Installing:
mysql57-community-release noarch el7-10 /mysql57-community-release-el7-10.noarch 30 k
Transaction Summary
======================================================================================================================================================
Install 1 Package
Total size: 30 k
Installed size: 30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 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 Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2022-11-24 18:05:02 CST; 29s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 21810 (mysqld)
CGroup: /system.slice/mysqld.service
└─21810 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Nov 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>
转载:https://blog.csdn.net/jks212454/article/details/128314856
查看评论