compose mysql_docker-compose部署mysql

1、安装部署docker

在linux下面只需简单的一个命令:

yum install docker

其他的系统类似。

2、编写docker-compose文件

version: '2'

services:

mysql:

network_mode: "bridge"

environment:

MYSQL_ROOT_PASSWORD: "yourpassword"

MYSQL_USER: 'test'

MYSQL_PASS: 'yourpassword'

image: "docker.io/mysql:5.5"

restart: always

volumes:

- "./db:/var/lib/mysql"

- "./conf/my.cnf:/etc/my.cnf"

- "./init:/docker-entrypoint-initdb.d/"

ports:

- "3306:33060"

这里稍微解释一下,其中,network_mode为容器的网络模式,需要连接navicat,所以用bridge,相当于虚拟机的NAT模式。MYSQL_ROOT_PASSWORD为数据库的密码,也就是root用户的密码。MYSQL_USER和MYSQL_PASS另外一个用户名和密码。image为你拉取镜像的地址和版本,当然也可以换成自己的镜像仓库,这里使用官方的。volumes里面的参数为映射本地和docker容器里面的文件夹和目录。./db 用来存放了数据库表文件,./conf/my.cnf存放自定义的配置文件,./init存放初始化的脚本。ports 为映射主机和容器的端口。写好docker-compose.yml后把相应的文件夹建好,当然也可以换成你自己的。下面的是博主的文件夹结构。

root@localhost mysql # tree

.

├── conf

│ └── my.cnf

├── db

├── docker-compose.yml

└── init

└── init.sql

3、编写配置文件和初始化文件

root@localhost conf # cat my.cnf

[mysqld]

user=mysql

default-storage-engine=INNODB

character-set-server=utf8

[client]

default-character-set=utf8

[mysql]

default-character-set=utf8

这里的配置文件只是一个简单的举例,大家需要根据自己的配置来更改。

root@localhost init # cat init.sql

create database test;

use test;

create table user

(

id int auto_increment primary key,

username varchar(64) unique not null,

email varchar(120) unique not null,

password_hash varchar(128) not null,

avatar varchar(128) not null

);

insert into user values(1, "zhangsan","test12345@qq.com","passwd","avaterpath");

insert into user values(2, "lisi","12345test@qq.com","passwd","avaterpath");

就是建表操作和插入数据的操作。

4、启动数据库

root@localhost mysql # docker-compose pull

.......下载镜像过程

启动容器

root@localhost mysql # docker-compose up -d

mysql_mysql_1_234be9b015e4 is up-to-date

此处需要在存放docker-compose.yml的文件夹进行操作。

5、检查初始化的数据

[root@localhost ~]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

33a1b073dcb1 mysql:5.5 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp root_mysql_1

root@localhost mysql # docker exec -it cffe8d56f222 bash

root@localhost:/# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 11

Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from user;

+----+----------+------------------+---------------+------------+

| id | username | email | password_hash | avatar |

+----+----------+------------------+---------------+------------+

| 1 | zhangsan | test12345@qq.com | passwd | avaterpath |

| 2 | lisi | 12345test@qq.com | passwd | avaterpath |

+----+----------+------------------+---------------+------------+

2 rows in set (0.00 sec)

可以看到数据存入到数据库当中去。

6、验证远程连接

在windows宿主机上面也可以用Navicat连接上数据库。ip填虚拟机的ip,port填写3306,密码为docker-compose文件中的root密码。此处需要将宿主机(我是liunx虚拟机)的防火墙给关掉,要不然一直连接不上,或者你开启3306端口给外面访问也可以。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值