上節回顧:docker(部署常見應用):docker部署nginxhtml
# 下載鏡像 docker pull mysql:5.7.26 # 查看鏡像 docker images|grep mysql # 啓動容器鏡像,建議在/usr/local/workspace/mysql 下執行如下docker run 命令
docker run -p 13306:3306 --name my-mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26mysql
# 建議寫死路徑nginx
docker run -p 13306:3306 --name my-mysql -v /usr/local/workspace/mysql/conf:/etc/mysql/conf.d -v /usr/local/workspace/mysql/logs:/logs -v /usr/local/workspace/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26sql
-p 13306:3306 將容器的 3306 端口映射到主機的 3306 端口docker
--name my-mysql 啓動後容器名爲 my-mysql 數據庫
-v $PWD/conf:/etc/mysql/conf.d 將主機當前目錄下的 conf/my.cnf 掛載到容器的 /etc/mysql/my.cnf (conf目錄爲mysql的配置文件)bash
-v $PWD/logs:/logs 將主機當前目錄下的 logs 目錄掛載到容器的 /logs (logs目錄爲mysql的日誌目錄)服務器
-v $PWD/data:/var/lib/mysql 將主機當前目錄下的data目錄掛載到容器的 /var/lib/mysql (data目錄爲mysql配置的數據文件存放路徑)網絡
-e MYSQL_ROOT_PASSWORD=123456 初始化 root 用戶的密碼session
[xxx@xxx-xx-xxx mysql]# docker ps|grep mysql 5291ed3fe987 mysql:5.7.26 "docker-entrypoint.s?? 5 minutes ago Up 5 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp my-mysql
# 登陸容器
[root@cbov10-sso55-xxx ~]# docker exec -it my-mysql bash root@5291ed3fe987:/# ls bin dev entrypoint.sh home lib64 media opt root sbin sys usr boot docker-entrypoint-initdb.d etc lib logs mnt proc run srv tmp var
# 登陸mysql
root@5291ed3fe987:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select host,user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | root | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | | localhost | test | +-----------+---------------+ 5 rows in set (0.00 sec) # 設置root用戶在任何地方進行遠程登陸,並具備全部庫任何操做權限,(公司絕對不能這麼作,暴露的攻擊面太大),這裏只是作測試。 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) # 刷新權限 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) # 退出mysql mysql> exit Bye
關於mysql受權,各位可自行百度,這裏推薦
使用數據庫鏈接工具鏈接 宿主機ip+宿主機暴露的端口。
前面咱們啓動容器的時候 -p 13306:3306 標識 宿主機端口 13306 映射 到容器的 3306 ,假如咱們宿主機,就是服務器的ip地址爲10.10.10.11
應該鏈接 10.10.10.11:13306 鏈接的數據庫這裏推薦新建test 數據庫 作測試,以下
# 查看容器 [root@cbov10-sso55-113 mysql]# docker ps|grep mysql 5291ed3fe987 mysql:5.7.26 "docker-entrypoint.s?? 4 hours ago Up 4 hours 33060/tcp, 0.0.0.0:13306->3306/tcp my-mysql # 中止容器 ( 5291ed3fe987 這裏是mysql容器id) [root@cbov10-sso55-113 mysql]# docker stop 5291ed3fe987 5291ed3fe987 # 刪除容器 [root@cbov10-sso55-113 mysql]# docker rm 5291ed3fe987 5291ed3fe987
去咱們原先掛載目錄下查看
掛載宿主機目錄是 /usr/local/workspace/mysql,
[root@cbov10-sso55-xxx mysql]# cd data/ [root@cbov10-sso55-xxx data]# ls auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 performance_schema public_key.pem server-key.pem test ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 mysql private_key.pem server-cert.pem sys
數據文件還在!咱們再從新執行
# 這裏要注意和掛載的宿主機目錄必定要一致,第一次在 /usr/local/workspace/mysql 下執行的命令,此次也應該在同目錄 # 固然,寫成固定路徑就沒有上面的問題 [root@cbov10-sso55-xxx mysql]# docker run -p 13306:3306 --name my-mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26 74c91431533ebb9bbfd3a1123b3f910f54770a08ad08c3c37cbbb996d29e0428 # 這裏能夠看出容器id已經發生了變化 [root@cbov10-sso55-xxx mysql]# docker ps |grep mysql 74c91431533e mysql:5.7.26 "docker-entrypoint.s?? 16 seconds ago Up 15 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp my-mysql # 進入容器 [root@cbov10-sso55-xxx mysql]# docker exec -it bash 74c91431533e Error: No such container: bash [root@cbov10-sso55-xxx mysql]# docker exec -it 74c91431533e bash root@74c91431533e:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)
發現建的test數據庫也在!mysql容器刪除先後,data文件大小也能夠驗證,讀者殼自行嘗試。
宿主機文件保存好的話,數據能夠不丟失。
其實 生產比作的這個測試要複雜的多,mysql集羣,主備,數據同步,網絡 等等問題,用docker解決確實爲難
mysql 容器 的管理或者說,有狀態應用的管理還得一個比較流弊的東西,這個項目是 大名鼎鼎的 kubernetes。
推薦
感謝您的觀看和支持。