docker實踐之docker-compose部署mysql

docker實踐之docker-compose部署mysql

前面用golang寫了一個api server,但是要用到一些測試數據,又要方便給別人,想來用docker部署環境最簡單了。只需要簡單執行兩個命令就可以搞定了。博主的環境是windows然後在windows裏面部署一個centos7的虛擬機。在虛擬機裏面安裝部署了docker。

1、安裝部署docker

在linux下面只需簡單的一個命令:

yum install docker

其他的系統類似。

2、編寫docker-compose文件

version: '2'
services:
    mysql:
        network_mode: "host"
        environment:
            MYSQL_ROOT_PASSWORD: "yourpassword"
            MYSQL_USER: 'test'
            MYSQL_PASS: 'yourpassword'
        image: "docker.io/mysql:latest" 
        restart: always
        volumes:
            - "./db:/var/lib/mysql"
            - "./conf/my.cnf:/etc/my.cnf"
            - "./init:/docker-entrypoint-initdb.d/"
        ports:
            - "3306:33060"

這裏稍微解釋一下,其中,network_mode爲容器的網絡模式,一般自己測試用host模式就可以了。MYSQL_ROOT_PASSWORD爲數據庫的密碼,也就是root用戶的密碼。MYSQL_USERMYSQL_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 
use mysql;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
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","[email protected]","passwd","avaterpath");
insert into user values(2, "lisi","[email protected]","passwd","avaterpath");

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpassword';這一句比較重要,放開root登入權限,如果你要在其他的主機用root用戶登入到數據庫就需要寫入這句話。其他的語句就是建表操作和插入數據的操作了。

4、啓動數據庫

root@localhost mysql # docker-compose pull

.......下載鏡像過程

root@localhost mysql # docker-compose up -d

mysql_mysql_1_234be9b015e4 is up-to-date
root@localhost mysql # 

此處需要在存放docker-compose.yml的文件夾進行操作。

5、檢查初始化的數據

root@localhost mysql # docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS               NAMES
cffe8d56f222        docker.io/mysql:latest   "docker-entrypoint..."   21 minutes ago      Up 21 minutes                           mysql_mysql_1_234be9b015e4
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 | [email protected] | passwd        | avaterpath |
|  2 | lisi     | [email protected] | passwd        | avaterpath |
+----+----------+------------------+---------------+------------+
2 rows in set (0.00 sec)

可以看到數據存入到數據庫當中去。

6、驗證遠程連接

在windows宿主機上面也可以用Navicat連接上數據庫。ip填虛擬機的ip,port填寫3306,密碼爲docker-compose文件中的root密碼。此處需要將宿主機(我是liunx虛擬機)的防火牆給關掉,要不然一直連接不上,或者你開啓3306端口給外面訪問也可以。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章