Docker-compose部署MySQL

 

 

目录结构

/Mysql             # Mysql应用文件夹
  docker-compose.yml      # docker compose脚本文件
  init.sql                  # mysql初始sql文件
 /conf            # 配置文件夹
   my.cnf          # 配置文件          
  /db             # 数据库文件         

docker-compose.yml

version: '3'
services:
    mysql:
        network_mode: "bridge"
        environment:
            MYSQL_ROOT_PASSWORD: "yourpassword"
            MYSQL_USER: 'test'
            MYSQL_PASS: 'yourpassword'
        image: "mysql:5.5"
        restart: always
        volumes:
            - "./db:/var/lib/mysql"
            - "./conf/my.cnf:/etc/my.cnf"
            - "./init:/docker-entrypoint-initdb.d/"
        ports:
            - "3306:3306"

my.cnf

[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

init.sql

create database test;
use test;
create table user
(
id int auto_increment primary key,
username varchar(64) unique not null,
email varchar(120) unique not null,
password_hash varchar(128) not null,
avatar varchar(128) not null
);
insert into user values(1, "zhangsan","test12345@qq.com","passwd","avaterpath");
insert into user values(2, "lisi","12345test@qq.com","passwd","avaterpath");

运行

docker-compose up -d

 

posted @ 2021-01-04 22:43  -零  阅读(5952)  评论(0编辑  收藏  举报