docker拉取mysql镜像:
[[email protected]_0_7_centos ~]$ sudo docker pull mysql:5.7 5.7: Pulling from library/mysql 80369df48736: Pull complete e8f52315cb10: Pull complete cf2189b391fc: Pull complete cc98f645c682: Pull complete 27a27ac83f74: Pull complete fa1f04453414: Pull complete d45bf7d22d33: Pull complete c7d49ffebc56: Pull complete 511a8052b204: Pull complete 5d5df4c12444: Pull complete d482603a2922: Pull complete Digest: sha256:44b33224e3c406bf50b5a2ee4286ed0d7f2c5aec1f7fdb70291f7f7c570284dd Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7
docker拉取mysql之后,我们来启动它:
先建好本地目录,以便跟docker做映射:
[[email protected]_0_7_centos ~]$ mkdir -p mydata/mysql/{log,lib,conf}
再执行启动命令,使用参数进行映射:
[[email protected]_0_7_centos ~]$ sudo docker run -p 3306:3306 --name mysql > -v /mydata/mysql/log:/var/log/mysql > -v /mydata/mysql/data:/var/lib/mysql > -v /mydata/mysql/conf:/etc/mysql > -e MYSQL_ROOT_PASSWORD=root > -d mysql:5.7 [sudo] password for mall: 8030a830a7b8af46fefd197e37c91cccfce867eb3593b08a64050fc90fefb98f
参数说明:
--name mysql:自定义容器名为“mysql”
-p 3306:3306:前者是你当前主机的3306端口,后者是当前容器中的3306端口,做了一个映射
-v /mydata/mysql/conf:/etc/mysql:将容器的配置文件夹/etc/mysql挂在到当前主机/mydata/mysql/conf
-v /mydata/mysql/log:/var/log/mysql:将容器的日志文件夹/var/log/mysql挂载到当前主机/mydata/mysql/log
-v /mydata/mysql/data:/var/lib/mysql/:将容器的数据文件夹/var/lib/mysql挂载到当前主机/mydata/mysql/data
-e MYSQL_ROOT_PASSWORD=root:初始化root用户的密码为root
我们进入该容器,登入mysql并创建新数据库mall:
[[email protected]_0_7_centos ~]$ sudo docker exec -it mysql /bin/bash [sudo] password for mall: [email protected]:/# mysql -uroot -proot --default-character-set=utf8 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.7.28 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> create database mall character set utf8;
Query OK, 1 row affected (0.00 sec)
将sql文件上传至本地:
[[email protected]_0_7_centos ~]$ cd mydata [[email protected]_0_7_centos mydata]$ rz -y rz waiting to receive. zmodem trl C ? 100% 185 KB 185 KB/s 00:00:01 0 Errors
从本地复制sql文件到docker:
[[email protected]_0_7_centos ~]$ sudo docker cp mydata/mall.sql mysql:/ [sudo] password for mall:
重新登入mysql,进入mall数据库,执行导入操作:
mysql> use mall;
mysql> source /mall.sql;
创建新账号,赋予所有权限给其他ip访问:
mysql> grant all privileges on *.* to ‘reader‘ @‘%‘ identified by ‘123456‘; Query OK, 0 rows affected, 1 warning (0.00 sec)