目录
1. 准备yml文件
其中MYSQL_ROOT_PASSWORD这里设定的是默认root账号的密码:
version: '3'
services:
mysql:
restart: always
image: mysql:5.7
container_name: mysql5_7
volumes:
- /work/mysql/mysql5/mydir:/mydir
- /work/mysql/mysql5/datadir:/var/lib/mysql
- /work/mysql/mysql5/conf/my.cnf:/etc/my.cnf
# 数据库还原目录 可将需要还原的sql文件放在这里
- /work/mysql/mysql5/source:/docker-entrypoint-initdb.d
environment:
- "MYSQL_ROOT_PASSWORD=root"
- "MYSQL_DATABASE=dataxweb_test"
- "TZ=Asia/Shanghai"
ports:
# 使用宿主机的3306端口映射到容器的3306端口
# 宿主机:容器
- 3307:3306
2. 准备配置mysql的配置文件
创建目录 mkdir -p /work/mysql/mysql5/{mydir,datadir,conf,source}
编写数据库配置文件:
vi /work/mysql/mysql5/conf/my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
character-set-client-handshake=FALSE
collation-server=utf8_unicode_ci
init_connect='SET NAMES utf8'
lower_case_table_names = 1
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
3. 启动mysql容器
docker-compose -f docker-mysql5.7.yml up -d
在宿主机上访问容器中的mysql
mysql -h vm01 -u root -P 3307 -p
密码为yml中指定的root
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dataxweb_test |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)