Step 2: Configure the standby machine
Let’s continue. Stop the MySQL service program on the standby machine, and move the database directory copied from the host machine to the data directory on the standby machine. Please make sure to change the owner and group of the directory to the corresponding values of the MySQL user, and modify the file mode to 660 (only readable and writable for the owner and group), and the directory itself to 770 (only for the owner and group) readable, writable and executable).
Continue. Start the MySQL service program on the backup machine and confirm that MySQL is working properly. Run a few select queries (do not update or insert queries) to see if the data snapshot obtained in the first step is successful. Then, shut down the MySQL service program after the test is successful.
Configure the host that needs to be accessed on the standby machine to receive changes from the host. So you need to edit the "my.cnf" file on the server and add the following lines in the [mysqld] section:
master-host=10.1.1.1
master-user=replicate
master-password=password
After starting the standby service program, the standby service program will check the host specified in the "my.cnf" file to see if there are any changes, and merge these changes into its own database. The standby machine maintains the host's update records, which are received from the host's "master.info" file. The status of the standby thread can be seen through the sql command "SHOW SLAVE-STATUS". If an error occurs while processing the binary log on the standby machine, it will cause the standby machine thread to exit and generate a message in the *.err log file. The error can then be corrected and the standby thread can be restarted using the SQL statement "SLAVE START". The thread will continue processing from where host binary log processing left off.
At this point, the data changes that occurred on the main machine should have been copied to the standby machine. To test it, you can insert or update a record on the main machine and select this record on the standby machine.
Now we have this master-slave relationship from machine A to machine B, which allows us to redirect all queries to machine B when machine A may crash, but when machine A recovers , we have no way to restore the changes to machine A. To solve this problem, we create a master-slave relationship from machine B to machine A.