前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker安装MySQL8数据库

Docker安装MySQL8数据库

作者头像
互联网-小阿宇
发布2023-03-06 16:13:23
2.6K0
发布2023-03-06 16:13:23
举报
文章被收录于专栏:互联网-小阿宇互联网-小阿宇

🎈 作者:互联网-小啊宇 🎈 简介: CSDN 运维领域创作者、阿里云专家博主。目前从事 Kubernetes运维相关工作,擅长Linux系统运维、开源监控软件维护、Kubernetes容器技术、CI/CD持续集成、自动化运维、开源软件部署维护等领域。 🎈 博客首页:CSDN【互联网-小阿宇】阿里云【互联网-小阿宇】

在这里插入图片描述
在这里插入图片描述

Docker安装MySQL8数据库

关闭防火墙、沙盒、清空iptables

代码语言:javascript
复制
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
vi /etc/selinux/config
#修改
SELINUX=disabled
iptables -F && iptables-save

Centos7安装Docker

默认安装的最新版docker

代码语言:javascript
复制
yum -y install yum-utils
yum-config-manager     --add-repo     https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo     
yum -y install docker-ce
[root@mysql8 ~]# systemctl start docker && systemctl enable docker
[root@mysql8 ~]# docker --version
Docker version 23.0.0, build e92dd87

下载MySQL8镜像

代码语言:javascript
复制
[root@mysql8 ~]# docker pull mysql:8.0.27  #需要其他版本就更改版本号

启动MySQL8测试容器

如果没有挂载数据目录、配置文件的需求,下面的启动命令即可满足使用

代码语言:javascript
复制
[root@mysql8 config]# docker run -itd --name mysqltest -p 3366:3306 --privileged=true -e MYSQL_ROOT_PASSWORD=EBXXXXXXXXX7Ai mysql:8.0.27
1c4ac77d18b92b6956782f8a2dabe145ec104dfa6827fbd6f275020fca03ba3c
[root@mysql8 config]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
1c4ac77d18b9   mysql:8.0.27   "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   33060/tcp, 0.0.0.0:3366->3306/tcp, :::3366->3306/tcp   mysqltest

如果不需要把数据目录和配置文件映射到主机,到这里就已经启动完毕了。

创建MySQL数据目录、配置文件目录

代码语言:javascript
复制
[root@mysql8 ~]# mkdir -p /data/mysql/{config,data}

 - /data/mysql/data      #数据目录
 - /data/mysql/config    #配置文件目录

MySQL容器配置文件拷贝到宿主机

在刚才运行的测试容器中把配置文件拷贝到宿主机,下一步映射会用到

my.cnf是MySQL的默认配置文件,可以在此文件内进行修改添加MySQL相关参数

代码语言:javascript
复制
[root@mysql8 ~]# cd /data/mysql/config
[root@mysql8 config]# pwd
/data/mysql/config
[root@mysql8 config]# docker cp mysqltest:/etc/mysql/my.cnf ./
Preparing to copy...
Successfully copied 3.072kB to /data/mysql/config/./
[root@mysql8 config]# ls
my.cnf
[root@mysql8 config]# cat my.cnf 
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/

删除测试容器

代码语言:javascript
复制
[root@mysql8 config]# docker stop mysqltest
mysqltest
[root@mysql8 config]# docker rm mysqltest
mysqltest

启动MySQL8容器并挂载数据目录、配置文件

代码语言:javascript
复制
[root@mysql8 ~]# docker run -itd --name mysql8 -p 3306:3306 --privileged=true --restart=always -v  /data/mysql/data:/var/lib/mysql -v /data/mysql/config/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=EBXXXXXXX7Ai mysql:8.0.27
8420dd0180c0a76d6179cebb9e746b6c6d47d2cd3ff85278a30c6e2138e02f0d
[root@mysql8 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
8420dd0180c0   mysql:8.0.27   "docker-entrypoint.s…"   3 minutes ago   Up 2 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql8
[root@mysql8 ~]# cd /data/mysql/
[root@mysql8 mysql]# ls
config  data
[root@mysql8 mysql]# cd config/
[root@mysql8 config]# cd ../data/
[root@mysql8 data]# ls
8420dd0180c0.err  binlog.index     client-key.pem     ibdata1      #innodb_temp        private_key.pem  sys
auto.cnf          ca-key.pem       #ib_16384_0.dblwr  ib_logfile0  mysql               public_key.pem   undo_001
binlog.000001     ca.pem           #ib_16384_1.dblwr  ib_logfile1  mysql.ibd           server-cert.pem  undo_002
binlog.000002     client-cert.pem  ib_buffer_pool     ibtmp1       performance_schema  server-key.pem

MySQL8容器数据库测试

代码语言:javascript
复制
[root@mysql8 ~]# docker exec -it mysql8 bash
root@8420dd0180c0:/# mysql -uroot -pEBXXXXX27Ai
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.27 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.01 sec)

mysql> select 0;
+---+
| 0 |
+---+
| 0 |
+---+
1 row in set (0.00 sec)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-02-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Docker安装MySQL8数据库
  • 关闭防火墙、沙盒、清空iptables
  • Centos7安装Docker
  • 下载MySQL8镜像
  • 启动MySQL8测试容器
  • 创建MySQL数据目录、配置文件目录
  • MySQL容器配置文件拷贝到宿主机
  • 删除测试容器
  • 启动MySQL8容器并挂载数据目录、配置文件
  • MySQL8容器数据库测试
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档