Docker 部署 Mysql 8.0 并设置数据持久化

Docker 部署 Mysql 8.0 并设置数据持久化

创建目录
创建 my.cnf 文件
创建初始化 SQL 脚本
启动 Mysql 容器
方法1:docker run
方法2:docker-compose
远程连接数据库测试
创建目录

创建项目目录

mkdir -p /home/workspace/docker-mysql

创建挂载目录

mkdir -p /home/workspace/docker-mysql/mysql/data
mkdir -p /home/workspace/docker-mysql/mysql/conf
mkdir -p /home/workspace/docker-mysql/mysql/initdb

创建 my.cnf 文件

Step1:启动一个 mysql 容器

docker run --name test -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest

docker run --name test -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest


systemctl start docker
systemctl restart docker


Step2:拷贝容器内的 my.cnf 文件到宿主机

docker cp test:/etc/mysql/my.cnf /home/workspace/docker-mysql/mysql/conf

Step3:退出并删除容器

docker stop test && docker rm test

创建初始化 SQL 脚本
使用下面的命令在 /home/workspace/docker-mysql/mysql/initdb 目录下创建并打开一个 SQL 脚本文件:

vim /home/workspace/docker-mysql/mysql/initdb/initdb.sql

在 initdb.sql 文件中编写以下内容,然后保存并退出:

-- 创建一个名为 docker_test 的数据库
CREATE DATABASE IF NOT EXISTS docker_test;
-- 允许 root 用户远程连接数据库
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'root';
FLUSH PRIVILEGES;

该 SQL 脚本的功能为创建一个名为 docker_test 的数据库并允许 root 用户远程连接数据库。

启动 Mysql 容器
启动 Mysql 容器有两种方法:使用 docker run 命令和使用 docker-compose。

方法1:docker run

docker run --name test-mysql -p 3306:3306
-e MYSQL_ROOT_PASSWORD=root
-v /home/workspace/docker-mysql/mysql/conf/my.cnf:/etc/mysql/my.cnf
-v /home/workspace/docker-mysql/mysql/initdb/:/docker-entrypoint-initdb.d/
-v /home/workspace/docker-mysql/mysql/data:/var/lib/mysql
--restart=on-failure:3 -d mysql

docker run --name test-mysql -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v /opt/app/softA/workspace/docker-mysql/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /opt/app/softA/workspace/docker-mysql/mysql/initdb/:/docker-entrypoint-initdb.d/ \
-v /opt/app/softA/workspace/docker-mysql/mysql/data:/var/lib/mysql \
--restart=on-failure:3 -d mysql


mkdir -p /opt/app/softA/workspace/docker-mysql
mkdir -p /opt/app/softA/workspace/docker-mysql/mysql/data
mkdir -p /opt/app/softA/workspace/docker-mysql/mysql/conf
mkdir -p /opt/app/softA/workspace/docker-mysql/mysql/initdb



docker cp test:/etc/mysql/my.cnf /opt/app/softA/workspace/docker-mysql/mysql/conf
docker stop test && docker rm test


docker run --name test-mysql -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v /opt/app/softA/workspace/docker-mysql/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /opt/app/softA/workspace/docker-mysql/mysql/initdb/:/docker-entrypoint-initdb.d/ \
-v /opt/app/softA/workspace/docker-mysql/mysql/data:/var/lib/mysql \
--restart=on-failure:3 -d mysql


方法2:docker-compose

使用下面的命令在 /home/workspace/docker-mysql 目录下创建并打开 docker-compose.yml 文件:

vim /home/workspace/docker-mysql/docker-compose.yml

在 docker-compose.yml 文件中编写以下内容,然后保存并退出:

version: '3'
services:
docker-mysql:
image: 'mysql:8.0.20'
container_name: test-mysql
privileged: true
restart: on-failure:3
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- /home/workspace/docker-mysql/mysql/conf/my.cnf:/etc/mysql/my.cnf
- /home/workspace/docker-mysql/mysql/initdb/:/docker-entrypoint-initdb.d/
- /home/workspace/docker-mysql/mysql/data/:/var/lib/mysql/
ports:
- '3306:3306'
command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp

然后在 /home/workspace/docker-mysql 目录下执行:

docker-compose up -d

远程连接数据库测试
C:\Users\Sunny
λ mysql -h 192.168.194.133 -P 3306 -u root -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> show databases;
+--------------------+
| Database |
+--------------------+
| docker_test |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> quit;
Bye

C:\Users\Sunny
————————————————
版权声明:本文为CSDN博主「二流人物」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wzfgd/article/details/114751108

docker 安装nginx 并部署

docker 安装nginx 并部署

sudo docker run --name nginx-test -p 8081:80 -d nginx
mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
sudo docker cp nginx-test:/etc/nginx/nginx.conf ~/nginx/conf

docker stop nginx-test && docker rm nginx-test

sudo docker run -d -p 8082:80 --name nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx

在~/nginx/www 下创建 index.html

echo this is my cat ! > index.html

http://bridged-node-220:8082/

重新访问:localhost:8082


posted @ 2022-09-13 16:20  三里清风18  阅读(1575)  评论(0编辑  收藏  举报