赞
踩
创建当前mysql的配置文件还有目录,项目文件如下图
version: '3.3' services: mysql: image: 'daocloud.io/library/mysql:5.7.7' container_name: mysql-5.7.7 # network_mode:'bridge' environment: MYSQL_ROOT_PASSWORD: 'Aa123456' MYSQL_USER: 'fmi110' MYSQL_PASS: 'Aa123456' # env_file: # - ./env/mysql.env restart: on-failure volumes: - ./data:/var/lib/mysql - ./config/my.cnf:/etc/my.cnf - ./init:/docker-entrypoint-initdb.d/ ports: - '3307:3306' # depends_on: # - xxx
说明:
services.mysql.image: 指定mysql镜像的版本
services.mysql.environment: 环境变量
MYSQL_ROOT_PASSWORD:必须,指定root用户的密码
MYSQL_USER、MYSQL_PASS 新建用户并指定密码,可选。
services.mysql.volumes: 数据卷挂载,将本地主机的目录,映射成容器里对应的目录
./data:/var/lib/mysql :mysql的数据存储到当前目录的 data目录下
./config/my.cnf:/etc/my.cnf :指定加载mysql的配置文件为当前目录的 config/my.cnf 文件
./init:/docker-entrypoint-initdb.d/ :容器初始化时,执行 init目录下的mysql脚本
services.mysql.ports: 指定端口映射,这里本地主机的 3307 映射到容器的3306端口,则外部连接数据库的url为 jdbc:mysql://localhsot:3307/xxxxx
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
-- GRANT ALL privileges on *.* TO 'root'@'%';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native-pasword BY 'Aa123456';
授权 root 用户可以远程链接,因为是脚本要给执行权限
root@192:/data/mysql# cd init/
root@192:/data/mysql/init# ls
init.sql
root@192:/data/mysql/init# chmod +x init.sql
进入docker-compose.yml文件所在目录,执行如下命令
docker-compose up
使用镜像的说明地址:
https://link.juejin.cn/?target=https%3A%2F%2Fdashboard.daocloud.io%2Fpackages%2Ffa51c1d6-9dc2-49d9-91ac-4bbfc24a1bda
https://www.cnblogs.com/haima/p/13649336.html
mkdir /data/mysql5.7
cd /data/mysql5.7
注意:文件名字必需是docker-compose.yml
version: '3.8' services: mysql: container_name: mysql57 image: mysql:5.7.31 restart: always ports: - 3307:3306 privileged: true volumes: - $PWD/mysql57/log:/var/log/mysql - $PWD/mysql57/conf/my.cnf:/etc/mysql/my.cnf - $PWD/mysql57/data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: "123456" MYSQL_USER: 'haima' MYSQL_PASS: '123456' command: [ '--character-set-server=utf8mb4', '--collation-server=utf8mb4_general_ci', '--max_connections=3000' ] networks: - myweb networks: myweb: driver: bridge
#!/bin/bash mkdir -p $PWD/mysql57/{conf,data,log} #创建本地文件夹 #新建配置文件 tee $PWD/mysql57/conf/my.cnf<<-'EOF' [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql lower_case_table_names=1 #实现mysql不区分大小(开发需求,建议开启) # By default we only accept connections from localhost #bind-address = 127.0.0.1 # Disabling symbolic-links is recommended to prevent assorted security risks default-time_zone = '+8:00' # 更改字符集 如果想Mysql在后续的操作中文不出现乱码,则需要修改配置文件内容 symbolic-links=0 character-set-server=utf8mb4 [client] default-character-set=utf8mb4 [mysql] default-character-set=utf8mb4 EOF
[root@centos7 mysql]# chmod +x init-mysql.sh docker-compose.yml #加执行权限
[root@centos7 mysql]# ./init-mysql.sh 创建
[root@centos7 mysql]# bash init-mysql.sh 创建
[root@centos7 mysql]# tree ./ #查看目结构
./
├── docker-compose.yml
├── init-mysql.sh
└── mysql57
├── conf
│ └── my.cnf
├── data
└── log
docker-compose up -d
此时服务已经启动成功了.使用角本是不是很爽,嘿嘿…
docker exec -it mysql57 mysql -uroot -p123456
说一下需求,因为我这里是需要我们华为与上面的数据库,做Yearning-sql审核我把华为云上面的数据库-把里面的库拷贝了几个库,拿到我测试环境的mysql上面做实验
因为我是本地数据库,我这里就直接容器使用本地数据库
CREATE DATABASE Yearning CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
grant all on Yearning.* to 'yearning'@'%' identified by 'sMHcsff32wUv38';
root@192:/# docker load -i yearning.tar
eb4bde6b29a6: Loading layer [==================================================>] 5.876MB/5.876MB
21b671aaac70: Loading layer [==================================================>] 22.47MB/22.47MB
96f46b88d808: Loading layer [==================================================>] 2.56kB/2.56kB
10df96ef7e05: Loading layer [==================================================>] 2.185MB/2.185MB
Loaded image: test/yearning:latest
docker run -d \
--name yearning \
--restart always \
-p 8000:8000 \
-e MYSQL_USER=yearning \
-e MYSQL_ADDR=xxxxx:3306 \
-e MYSQL_PASSWORD=sMHcsff32wUv38 \
-e MYSQL_DB=Yearning \
-e TZ="Asia/Shanghai" \
test/yearning:latest
http://xxxxxxx:8000/#/login
查看容器日志是成功的
docker logs 容器id
mysql -uroot -p123456
mysql> grant all on bin.* to 'yearning'@'%' identified by '123456';
账号 aike
密码 Aike@11
账号 daxia
密码 Daxia@11
CREATE TABLE aike
( job_id INTEGER NOT NULL PRIMARY KEY
, job_title VARCHAR(50) NOT NULL
) ;
img-RI5f6i0l-1657263999673)]
[外链图片转存中…(img-0ZsCszwo-1657263999674)]
[外链图片转存中…(img-Ipx0ORnr-1657263999674)]
[外链图片转存中…(img-mKzDLTgH-1657263999674)]
[外链图片转存中…(img-VTD4MQE6-1657263999675)]
[外链图片转存中…(img-cODOQoW2-1657263999675)]
[外链图片转存中…(img-Cr6bwcAS-1657263999675)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。