1. 程式人生 > 其它 >docker-compose 安裝 mysql:5.7.31

docker-compose 安裝 mysql:5.7.31

目錄

參考文件:

一.新建一個啟動服務的目錄

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
    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

四.實使化目錄和配置檔案

[root@centos7 mysql]# chmod +x init-mysql.sh docker-compose.yml  #加執行許可權
[root@centos7 mysql]# ./init-mysql.sh

[root@centos7 mysql]# tree ./  #檢視目結構
./
├── docker-compose.yml
├── init-mysql.sh
└── mysql57
    ├── conf
    │ └── my.cnf
    ├── data
    └── log

啟動服務

docker-compose up -d

此時服務已經啟動成功了.使用角本是不是很爽,嘿嘿...

登陸mysql

[root@centos7 mysql57]# docker exec -it mysql57 mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

其它操作

docker ps -a #檢視啟動的服務
docker-compose up -d #後臺啟動服務
docker-compose -h #幫助命令
docker-compose down #停止並刪除服務
docker-compose restart #重啟服務
docker-compose stop #停止服務
docker-compose start #停止服務
docker-compose logs #停止日誌
[Haima的部落格] http://www.cnblogs.com/haima/