Step 1: Confirm the MySQL version of the master and slave servers.
The BinLog format of different versions of MySQL may be different, and it is better to use the same version. If the requirements are not met, it must be ensured that the master service version is not higher than the slave server version. Check it with the mysql -V command.
Step 2: Set up a connection account for the slave server on the master server and grant REPLICATION SLAVE 权限。
Each slave server connects to the master server using a standard MySQL username and password. Assuming the domain is mydomain.com, to create an account with the username "repl" that the slave uses to access the master from any host in the domain with the password "pass4slave". Create this account using the GRANT statement:
mysql>CREATE USER 'repl'@'%
.mydomain.com
' IDENTIFIED BY 'slavepass';
mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
Step 3: Configure the master server.
Open the binary log and make a unique Server ID. For example, add the following values to the my.cnf configuration file:
[mysqld]
log-bin = mysql-bin
server-id = 1
Step 4: Restart the master server.
1. Open the primary server MySQL client and execute the following SQL statement to prevent MySQL write operations
mysql>FLUSH TABLES WITH READ LOCK;
mysql >SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 73 | | | +------------------+----------+--------------+------------------+
File represents the binlog file being used by the master server; the value of Position is the same as the size of the binlog file, indicating the location of the next recorded event; Binlog_Do_DB and Binlog_Ignore_DB are the filter options for the master server to control the content written to the binlog file. Do not do any filtering.
The two fields File and Position indicate which binlog file the slave will copy from, and where the copy starts, and are also parameters to the CHANGE MASTER TO statement.
Step 5: Configure the slave server.
The configuration of the slave server is similar to that of the master server. A unique Server ID must be provided (it cannot be the same as the master server ID). After the configuration is complete, the MySQL server also needs to be restarted. The configuration is as follows:
[mysqld]
log-bin = mysql-bin
server-id = 2
Step 6: Start the slave server.
Next let the slave connect to the master and start redoing the events in the master's binlog file.
Step 7: Start the main server information.
Use the CHANGE MASTER TO statement to specify the master server information, do not set it in the configuration file. This statement can replace the information of the master server in the configuration file. In addition, you can specify a different master server for the slave server without stopping the server. The statement is as follows
mysql>CHANGE MASTER TO
-> -> -> -> ->MASTER_HOST='
192.168.1.100
',MASTER_USER='repl',
MASTER_PASSWORD='slave
pass
',MASTER_LOG_FILE='
mysql-bin.000001
',MASTER_LOG_POS=
0
;
The value specified here MASTER_LOG_POS
is 0, because the reading starts from the beginning of the log. You can also start reading at the file position shown in the statement SHOW MASTER STATUS. Just copy the current database snapshot of the master server to the slave server
Create data snapshots with mysqldump
mysql>FLUSH TABLES WITH READ LOCK;
shell>mysqldump --all-databases --lock-all-tables >dbdump.db
shell>mysqldump --all-databases --master-data >dbdump.db
mysql>UNLOCK TABLES;
If the system variables ft_stopword_file, ft_min_word_len or ft_max_word_len of your master and slave servers are different, there will also be problems in replicating tables with full-text indexes.
Step 8: Check whether the settings of the slave server are accurate.
Use the SHOW SLAVE STATUS statement to see if the slave's settings are accurate.
Step 9: Execute the START SLAVE statement to start replication.
mysql>START SLAVE;