Docker迁移部署-亲测
系统环境:
Linux 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
docker -v
Docker version 27.3.1, build ce12230
docker-compose -v
Docker Compose version v2.32.2
mysql -V(docker - mysql)
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
1. 新服务器部署docker
cd /opt/software
curl -O -L “https://download.docker.com/linux/static/stable/x86_64/docker-27.3.1.tgz”
tar -zxvf docker-27.3.1.tgz
cd ./docker
cp * /usr/bin/
cd /etc/systemd/system/
touch docker.service
vi docker.service
*******
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd --selinux-enabled=false --insecure-registry=127.0.0.1
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
# TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
******
chmod 777 /etc/systemd/system/docker.service
systemctl status docker
systemctl enable docker
systemctl start docker
docker -v
Docker version 27.3.1, build ce12230
2. 部署docker-compose (github可能要翻墙才能访问)
curl -O -L “https://github.com/docker/compose/releases/download/v2.32.2/docker-compose-linux-x86_64”
cp docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v
***
Docker Compose version v2.32.2
3. 新服务器部署docker-mysql
配置my.cnf
创建一个临时容器,将容器内/etc/my.cnf 复制出来
docker run -d -p 2803:3306 \
--name TmpMysql \
-e MYSQL_ROOT_PASSWORD=root mysql:8.0
复制容器内my.cnf到本地:
cp TmpMysql:/etc/my.cnf /app/dkapp/mysql/conf/my.cnf
vim my.cnf
******
[mysqld]
default-time-zone = 'Asia/Shanghai'
log_timestamps=SYSTEM
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
slow-query-log=1
slow-query-log-file=/var/lib/mysql/slow-query.log
long-query-time=3
#启用错误日志:
log-error=/var/lib/mysql/error.log
#启用二进制日志
log-bin=/var/lib/mysql/mysql-bin.log
server-id=1
max_binlog_size = 100M
expire_logs_days = 30
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
port = 3306
default-character-set=utf8mb4
******
docker run -d \
-u=root \
-p 2002:3306 \
-e TZ=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=12345678 \
-v /app/dkapp/mysql/data:/var/lib/mysql \
-v /app/dkapp/mysql/conf/my.cnf:/etc/my.cnf \
-v /app/dkapp/mysql/log/mysql.log:/var/lib/mysql.log \
--restart=always \
--privileged=true \
--name mysql \
--hostname mysql8.0 \
-d mysql:8.0.27
查看mysql日志
docker logs -f mysql
# 设置允许远程用户访问,更新root - localhost 为 root - %
update user set host = '%' where user = 'root' and host='localhost';
# 设置root用户所有权
GRANT ALL ON *.* TO 'root'@'%';
创建gitea用户并授权,注:用户名不能使用:gitea,否则报错;
create user gitea@‘%’ identified by '12345678';
grant all privileges on gitea110.* to gitea@'%' with grant option;
flush privileges;
# 刷新权限
flush privileges;
远程访问mysql
mysql -h 192.168.1.110 -P 2002 -uroot -p"12345678"
mysql -h 127.0.0.1 -P 2002 -u root -p
4. 备份旧docker 应用程序数据:
4.1 复制docker应用程序到备份目录:
容器名称及原数据路径:gitea:/data/gitea
备份目录:/data/gitea-data-backup/
docker cp gitea:/data/gitea /data/gitea-data-backup/
4.2 将备份的docker应用数据scp到新服务器:
cd /data/gitea-data-backup/
scp -r ./ root@192.168.1.2:/app/dkapp/
4.3 创建yml文件安装gitea
cd /app/dkapp
vim docker-compose.yml
******
services:
server:
image: gitea/gitea:1.22.6
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__database__DB_TYPE=mysql
- GITEA__database__HOST=129.168.1.2:2802
- GITEA__database__NAME=giteadb
- GITEA__database__USER=gitea
- GITEA__database__PASSWD=12345678
restart: always
volumes:
- /app/dkapp/gitea:/data/gitea
- /app/dkapp/gitea/conf/app.ini:/data/gitea/conf/app.ini
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "2801:3000"
- "222:22"
******
启动yml文件
docker-compose up -d
启动gitea
docker start gitea
查看gitea启动日志
docker logs -f gitea
- 本文标签: Mysql
- 本文链接: https://www.jimmy2k.top/article/373
- 版权声明: 本文由JimmyZ的个人博客原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权