🎈 作者:互联网-小啊宇
🎈 简介: CSDN 运维领域创作者、阿里云专家博主。目前从事 Kubernetes运维相关工作,擅长Linux系统运维、开源监控软件维护、Kubernetes容器技术、CI/CD持续集成、自动化运维、开源软件部署维护等领域。
🎈 博客首页:CSDN【互联网-小阿宇】 、阿里云【互联网-小阿宇】
🎈 欢迎小伙伴们点赞👍、收藏⭐、留言💬
Docker安装MySQL8数据库
- 关闭防火墙、沙盒、清空iptables
- Centos7安装Docker
- 下载MySQL8镜像
- 启动MySQL8测试容器
- 创建MySQL数据目录、配置文件目录
- MySQL容器配置文件拷贝到宿主机
- 删除测试容器
- 启动MySQL8容器并挂载数据目录、配置文件
- MySQL8容器数据库测试
关闭防火墙、沙盒、清空iptables
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
vi /etc/selinux/config
#修改
SELINUX=disabled
iptables -F && iptables-save
Centos7安装Docker
默认安装的最新版docker
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镜像
[root@mysql8 ~]# docker pull mysql:8.0.27 #需要其他版本就更改版本号
启动MySQL8测试容器
如果没有挂载数据目录、配置文件的需求,下面的启动命令即可满足使用
[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数据目录、配置文件目录
[root@mysql8 ~]# mkdir -p /data/mysql/{config,data}
- /data/mysql/data #数据目录
- /data/mysql/config #配置文件目录
MySQL容器配置文件拷贝到宿主机
在刚才运行的测试容器中把配置文件拷贝到宿主机,下一步映射会用到
my.cnf是MySQL的默认配置文件,可以在此文件内进行修改添加MySQL相关参数
[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/
删除测试容器
[root@mysql8 config]# docker stop mysqltest
mysqltest
[root@mysql8 config]# docker rm mysqltest
mysqltest
启动MySQL8容器并挂载数据目录、配置文件
[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容器数据库测试
[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)