之前給大家介紹過:docker(部署常見應用):docker部署nginx
docker部署mysql:5.7.26
# 下載鏡像
docker pull mysql:5.7.26
# 查看鏡像
docker images|grep mysql
# 啟動容器鏡像,建議在/usr/local/workspace/mysql 下執(zhí)行以下docker run 命令
docker run -p 13306:3306 --name my-mysql -v $PWD/conf:/etc/mysql -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26
# 建議寫死路徑
docker run -p 13306:3306 --name my-mysql -v /usr/local/workspace/mysql/conf:/etc/mysql -v /usr/local/workspace/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26
命令說明:
-p 13306:3306 將容器的 3306 端口映射到主機的 3306 端口
--name my-mysql 啟動后容器名為my-mysql
-v $PWD/conf:/etc/mysql 將主機當前目錄下的 conf/ 掛載到容器的 /etc/mysql (conf目錄為mysql的配置文件,不掛載也沒問題)
-v $PWD/logs:/logs 將主機當前目錄下的 logs 目錄掛載到容器的 /logs (logs目錄為mysql的日志目錄,不掛載也沒影響)
-v $PWD/data:/var/lib/mysql 將主機當前目錄下的data目錄掛載到容器的 /var/lib/mysql (data目錄為mysql配置的數(shù)據(jù)文件存放路徑,這個還是建議掛載,是存儲數(shù)據(jù)的,容器down掉,還能再次掛載數(shù)據(jù)。)
-e MYSQL_ROOT_PASSWORD=123456 初始化 root 用戶的密碼
查看容器啟動情況
[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
進入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
# 登錄mysqlroot@5291ed3fe987:/# mysql -uroot -p --default-character-set=utf8
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
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授權,各位可自行百度,這里推薦
mysql之權限管理
Mysql權限控制
遠程登錄測試
使用數(shù)據(jù)庫連接工具連接 宿主機ip+宿主機暴露的端口。
前面我們啟動容器的時候-p13306:3306 標識宿主機端口 13306 映射 到容器的 3306 ,假如我們宿主機,就是服務器的ip地址為10.10.10.11
應該連接 10.10.10.11:13306 連接的數(shù)據(jù)庫這里推薦新建test 數(shù)據(jù)庫 做測試,如下
docker ,mysql重啟問題(數(shù)據(jù)會不會丟失?)
# 查看容器
[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
數(shù)據(jù)文件還在!我們再重新執(zhí)行
# 這里要注意和掛載的宿主機目錄一定要一致,第一次在 /usr/local/workspace/mysql 下執(zhí)行的命令,這次也應該在同目錄
# 當然,寫成固定路徑就沒有上面的問題
[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已經(jīng)發(fā)生了變化
[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)
發(fā)現(xiàn)建的test數(shù)據(jù)庫也在!mysql容器刪除前后,data文件大小也可以驗證,讀者殼自行嘗試。
宿主機文件保存好的話,數(shù)據(jù)可以不丟失。
說明:
其實 生產(chǎn)比做的這個測試要復雜的多,mysql集群,主備,數(shù)據(jù)同步,網(wǎng)絡 等等問題,用docker解決確實為難
mysql 容器 的管理或者說,有狀態(tài)應用的管理還得一個比較流弊的東西,這個項目是 大名鼎鼎的kubernetes。
推薦
docker官網(wǎng)
kubernetes官網(wǎng)
到此這篇關于docker 部署mysql(docker部署常見應用)的文章就介紹到這了,更多相關docker 部署mysql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 遠程連接mysql錯誤代碼1130的解決方法
- mysql error 1130 hy000:Host''localhost''解決方案
- MySQL連接異常報10061錯誤問題解決
- mac安裝mysql數(shù)據(jù)庫及配置環(huán)境變量的圖文教程
- 基于JavaSwing+mysql開發(fā)一個學生社團管理系統(tǒng)設計和實現(xiàn)
- MySQL/MariaDB中如何支持全部的Unicode
- Linux下MySQL多實例部署及安裝指南
- MySQL 1130異常,無法遠程登錄解決方案詳解