docker快速搭建數據庫測試環境

背景: 不僅僅是開發人員,對於DBA來說,重複的安裝部署也是煩的一筆。還好有docker這樣的神器,免去了很多安裝部署的煩惱。
三條命令快速安裝mysql數據庫:
前提是安裝好docker並且把docker hub配置爲國內阿里雲。參考:https://blog.csdn.net/u010033674/article/details/105345211
1)、搜索mysql官方鏡像

docker search mysql

在這裏插入圖片描述
2)、獲取對應版本鏡像

docker pull mysql:8.0.19

版本可以自己指定,也是可以mysql:5.7.25
在這裏插入圖片描述
3)、啓動mysql容器

docker run --name mysql8.0.19  --restart=always -p 13306:3306 -e MYSQL_ROOT_PASSWORD=1234.C0m -d mysql:8.0.19

解釋:–name mysql8.0.19 #指定容器名字爲mysql8.0.19
–restart=always #容器策略,失敗後就重啓
-p 13306:3306 #容器的端口3306映射到宿主機端口13306
-d mysql:8.0.19 #容器後臺運行,鏡像是剛纔pull下來的mysql:8.0.19
4)、訪問mysql數據庫
宿主機訪問:

[root@onetest ~]# mysql -h127.0.0.1 -uroot -p1234.C0m -P13306
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 8
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> 

容器內訪問:

[root@onetest ~]# docker exec -it mysql8.0.19 bash
root@bc97c1c45972:/# mysql -uroot -p1234.C0m
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 9
Server version: 8.0.19 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> 

附錄:搭建自定義配置的mysql容器

docker run -itd --name mysql01 \
--hostname mysql01 \
--network mynet \
--ip 192.168.0.10 \
--restart=always \
-v /data/mysql01/data:/var/lib/mysql \
-v /data/mysql01/conf:/etc/mysql \
-p 3309:3306 \
-e MYSQL_ROOT_PASSWORD=1234.C0m \
mysql:8.0.20

命令說明:
–name mysql01 #指定容器名
–hostname mysql01 #指定容器主機名
–network mynet #指定自己創建的docker網絡
–ip 192.168.0.10 #從docker網絡中爲容器固定IP
–restart=always #重啓策略
-v /data/mysql01/data:/var/lib/mysql #把mysql 的數據文件目錄映射到宿主機/data/xxx目錄下
-v /data/mysql01/conf:/etc/mysql #同上,把容器中mysql的配置文件映射到宿主機目錄,可以把自己的my.cnf配置文件提前放進宿主機對應目錄下
-p 3309:3306 #映射端口
-e MYSQL_ROOT_PASSWORD=1234.C0m #設置root密碼
mysql:8.0.20 #鏡像名

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章