MySQL advanced knowledge - master-slave replication

Introduction: This chapter explains the main MySQL procedures copied from. Due to environmental constraints, the host uses the Windows environment, from a machine with a Linux environment. In addition MySQL version of the best consistent, MySQL5.7.22 version I used, the specific installation process please check the relevant information.

1. The basic principle of master-slave replication

slave synchronization data will be read from the master binlog. There are three main steps:
Master will record the change in the binary log (binary log), the recording process is called binary log events (binary log events).
The master slave copy of the binary log events to log the relay (relay log).
slave relay redo logs event that will change the application to its own database. MySQL replication is asynchronous and the serial.

 

 

 2. The master-slave replication rules

Each slave can have only one master. (One to one)

Each slave can have only one unique server ID.

Each master can have multiple slave. (One to many)

In the master-slave replication process, the biggest problem is the delay.

3. A common configuration of a master-slave

# 1 - Requirements.

MySQL version running in the background and is consistent with the best service. And to ensure that the host machine from ping each other. In the master-slave configuration are [mysqld] nodes, all lowercase.

# 2. Host modify configuration files my.ini

server-id = 1, the host server id. (have to)

You must enable binary files.

log-bin="E:\devSoft\mysql-5.7.22-winx64\data\mysql-bin"

After the configuration, restart the mysql service, see the following.

 

 

 Enable error log. (Optional)

log_error ="E:\devSoft\mysql-5.7.22-winx64\data\log\errorlog\log_error.log"

Root directory, data directory. (Optional)

#mysql installation root basedir = "E: \ devSoft \ mysql-5.7.22-winx64" #mysql data file location datadir = "E: \ devSoft \ mysql-5.7.22-winx64 \ data"

Temporary directory. (Optional)

tmpdir ="E:\devSoft\mysql-5.7.22-winx64\"

read-only = 0, it indicates that the host can read and write.

Can be set not need to copy the database. (Optional)

binlog-ignore-db=mysql

It can be set to be replicated database. (Optional)

binlog-do-db=databasename

# 3. Modify my.cnf configuration file from the machine

ID from the server. (have to)

Enable binary logging. (Optional)

# 4. Master and slave are off the firewall, in fact, can also be configured ip rules, but turn off the firewall faster.

# 5. Establish an account on a Windows host and licensed to slave.

. A first create an account on the host:

#% Represent any client can connect to grant all privileges on * * to slaveaccount (user name) @ "% (or designated ip)" identified by 'password you want to set' with grant option.;

. B then refresh authority table:

flush privileges;

. C then licensed to slave:

GRANT REPLICATION SLAVE ON *. * TO 'slaveaccount (user name created above)' @ 'slave database ip' IDENTIFIED BY 'password you want to set'

d. the use of step b command to refresh authority table again.

# 6. Status of master.

 

 

 File and Position these two fields is very important, File tells the slave need to copy the file from which, Position tell from start copying machine from which the location of the file, when configuring from the machine required to. After you do this, try not to operate the main server MySQL, prevent the main server status changes (File Position and status changes).

# 7. Host configuration required from the machine in Linux.

CHANGE MASTER TO MASTER_HOST = 'Host IP', MASTER_USER = 'salveaccount', MASTER_PASSWORD = 'Host Authorization password', MASTER_LOG_FILE = 'File name', MASTER_LOG_POS = Position numbers;

# 8. Start replication from the server.

start slave;

After starting the copy function, you need to see a master-slave replication configuration was successful.

 

 

 Note: Only when Slave_IO_Running: Yes and Slave_SQL_Running: Yes, both are Yes, when it successfully master-slave replication configurations.

# 9. For testing.

First, to establish a database on the host computer and insert the data.

 

 

 Whether there is a corresponding database in the view from the machine.

 

 

 

 

 

 By view the data from the machine, seen from the master configuration successful replication.

# 10. Stop copying function from the service.

stop slave;

4. Summary

# 1. Copy of the master-slave configuration, most of them on the host, pay attention to see how.

# 2. Here the master and slave firewalls are turned off in order to better demonstrate the actual production environment is generally not a windows and linux host machine from this case, and therefore should not turn off the firewall, configure the firewall but depending on the circumstances rule.

 

Guess you like

Origin www.cnblogs.com/weigy/p/12578587.html