目录
一.新建一个启动服务的目录
mkdir /usr/local/docker/mysql
cd /usr/local/docker/mysql
二.新建文件docker-compose.yml
注意:文件名字必需是docker-compose.yml
version: '3.8'
services:
mysql:
container_name: mysql57
image: mysql:5.7.31
cpus: "2"
mem_limit: "2G"
restart: always
ports:
- 3307:3306
privileged: true
volumes:
- $PWD/mysql57/log:/var/log/mysql
- $PWD/mysql57/conf/my.cnf:/etc/mysql/my.cnf
- $PWD/mysql57/data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_USER: 'haima'
MYSQL_PASS: '123456'
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_general_ci',
'--max_connections=3000'
]
networks:
- myweb
networks:
myweb:
driver: bridge
三.新建角本文件 init-mysql.sh
#!/bin/bash
mkdir -p $PWD/mysql57/{conf,data,log} #创建本地文件夹
#新建配置文件
tee $PWD/mysql57/conf/my.cnf<<-'EOF'
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
lower_case_table_names=1 #实现mysql不区分大小(开发需求,建议开启)
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
default-time_zone = '+8:00'
# 更改字符集 如果想Mysql在后续的操作中文不出现乱码,则需要修改配置文件内容
symbolic-links=0
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
EOF
#启动服务
docker-compose up -d
四.启动服务
chmod +x init-mysql.sh
./init-mysql.sh
此时服务已经启动成功了.使用角本是不是很爽,嘿嘿...
其它操作
docker ps -a #查看启动的服务
docker-compose -h #帮助命令
docker-compose down #停止并删除服务
docker-compose restart #重启服务
docker-compose stop #停止服务
docker-compose start #停止服务
docker-compose logs #停止日志