安装Docker Compose并迁移WordPress至Docker

发布于 2021-12-15  39 次阅读


安装Docker Compose

curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose --version

创建wp的dc所在文件夹及文件

mkdir /data/wordpress

nano /data/wordpress/stack.yml

version: '3'
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 密码1
      MYSQL_DATABASE: wp
      MYSQL_USER: wp
      MYSQL_PASSWORD: 密码2
    ports:
      - "3306:3306"
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wp_site:/var/www/html
    ports:
      - "801:80"
      - "4431:443"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wp
      WORDPRESS_DB_PASSWORD: 密码2
      WORDPRESS_DB_NAME: wp
volumes:
  db_data:
  wp_site:
docker-compose -f /data/wordpress/stack.yml up -d

上传并配置原wp数据库备份文件

将原wordpress数据库备份文件上传至/data/wordpress,并拷贝至docker wp数据库/opt

cd /data/wordpress

docker cp wp.sql wordpress_db_1:/opt/

进入docker wordpress的mysql,并替换wp数据库,建议直接用navicat编辑数据库!

以下是用code的方式(不推荐)

docker exec -it wordpress_db_1 bash

#如果不设置max_allowed_packet,该版本默认4M,可能导致无法替换wp数据库,因此先修改默认上限

mysql -uroot -p【密码1】

set global max_allowed_packet = 100 * 1024 * 1024;

#命令行修改之后,需要退出当前回话(关闭当前mysql server链接),然后重新登录才能查看修改后的值。通过命令行修改只能临时生效,下次数据库重启后又复原了。max_allowed_packet 最大值是1G(1073741824)

exit

mysql -uroot -p【密码1】

select @@max_allowed_packet;

exit

#替换wp数据库

mysql -uroot -p wp>/opt/wp.sql

exit

将原wordpress wp-content备份文件夹上传至/data/wordpress/wordpress,并拷贝至docker wordpress

cd /data/wordpress
docker cp wordpress/wp-content/plugins wordpress_wordpress_1:/opt/
docker cp wordpress/wp-content/themes wordpress_wordpress_1:/opt/
docker cp wordpress/wp-content/uploads wordpress_wordpress_1:/opt/
docker exec -it wordpress_wordpress_1 bash
cp -r /opt/* /var/www/html/wp-content/
exit

修改wordpress权限

docker exec -it wordpress_wordpress_1 bash
chown -R www-data:www-data /var/www/html
exit

访问ip:801即可


Love is merely a madness.