docker安装mysql

1. 获取mysql镜像

docker search mysql 
docker pull msyql:5.7

mkdir -p /docker/mysql
cd /docker/mysql

2. 配置启动命令

cat <<EOF > start.sh
#!/bin/bash
docker run \
--name mysql -p 3306:3306 \
-v /docker/mysql/conf:/etc/mysql/conf.d \
-v /docker/mysql/logs:/logs \
-v /docker/mysql/data:/var/lib/mysql \
-–privileged=true \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
EOF

3. 设置访问权限

docker exec -it mysql /bin/bash
mysql -uroot -p
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION
FLUSH PRIVILEGES

docker-compose 安装mysql

1.  安装docker-compose

curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

2. 编写docker-compose.yml
cd /docker/mysql

cat <<EOF > docker-compose.yml

version: '3.0'

services:
  mysql57:
    container_name: mysql57
    image: mysql:5.7
    #command:
    #build:
    # context: .
    # dockerfile: Dockerfile
    ports:
      - "3306:3306"
    privileged: true
    restart: always
    networks:
      - mybridge
    volumes:
      - data:/var/lib/mysql
      - "./conf:/etc/mysql/conf.d"
      - "./logs:/logs"
    environment:
      MYSQL_DATABASE: "test"
      MYSQL_ROOT_PASSWORD: "123456"
    logging:
      driver: "json-file"
      options:
        max-size: "5g"

networks:
  mybridge:
    driver: bridge

volumes:
  data:

EOF

3. 执行docker-compose

docker-compose up -d

 

备份数据库

[root@docker mysql]# docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p"123456" '> /mysql/all_databases.sql
posted @ 2019-03-29 17:48  Ray_chen  阅读(282)  评论(0编辑  收藏  举报