Saki's 研究记录

docker-compose部署MySQL

字数统计: 707阅读时长: 3 min
2021/11/22

背景

最近由于项目测试需要使用到DB,随便找台机器搭个需要密码登录的单机MySQL,并记录下流程XD。

零. 部署

环境

机型: 1核 1GB 1Mbps 腾讯云服务器
系统: CentOS Linux release 8.4.2105 (Core)

目录

在机器上喜欢的目录(/data,/opt…)执行以下命令:

1
mkdir -p mysql/{mydir,datadir,conf};cd mysql

配置

docekr-compose.yml

创建一个vim docekr-compose.yml文件,并将下面配置全部拷贝进去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
version: "3"

services:
local-mysql:
image: mysql:8.0.21 # 镜像
container_name: mysql_compose # 容器名
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci # 设置utf8字符集
restart: always # 总是重启
ports:
- "13306:3306" # host物理直接映射端口为13306
volumes:
# 文件挂载
- $PWD/mydir:/mydir
# 挂载目录
- $PWD/datadir:/var/lib/mysql
# 挂载 my.cnf 配置文件
- $PWD/conf/my.cnf:/etc/mysql/my.cnf
environment:
# 设置默认数据库
- "MYSQL_DATABASE=mysql" # 初始化启动时要创建的数据库的名称
# 设置密码
- "MYSQL_ROOT_PASSWORD=123456" # root管理员用户密码
# 设置时区
- "TZ=Asia/Shanghai"
networks:
- appnet

networks:
appnet:

my.cnf

创建vim ./conf/my.cnf, 并将下面配置全部拷贝进去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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
# 不使用密码
# skip-grant-tables
# 设置FILE权限,限制mysqld导入/导出
secure-file-priv=NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
# 注意,以下为额外配置按需求进行增删, 20211130
# 设置权限插件
default_authentication_plugin=mysql_native_password
# 设置最大连接数
max_connections=200
# 允许连接失败的次数,防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 创建新表示将使用默认的存储引擎
default-storage-engine=INNODB
# 服务端默认使用字符集默认为utf-8
character-set-server=utf8
# 设置端口
port=3306

壹.运行

启动容器

mysql文件夹运行

1
docker-compose up

或者后台启动

1
docker-compose up -d

查看运行结果

1
2
3
4
docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------------
mysql_compose docker-entrypoint.sh mysql ... Up 0.0.0.0:13306->3306/tcp,:::13306->3306/tcp, 33060/tcp

关闭并删除容器

1
docker-compose down

贰.验证

本地(Local)验证

1
2
3
docker exec -it mysql_compose /bin/bash
mysql -uroot -p
Enter password:

或者

1
docker exec -it mysql_compose mysql -uroot -P13306 -p123456

远程(Remote)验证

1
mysql -h{SERVER_IP} -P13306 -uroot -p123456

叁.总结

简单又方便。

参考:docker-compose 搭建mysql数据库

Done.

CATALOG
  1. 1. 背景
  2. 2. 零. 部署
    1. 2.1. 环境
    2. 2.2. 目录
    3. 2.3. 配置
      1. 2.3.1. docekr-compose.yml
      2. 2.3.2. my.cnf
  3. 3. 壹.运行
    1. 3.1. 启动容器
    2. 3.2. 查看运行结果
    3. 3.3. 关闭并删除容器
  4. 4. 贰.验证
    1. 4.1. 本地(Local)验证
    2. 4.2. 远程(Remote)验证
  5. 5. 叁.总结