背景:
現如今不管什么服務和應用基本都可以在docker里跑一跑了,但是在我個人的印象中,像數據庫這種比較重要大型且數據容易受傷的應用是不適合在docker里跑的。但是也有很多人嘗試在docker中跑mysql等數據庫,所以也試著嘗試一下。(好吧,重點是領導喜歡~~)
獲取鏡像:
mysql的鏡像可以自己用dockerfile制作一個,或者直接到官方的docker鏡像庫中下載,本文用的是官方鏡像。
# docker pull mysql# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/mysql latest d9124e6c552f 12 days ago 383.4 MB
運行容器:
1:正常運行。
啟動容器:
# docker run --name cmh-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d docker.io/mysql
進入容器:
# docker-enter cmh-mysql
進入mysql:
root@3a2b8ab0d971:~# mysql -u root -pmy-secret-pwmysql: [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 2Server version: 5.7.16 MySQL Community Server (GPL)Copyright (c) 2000, 2016,Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.mysql>
以上就創建了一個mysql的docker容器,可以看到版本為5.7.16。但是這樣創建的容器有兩個問題,一是容器刪除后,數據就丟失了,二是要訪問數據庫,必須進入到容器里面才可以。
2:持久化數據,映射開放mysql端口
創建宿主機數據存放目錄:
# mkdir -p /opt/data/mysql
啟動容器:
# docker run --name cmh-mysql -v /opt/data/mysql/:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d docker.io/mysql
c38f50a540ff4d5ecf1a5ec49fb721335a8e1b79dec58229cf5e00553f988e44
查看容器:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c38f50a540ff docker.io/mysql "docker-entrypoint.sh" 9 seconds ago Up 8 seconds 0.0.0.0:3306->3306/tcp cmh-mysql
查看端口:
# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
新聞熱點
疑難解答
圖片精選