本文记录docker和docker-compose两种方式安装使用mysql。从简单快速使用入手,再根据实际场景不断完善更符合开发/生产环境的配置实践。
docker pull mysql:5.7
docker run -itd --name mysql_01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5fa2b58a0e8 mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql_01
[root@docker ~]# docker logs mysql_01
[root@localhost ~]# docker exec -it mysql_01 bash
mysql -u root -p
mysql -u root -p
root@d357051fa903:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
CREATE DATABASE `demo_01` default CHARACTER set utf8 COLLATE utf8_general_ci;
demo_01
— 代表的是创建数据库 demo_01。utf8_general_ci 不区分大小写,这个你在注册用户名和邮箱的时候就要使用。
utf8_general_cs 区分大小写,如果用户名和邮箱用这个 就会照成不良后果
utf8_bin:字符串每个字符串用二进制数据编译存储。 区分大小写,而且可以存二进制的内容
查看数据库创建信息
show crate database demo_01;
drop database demo_01;
安装docker-compose
version: "2"
services:
mysql:
container_name: mysql_simple
image: mysql:5.7
#env_file:
#- ../env/mysql.env
environment:
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_USER: 'test'
MYSQL_PASS: 'test'
volumes:
- ./mysql:/var/lib/mysql
ports:
- "3306:3306"
docker-compose -f mysql_simple.yaml up
docker-compose -f mysql_simple.yaml down
一个应用服务一般连接, docker的mysql容器对应的业务数据库, 并且使用专用账号密码。如果mysql容器创建时候,同时完成数据库初始化和账号创建。这样在一个新环境,快速部署一套服务时候,可以进一般减少手动操作的环节。
vi mysql_init_db.yaml
version: "2"
services:
mysql:
container_name: mysql_init_db
image: mysql:5.7
#env_file:
#- ../env/mysql.env
environment:
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_USER: 'test'
MYSQL_PASS: 'test'
volumes:
- ./mysql:/var/lib/mysql
- ./init:/docker-entrypoint-initdb.d/ # sql初始化目录
ports:
- "3306:3306"
mkdir -p init
vi /init/db_demo_01.sql
create database db_demo_01 default charset 'utf8';
use db_demo_01;
create table user
(
id int auto_increment primary key,
username varchar(64) unique not null,
email varchar(120) unique not null
);
insert into user values(1, "zhangsan","[email protected]");
insert into user values(2, "lisi","[email protected]");
docker-compose -f mysql_init_db.yaml up
docker exec -it mysql_init_db bash
mysql -u root -p
> grant all on db_demo_01.* to 'test'@'%' identified by 'test';
> flush privileges;
> quit
注意./init中sql脚本只会首次运行创建./mysql,执行一次。
通过上面的快速启动版, 已经实现了创建容器自带业务库和账号信息,但是授权操作还是过于麻烦,账号授权其实可以和创建数据库同时进行。
vi /init/db_demo_01.sql
优化创建脚本补充授权信息
create database db_demo_01 default charset 'utf8';
grant all on db_demo_01.* to 'test'@'%' identified by 'test';
flush privileges;
use db_demo_01;
create table user
(
id int auto_increment primary key,
username varchar(64) unique not null,
email varchar(120) unique not null
);
insert into user values(1, "zhangsan","[email protected]");
insert into user values(2, "lisi","[email protected]");
这样可以为不同的微服务,初始化不同数据库环境。
上面场景更多适合快速创建成熟的产品的数据环境或者是演示库,如果在开发阶段,数据库设计经常需要变动, 推荐使用liquibase通过版本化来管理数据库脚本。SpringBoot_liquibase使用