General talk about MySQL (5)

5. Master-slave replication

Rationale for Replication

slave will read binlog from master for data synchronization

Three steps + schematic diagram

The MySQL replication process is divided into three steps

  1. The master records changes to the binary log (binary log). These recording processes are called binary log events, binary log events;
  2. The slave copies the master's binary log events to its relay log;
  3. The slave redoes the events in the relay log, applying the changes to its own database. MySQL replication is asynchronous and serialized

Basic Principles of Replication

  • Each slave has only one master
  • Each slave can only have one unique server ID
  • Each master can have multiple slaves

The biggest problem with replication is latency .

Common configuration of one master and one slave

  • The version of mysql is the same and it runs as a service in the background

  • Both the master and the slave are configured under the [mysqld] node, both in lowercase

The host modifies the my.ini configuration file

  1. [Required] The unique ID of the main server

    1. server-id=1
  2. [must] enable binary logging

    1. log-bin=your own local path/mysqlbin
    2. log-bin=D:/devSoft/MySQLServer5.5/data/mysqlbin
  3. [Optional] Enable error logging

    1. log-err=your own local path/mysqlerr
    2. log-err=D:/devSoft/MySQLServer5.5/data/mysqlerr
  4. [optional] root directory

    1. basedir="your own local path"
    2. basedir=“D:/devSoft/MySQLServer5.5/”
  5. [optional] temporary directory

    1. tmpdir="your own local path"
    2. tmpdir=“D:/devSoft/MySQLServer5.5/”
  6. [optional] data directory

    1. datadir="your own local path /Data/"
    2. datadir=“D:/devSoft/MySQLServer5.5/Data/”
  7. Host, can read and write

    1. read-only=O
  8. [Optional] Set databases not to be replicated

    1. binlog-ignore-db=mysql
  9. [Optional] Set the database to be replicated

    1. binlog-do-db=The name of the primary database that needs to be replicated

Modify the my.cnf configuration file from the machine

  • [Required] Unique ID of the slave server
  • [Optional] Enable binary logging

Configuration file, please restart the background mysql service on both the host and slave

  • service mysql stop
  • service mysql start

Both the host and the slave close the firewall

  • windows closed manually

  • Turn off the virtual machine linux firewall

    View firewall status

    firewall-cmd --state

    stop firewall

    systemctl stop firewalld.service

    Prohibit firewall from booting

    systemctl disable firewalld.service

Create an account on the Windows host and authorize the slave

  • GRANT REPLICATION SLAVE ON *.* TO 'zhangsan'@'slave machine database IP' IDENTIFIED BY '123456';
  • flush privileges;//refresh
  • Query the status of the master
    • show master status;
    • Record the value of File and Position

  • Do not operate the main server MYSQL after performing this step to prevent the status value of the main server from changing

Configure the host to be copied on the Linux slave

  • CHANGE MASTER TO MASTER_HOST='host IP', MASTER_USER='zhangsan', MASTER_PASSWORD='123456', MASTER_LOG_FILE='File name', MASTER_LOG_POS=Position number;

  • Start the replication function from the server

    • start slave;
  • show slave status\G

    • The following two parameters are both Yes, indicating that the master-slave configuration is successful!
    • Slave_IO_Running:Yes
    • Slave_SQL_Running:Yes

The host creates a new database, creates a new table, inserts records, and replicates from the slave

How to stop copy function from service

  • stop slave;

Guess you like

Origin blog.csdn.net/m0_60961651/article/details/132272857