docker系列一MySQL5.6镜像使用&挂载本地数据库文件

下载原始的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 &

你可能感兴趣的:(docker系列)