文章目錄
docker實(shí)踐之docker-compose部署mysql
1樊拓、安裝部署docker
2、編寫(xiě)docker-compose文件
3实束、編寫(xiě)配置文件和初始化文件
4卵惦、啟動(dòng)數(shù)據(jù)庫(kù)
5、檢查初始化的數(shù)據(jù)
6谈竿、驗(yàn)證遠(yuǎn)程連接
docker實(shí)踐之docker-compose部署mysql
前面用golang寫(xiě)了一個(gè)api server团驱,但是要用到一些測(cè)試數(shù)據(jù),又要方便給別人空凸,想來(lái)用docker部署環(huán)境最簡(jiǎn)單了嚎花。只需要簡(jiǎn)單執(zhí)行兩個(gè)命令就可以搞定了。博主的環(huán)境是windows然后在windows里面部署一個(gè)centos7的虛擬機(jī)呀洲。在虛擬機(jī)里面安裝部署了docker紊选。
1、安裝部署docker
在linux下面只需簡(jiǎn)單的一個(gè)命令:
yum install docker
其他的系統(tǒng)類似道逗。
2兵罢、編寫(xiě)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為容器的網(wǎng)絡(luò)模式卖词,一般自己測(cè)試用host模式就可以了。MYSQL_ROOT_PASSWORD為數(shù)據(jù)庫(kù)的密碼吏夯,也就是root用戶的密碼此蜈。MYSQL_USER和MYSQL_PASS另外一個(gè)用戶名和密碼。image為你拉取鏡像的地址和版本噪生,當(dāng)然也可以換成自己的鏡像倉(cāng)庫(kù)裆赵,這里使用官方的。volumes里面的參數(shù)為映射本地和docker容器里面的文件夾和目錄跺嗽。./db 用來(lái)存放了數(shù)據(jù)庫(kù)表文件顾瞪,./conf/my.cnf存放自定義的配置文件,./init存放初始化的腳本抛蚁。ports 為映射主機(jī)和容器的端口陈醒。寫(xiě)好docker-compose.yml后把相應(yīng)的文件夾建好,當(dāng)然也可以換成你自己的瞧甩。下面的是博主的文件夾結(jié)構(gòu)钉跷。
root@localhost mysql # tree
.
├── conf
│ └── my.cnf
├── db
├── docker-compose.yml
└── init
└── init.sql
3、編寫(xiě)配置文件和初始化文件
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
這里的配置文件只是一個(gè)簡(jiǎn)單的舉例肚逸,大家需要根據(jù)自己的配置來(lái)更改爷辙。
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","test12345@qq.com","passwd","avaterpath");
insert into user values(2, "lisi","12345test@qq.com","passwd","avaterpath");
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpassword';這一句比較重要彬坏,放開(kāi)root登入權(quán)限,如果你要在其他的主機(jī)用root用戶登入到數(shù)據(jù)庫(kù)就需要寫(xiě)入這句話膝晾。其他的語(yǔ)句就是建表操作和插入數(shù)據(jù)的操作了栓始。
4、啟動(dòng)數(shù)據(jù)庫(kù)
root@localhost mysql # docker-compose pull
.......下載鏡像過(guò)程
root@localhost mysql # docker-compose up -d
mysql_mysql_1_234be9b015e4 is up-to-date
root@localhost mysql #
此處需要在存放docker-compose.yml的文件夾進(jìn)行操作血当。
5幻赚、檢查初始化的數(shù)據(jù)
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 | test12345@qq.com | passwd | avaterpath |
| 2 | lisi | 12345test@qq.com | passwd | avaterpath |
+----+----------+------------------+---------------+------------+
2 rows in set (0.00 sec)
可以看到數(shù)據(jù)存入到數(shù)據(jù)庫(kù)當(dāng)中去。
6臊旭、驗(yàn)證遠(yuǎn)程連接
在windows宿主機(jī)上面也可以用Navicat連接上數(shù)據(jù)庫(kù)落恼。ip填虛擬機(jī)的ip,port填寫(xiě)3306离熏,密碼為docker-compose文件中的root密碼佳谦。此處需要將宿主機(jī)(我是liunx虛擬機(jī))的防火墻給關(guān)掉,要不然一直連接不上滋戳,或者你開(kāi)啟3306端口給外面訪問(wèn)也可以钻蔑。