下載原始的MySQL5.6鏡像:
docker pull mysql:5.6
查看鏡像:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 9e4a20b3bbbc 7 days ago 302MB
啓動MySQL鏡像的實例
docker run -itd --name my_sql_test mysql:5.6 /bin/bash
進入實例:
[root@VM_1_202_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e92cd8ad1356 mysql:5.6 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 3306/tcp my_sql_test
[root@VM_1_202_centos ~]#
[root@VM_1_202_centos ~]# docker attach e92cd8ad1356
啓動MySQL:
root@e92cd8ad1356:/# service mysql status
[info] MySQL Community Server 5.6.48 is not running.
root@e92cd8ad1356:/# service mysql start
......
2020-05-28 07:38:54 144 [Note] InnoDB: 128 rollback segment(s) are active.
2020-05-28 07:38:54 144 [Note] InnoDB: Waiting for purge to start
2020-05-28 07:38:54 144 [Note] InnoDB: 5.6.48 started; log sequence number 1625977
2020-05-28 07:38:54 144 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2020-05-28 07:38:54 144 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2020-05-28 07:38:54 144 [Note] Binlog end
2020-05-28 07:38:54 144 [Note] InnoDB: FTS optimize thread exiting.
2020-05-28 07:38:54 144 [Note] InnoDB: Starting shutdown...
2020-05-28 07:38:56 144 [Note] InnoDB: Shutdown completed; log sequence number 1625987
No directory, logging in with HOME=/
..
[info] MySQL Community Server 5.6.48 is started.
使用MySQL:
root@e92cd8ad1356:/# mysql -uroot -p
Enter password: 【這裏什麼都不輸入,直接回車】
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.48 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
設置MySQL密碼:
set password for root@localhost = password('123456');
下次登錄就需要輸入你設置的密碼:123456 了。
然後對你的MySQL做一些導入表和數據等初始化操作。
製作自己的MySQL鏡像:
root@e92cd8ad1356:/# apt-get update
root@e92cd8ad1356:/# exit
[root@VM_1_202_centos ~]# docker commit -m="提交說明文案" -a="作者" e92cd8ad1356【容器id】 image_name:tag
然後使用你製作的鏡像創建容器,你會發現容器裏面根本沒有你對數據庫做的相關操作,數據庫依然是最初的空庫。
原因見以下鏈接:
https://www.jianshu.com/p/530d00f97cbf
掛載本地數據文件
原因:mysql鏡像創建的容器再次製作鏡像之後,數據庫裏面的數據就會丟失,爲了使得數據和配置不丟失,需要把數據固化。
準備數據庫文件
0、進入數據庫,開啓數據庫的遠程登錄權限
use mysql;
update user set host = '%' where user = 'root'; #這一步可能會報錯,但是沒關係
FLUSH PRIVILEGES;
1、導出容器的初始數據庫(空的)文件到宿主機器
docker cp container_name:/var/lib/mysql /var/own/mysqldata
2、修改文件的共享權限
chmod 777 -R /var/own/mysqldata
使用本地數據庫文件
1、重新啓動新的容器時把數據庫文件掛載到本地文件:
docker run -itd -v /var/own/mysqldata:/var/lib/mysql --name mysqlnew mysql:5.6 /bin/bash
2、進入容器,免驗證的模式啓動mysql,防止權限問題
mysqld_safe --skip-grant-tables &