Docker安装Mysql8.1.0

Docker安装Mysql8.1.0

准备

Docker版本为24.0.2

提前下载好mysql8镜像,为后面的安装节省时间

docker pull mysql:8.1.0-oracle

创建MySQL安装目录

mkdir -p /opt/mysql8/{data,conf,logs}

创建my.cnf

tee /opt/mysql8/conf/my.cnf <<EOF
###### [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
EOF

创建mysql8.1.0-docker-compose.yml

tee /opt/mysql8/mysql8.1.0-docker-compose.yml <<EOF
version: '3.5'
services:
  mysql:
    image: mysql:8.1.0-oracle
    restart: always
    privileged: True
    container_name: mysql8.1.0
    volumes:
      - ./data:/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: "root"
      MYSQL_USER: "admin"
      MYSQL_PASSWORD: "admin"
      MYSQL_INITDB_SKIP_TZINFO: "Asia/Shanghai"
    ports:
      - "3306:3306"
    network_mode: "bridge"
EOF

当中的端口及账号密码,记得单独修改

安装部署

docker-compose -f /opt/mysql8/mysql8.1.0-docker-compose.yml up -d

创建数据库、创建用户、用户授权

# 创建数据库
create database pay_pro
create database pay_dev

# 创建新用户
# 规则:manager用户拥有pay_pro\pay_dev的所有权限,proread用户只拥有pay_pro库读权限,test用户只拥有pay_dev库所有权限
create user 'manager'@'%' identified by 'manager321';
create user 'proread'@'%' identified by 'proread@com';
create user 'test'@'%' identified by '123456';

# 为用户授权指定库的所有权限
grant all privileges on pay_pro.* to 'manager'@'%' with grant option;
grant all privileges on pay_dev.* to 'manager'@'%' with grant option;
grant all privileges on pay_dev.* to 'test'@'%' with grant option;

# 为用户授权只读权限
grant select on pay_pro.* to proread;

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

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

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

引用 Reference

CentOS8搭建nfs服务
Kubernetes 1.25.4版本安装
kubeasz安装kubernetes1.25.5
k8s一键安装redis单机版
k8s一键安装mysql8单机版
k8s部署springboot应用
Docker安装及学习
Docker制作springboot运行应用镜像
Docker制作Java8环境镜像
Docker安装Mysql5.7.31
Docker安装Mysql8.1.0
Elasticsearch单机版本安装
Elasticsearch集群安装
ELK安装
Docker安装ELK
zookeeper集群安装
Nginx日志切割
RabbitMQ集群安装
springboot集成prometheus+grafana
windows11安装android应用

你可能感兴趣的:(运维,docker,容器,运维,mysql,数据库)