background

Since I went to college, I have been using the student cloud server of Ali Cloud for blogging and programming. After graduation, I was too lazy to move to Ali Cloud. However, the price of Ali Cloud is becoming more and more expensive. In the last one or two years, the renewal fee of a 2-core 2G server also costs about 1500 for a year.

In addition, 2G memory and 40G storage can no longer meet my current needs. Running several more services will result in a shortage of memory and storage.

In November this year, I saw a Tencent cloud 2 core 4G 80SSD cloud server in the group, only 198 yuan in total in 3 years, which is cheaper than Ali Cloud for half a year, so I bought one directly.

I have been busy with my work recently, so I haven’t had time to migrate. Today is the New Year’s Day holiday. On January 1, 2022, I went to the company to migrate the server by myself. The New Year, happy happy ~

architecture

Also built and migrated for many times the server, each time are more dark circle, will also encounter a lot of pits. But it’s getting calmer. I’m not at my wit’s end.

When I first set up my blog, I took data migration into account.

The overall technical architecture is deployed using Docker container, which, together with Docker Compose, uniformly mounts the container data to separate folders. In this way, during migration, you only need to ensure that the version of the container is the same, and directly copy the data volume to the new server. Hahaha, but migration is really torture, all kinds of weird problems, that’s why it’s important to write about it. Save time for the next migration.

bloggers

WordPress

Blog front-end, the use of wordpress software. The reason why I choose WordPress is, on the one hand, I love my own handwritten blog code and style of age, on the other hand, the current WordPress is very good, decades of ecology, there are good block editor, as well as perfect documentation tutorials. You can build a personal blog website that you like most quickly.

MySQL

With WordPress, MySQL is a must-have and must be brought along. Now that you have it, you can also fiddle with other things in it.

nginx

Reverse proxy server. The reason Nginx is necessary is because there are many Node projects and subdomain names, so Nginx is also necessary.

Docker – compose files

This is the source file from a few years ago.

version: '3'
services:
  nginx:
    image: Nginx: 1.15.8
    restart: always
    # port mapping
    ports:
      - "80:80"
      - "443:443"
    # dependencies
    depends_on:
      - "wordpress"
    # data volume
    volumes:
      /conf. D to /etc/nginx/conf. D
      - "./conf.d:/etc/nginx/conf.d"
      - "/volumes/ever/www:/usr/share/nginx/html"
      - "./ssl:/var/ssl"
    networks:
      - app_net
    # container name
    container_name: "compose-nginx"
    
  mysql:
    image: Mysql: 5.7
    restart: always
    ports:
      - "3336:3306"
    # Environment variables
    environment:
      MYSQL_PASSWORD: **
      MYSQL_ROOT_PASSWORD: **
    volumes:
      - ./mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
      - /volumes/ever/mysql:/var/lib/mysql
    networks:
      app_net:
        Fixed subnet IP address, the network segment must be in the subnetwork 10.10.*.*
        ipv4_address: 10.1010.1.
    container_name: "compose-mysql"
    
  wordpress:
    depends_on:
      - mysql
    image: WordPress: 5.3.2
    ports:
      - "8211:80"
    restart: always
    environment:
      WORDPRESS_DB_NAME: **
      WORDPRESS_DB_HOST: **
      WORDPRESS_DB_USER: **
      WORDPRESS_DB_PASSWORD: **
    volumes:
      - /volumes/ever/wordpress:/var/www/html
    networks:
      app_net:
        Fixed subnet IP address, the network segment must be in the subnetwork 10.10.*.*
        ipv4_address: 10.1010.. 5
networks:
  app_net:
    driver: bridge
    ipam:
      config:
        # subnet
        - subnet: 10.10. 0. 0/ 16
Copy the code

There are a few points to note that are important for silky migration:

  1. Docker image version number, be sure to specify the exact version number X.X.X, not only x, not latest. If it’s even slightly different, migrating can take a lot of time to figure out the problem, and if it’s latest, it may not even be compatible.
  2. Whenever the container will generate data, all the data will be mounted to the outside of the container, and it is best to put in a folder, so that migration is convenient for one pot.

Data migration

If you want to load /volumes on a new server, just copy /volumes to the same location on the new server, then git the code and run:

docker compose up
Copy the code

It’s a one-click thing. perfect!

Ha ha ha, but goose actually took me half a day to fix it.

To copy volumes, run the following command:

scp -r /volumes user@ip:/volumes
Copy the code

There are hundreds of megabytes of data in this folder and it is still very slow to copy and for some reason I copied it several times and got mad.

Why did I copy it multiple times? Mysql > insert into mysql

[ERROR] InnoDB: Plugin initialization aborted with error Cannot open a file
Copy the code

I remember seeing him on the move, old rival. My first reaction was that maybe I didn’t copy it completely when I copied it, so THE data was missing, so I deleted it and copied it again, and it was the same mistake. After checking some information, some people said that mysql data backup should be stopped when the service. I thought this might be the reason, so I stopped the Docker on the old server, copied it again, and it ran successfully!

After mysql was ready, I opened wordpress, and found that THE service could be accessed, but the page could not be loaded. Browser F12 looked, and found that all CSS and JS were not loaded successfully, because wordpress wrote the absolute path, using my domain name, and the service was just stopped by me.

After restarting the service, the verification is ok and ali Cloud has resolved DNS to the new IP.

Speaking of parsing, there’s a delay, like 10 minutes. During these 10 minutes, I ping redream.cn to return a new IP, but the browser access is a network error, while I am still checking everything, it suddenly takes effect hahaha.

However, the wordpress method of writing absolute paths feels like a pit for me, and may cause minor problems if I put my blog under a subdomain later. But the problem is not big, check wordpress to change the domain name is quite simple, you can change in the Settings, if the service does not work, you can directly change in the database.

To optimize the

The migration is almost complete, but there is still a lot of optimization to be done. Here’s a quick list:

  1. The Mongo database is added to the Docker Compose cluster instead of being served separately.
  2. Avoid doing docker builds locally. Every time a local build is migrated, some images are lost if it is not pushed to the repository. Docker Hub can only be built by paying users. In preparation for migration, use Github Actions to build and push to docker Hub, local pull image only run.
  3. All data volumes are placed in the same directory.

The last

Today is the first day of 2022, ordinary and ordinary day, as always, at the office tinkering with a few things. Hope the New Year, happy, happy, healthy, have a breakthrough!

Tencent cloud server under Amway, really very fragrant, 1 core 2G 38/ year, 2 core 4G 74/ year. Less than a meal, learn more things, more possibilities!

Blog: www.redream.cn/