Docker-compose搭建Mysql8.0(并开启binlog)

Docker-compose搭建Mysql8.0

1.目录结构

.
└── docker_mysql_8_0
	├── docker-compose.yml
    ├── conf
    │   └── my.cnf
    ├── logs
    └── db

2. docker-compose.yml

注意:该编排mysql端口使用的宿主机33106端口,可根据自己需求修改配置

version: '3.5'
services:
  mysql:
    image: mysql:8.0
    restart: always
    privileged: True
    container_name: mysqlServer33106
    volumes:
      - ./db:/var/lib/mysql
      - ./conf:/etc/mysql/conf.d
      - ./logs:/logs
    command:
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    environment:
      MYSQL_ROOT_PASSWORD: "Abc123654"
      MYSQL_USER: "test"
      MYSQL_PASSWORD: "Abc123654"
      MYSQL_INITDB_SKIP_TZINFO: "Asia/Shanghai"
    ports:
      - "33106:3306"
    network_mode: "bridge"

3.my.cnf

###### [mysql]配置模块 ######
[mysql]
# 设置MySQL客户端默认字符集
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.sock
###### [mysqld]配置模块 ######
[mysqld]
port=3306
user=mysql
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# MySQL8 的密码认证插件
default_authentication_plugin=mysql_native_password
# 禁用符号链接以防止各种安全风险
symbolic-links=0
# 允许最大连接数
max_connections=1000
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 表名存储在磁盘是小写的,但是比较的时候是不区分大小写
lower_case_table_names=0
max_allowed_packet=16M
# 设置时区
default-time_zone='+8:00'
# binlog 配置
binlog_format="ROW"
log-bin = /var/lib/mysql/mysql-bin
expire-logs-days = 90
max-binlog-size = 500M
# server-id 配置
server-id = 1
###### [client]配置模块 ######
[client]
default-character-set=utf8mb4

4. 按照上述创建文件夹和文件

如图:
Docker-compose搭建Mysql8.0(并开启binlog)_第1张图片

5.启动服务

# 进入docker_mysql_8_0目录下
cd /系统目录/docker_mysql_8_0
# 启动服务
docker compose up -d

备注: Mysql常用命令

# 创建新用户
create user 'username'@'host' identified by 'password'; 

# 为用户授权
grant all privileges on *.* to 'username'@'%' with grant option; 

# 授权之后刷新权限
flush privileges; 

# 收回权限(不包含赋权权限)
REVOKE ALL PRIVILEGES ON *.* FROM user_name;
REVOKE ALL PRIVILEGES ON user_name.* FROM user_name;
# 收回赋权权限
REVOKE GRANT OPTION ON *.* FROM user_name;

# 操作完后重新刷新权限
flush privileges;

你可能感兴趣的:(Docker系列,MySQL系列,docker,mysql,数据库)