Docker-创建mysql容器进行持久化&设置远程连接&导入sql文件(centos7)

1,下载镜像

//下载镜像
[root@iz2zei9wv79ob7vwy7y1jcz /]#  docker pull mysql
//查看已下载镜像
[root@iz2zei9wv79ob7vwy7y1jcz /]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis                        latest              857c4ab5f029        2 days ago          98.2 MB
docker.io/nginx                        latest              e445ab08b2be        12 days ago         126 MB
docker.io/mysql                        latest              2151acc12881        13 days ago         445 MB
docker.io/tomcat                       latest              238e6d7313e3        2 weeks ago         506 MB
docker.io/frolvlad/alpine-oraclejdk8   latest              7f2049911fa1        6 months ago        169 MB


2,创建mysql容器并进行持久化配置

首先在物理机上创建文件夹用来存放mysql容器中的数据。
我的是放在/home/docker/mysql/data

[root@iz2zei9wv79ob7vwy7y1jcz /]# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@iz2zei9wv79ob7vwy7y1jcz /]# cd home
[root@iz2zei9wv79ob7vwy7y1jcz home]# mkdir docker
[root@iz2zei9wv79ob7vwy7y1jcz home]# ls
admin  developer  docker
[root@iz2zei9wv79ob7vwy7y1jcz home]# cd docker
[root@iz2zei9wv79ob7vwy7y1jcz docker]# ls
[root@iz2zei9wv79ob7vwy7y1jcz docker]# mkdir mysql
[root@iz2zei9wv79ob7vwy7y1jcz docker]# cd mysql
[root@iz2zei9wv79ob7vwy7y1jcz mysql]# mkdir data
[root@iz2zei9wv79ob7vwy7y1jcz mysql]# ls
data

下来创建mysql容器并挂载本地文件夹

  • - -name 给容器起别名
  • -d 后台启动
  • --restart 设置容器启动状态
  • -p 设置物理机端口与容器端口映射
  • -e MYSQL_ROOT_PASSWORD=root 设置root 账户密码
  • -v /home/docker/mysql/data:/var/lib/mysql : 前为我们物理机设置文件夹的绝对路径 后为容器中我们要持久化的文件夹
  • mysql 为自己下载mysql镜像
[root@iz2zei9wv79ob7vwy7y1jcz docker]# docker run --name mysql01 -d --restart always -p3307:3306 -e MYSQL_ROOT_PASSWORD=root -v /home/docker/mysql/data:/var/lib/mysql mysql
5282b96fd1156c2d1399ab1a07369254e61aa6a2c1b8e54c78ae16497938f97d
//查看已启动容器
[root@iz2zei9wv79ob7vwy7y1jcz docker]# docker ps -s
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES               SIZE
5282b96fd115        mysql               "docker-entrypoint..."   19 seconds ago      Up 18 seconds       33060/tcp, 0.0.0.0:3307->3306/tcp   mysql01             7 B (virtual 445 MB)
568d1f7734d1        2bbd10d5ff05        "java -jar /app.jar"     About an hour ago   Up About an hour    0.0.0.0:8090->8080/tcp              miaoshaserver01     0 B (virtual 197 MB)

使用以下命令登陆并进入数据库:

//进入容器
[root@iz2zei9wv79ob7vwy7y1jcz docker]# docker exec -it mysql01 bash
root@5282b96fd115:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 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.01 sec)

退出

3,如何在物理机连接容器内的mysql数据库(远程连接docker容器中数据库)

若直接连接会报错,因此我们需要进入容器内的mysql数据库进行设置’

//先查看登陆权限
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host      | user             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
SDiN        | root             | caching_sha2_password | $A$005$s61M.%mjF--.v
oLQkNpYjomzTmRKsXwr0slIrEFayZRvsz8Lhs681Sb9 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session    | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys        | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root             | caching_sha2_password | $A$005$	Xmx`"W[B+zyDo=+qb9DAzxt5lsZlLd2EfwMyiHalzVIb.WzPspmivjGf53 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)


设置远程用户访问权限:
grant all privileges on *.* to 'root'@'%';
 
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
 
 
flush privileges;

可以看到物理机连接数据库成功

mysql
[root@iz2zei9wv79ob7vwy7y1jcz /]# mysql -u root -p -h 127.0.0.1 -P 3307
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| miaosha            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)



如何将物理机sql文件传入容器中的mysql数据库

首先将文件从物理机copy到容器中,

  • /tmp/miaosha.sql 为物理机中sql文件存放的绝对路径
  • mysql01: 容器别名
  • /tmp/ 容器中的绝对路径
[root@iz2zei9wv79ob7vwy7y1jcz /]# docker cp /tmp/miaosha.sql mysql01:/tmp/
[root@iz2zei9wv79ob7vwy7y1jcz /]# docker exec -it mysql01 bash
root@5282b96fd115:/# cd tmp
root@5282b96fd115:/tmp# ls
miaosha.sql

将sql文件加载进数据库:

  • source /tmp/miaosha.sql;
mysql> source /tmp/miaosha.sql;
Query OK, 0 rows affected (0.00 sec)
.......................
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| miaosha            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

你可能感兴趣的:(Docker)