用docker建立以及連接MySQL
===
先建立一個網路
```
docker network create lamp
```
建立一個MySQL Server的container
```
docker run -d --name mydb -e MYSQL_ROOT_PASSWORD=mypassword --network lamp mysql
```
用docker的`mysql`指令而不是本機安裝來連入這個MySQL Server
```
docker run -it --rm --network lamp mysql mysql -hmydb -uroot -p
```
```
vagrant@ubuntu-xenial:~$ docker run -it --rm --network lamp mysql mysql -hmydb -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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>
```
獲得container的IP
```
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
```
建立一個MySQL Server的container,然後在建立時同時就建立一個volume叫`dbstore`,會指定mysql的資料存放處到這個volume中。
```
docker run -d --name mydb --network lamp -v dbstore:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mypassword mysql
```
利用container把這個volume備份到`backup.tar.gz`
```
docker run --rm --volumes-from mydb -v $(pwd):/backup ubuntu tar -zcvf /backup/backup.tar.gz /var/lib/mysql
```