docker實踐之docker-compose部署mysql方式
前面用golang寫了一個api server,但是要用到一些測試數據,又要方便給別人,想來用docker部署環(huán)境最簡單了。
只需要簡單執(zhí)行兩個命令就可以搞定了。
博主的環(huán)境是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/"
這里稍微解釋一下,其中,network_mode為容器的網絡模式,一般自己測試用host模式就可以了。
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 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';
這一句比較重要,放開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 | 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端口給外面訪問也可以。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
gitlab-runner中搭建nvm、nrm以及優(yōu)化maven打包方式
文章描述了如何在GitLab Runner上配置NVM、NRM,并優(yōu)化Maven打包過程,通過上述配置和優(yōu)化,作者將Maven打包時間從三分鐘縮短到不到40秒2024-11-11docker-compose ports和expose的區(qū)別詳解
這篇文章主要介紹了docker-compose ports和expose的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01