目录结构
root@localhost mysql # tree
.
├── conf
│ └── my.cnf
├── db
├── docker-compose.yml
└── init
└── init.sql
docker-compose.yml
version: '2'
services:
mysql:
#network_mode: "host" #将直接使用主机端口
environment:
MYSQL_ROOT_PASSWORD: "yourpassword"
MYSQL_USER: 'test'
MYSQL_PASS: 'yourpassword'
image: "docker.io/mysql:latest"
restart: always
volumes:
- "./db:/var/lib/mysql"
- "./conf/my.cnf:/etc/my.cnf"
- "./init:/docker-entrypoint-initdb.d/"
my.cnf
root@localhost conf # cat 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
root@localhost init # cat init.sql
use mysql;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
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","[email protected]","passwd","avaterpath");
insert into user values(2, "lisi","[email protected]","passwd","avaterpath");
启动
root@localhost mysql # docker-compose pull
.......下载镜像过程
root@localhost mysql # docker-compose up -d
mysql_mysql_1_234be9b015e4 is up-to-date
root@localhost mysql #