1刨裆、安裝部署docker
在linux下面只需簡(jiǎn)單的一個(gè)命令:
yum install docker
其他的系統(tǒng)類似伴郁。
2邑茄、編寫(xiě)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為容器的網(wǎng)絡(luò)模式策幼,需要連接navicat,所以用bridge奴紧,相當(dāng)于虛擬機(jī)的NAT模式特姐。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
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");
就是建表操作和插入數(shù)據(jù)的操作涩笤。
4、啟動(dòng)數(shù)據(jù)庫(kù)
root@localhost mysql # docker-compose pull
.......下載鏡像過(guò)程
啟動(dòng)容器
root@localhost mysql # docker-compose up -d
mysql_mysql_1_234be9b015e4 is up-to-date
此處需要在存放docker-compose.yml的文件夾進(jìn)行操作盒件。
5蹬碧、檢查初始化的數(shù)據(jù)
[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)
可以看到數(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)也可以渤闷。