美文网首页
docker-compose部署mysql

docker-compose部署mysql

作者: 小月半会飞 | 来源:发表于2019-03-18 17:13 被阅读0次

    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端口给外面访问也可以。

    相关文章

      网友评论

          本文标题:docker-compose部署mysql

          本文链接:https://www.haomeiwen.com/subject/tbsbmqtx.html