docker运行mysql_docker安装并运行mysql

docker拉取mysql镜像:

[mall@VM_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 imagefor mysql:5.7docker.io/library/mysql:5.7

docker拉取mysql之后,我们来启动它:

先建好本地目录,以便跟docker做映射:

[mall@VM_0_7_centos ~]$ mkdir -p mydata/mysql/{log,lib,conf}

再执行启动命令,使用参数进行映射:

[mall@VM_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 formall:

8030a830a7b8af46fefd197e37c91cccfce867eb3593b08a64050fc90fefb98f

参数说明:

-name:自定义容器名为“mysql”

-p 3306:3306:前者是你当前主机的3306端口,后者是当前容器中的3306端口,做了一个映射

-v /mydata/mysql/conf:/etc/mysql:将容器的配置文件夹挂在到当前主机

-v /mydata/mysql/log:/var/log/mysql:将容器的日志文件夹挂载到当前主机

-v /mydata/mysql/data:/var/lib/mysql/:将容器的数据文件夹挂载到当前主机

-e MYSQL_ROOT_PASSWORD=root:初始化root用户的密码为root

我们进入该容器,登入mysql并创建新数据库mall:

[mall@VM_0_7_centos ~]$ sudo docker exec -it mysql /bin/bash

[sudo] password formall:

root@8030a830a7b8:/# 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 connectionid is 2Server version:5.7.28MySQL 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 clearthe current input statement.

mysql> create database mall character set utf8;

Query OK, 1 row affected (0.00 sec)

将sql文件上传至本地:

[mall@VM_0_7_centos ~]$ cd mydata

[mall@VM_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:

[mall@VM_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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值