Realize mysql's master-master (master-slave) replication under windows

  • The mysql server allows one server to be set as the master server, and multiple slave database servers are allowed to copy data from one mysql database server to one or more mysql database servers
  • mysql master-slave is the process of asynchronous replication
  • What is supporting this master-slave replication at the bottom layer?

       Answer: master opens the bin-log function, the log file is used to record the read and write additions and deletions of the database

A total of three threads are required in the master-slave configuration:

  • master: IO thread: 

Any modification of the main server above will be saved in the binary log through its own IO thread bin-log

  • Slave on: IO thread: 

The slave server will also start an IO Thread. Connect to the main server to request the binary log reading through the configured user name and password, and then store the read content in a local Realy log (relay log)

  • Slave on: SQL thread:

Open a SQL Theard from the server at the same time. Check the Realy log regularly. If any updates are found, immediately execute the updated content on the local database.

How the three threads work:

         The slave opens the IO thread and the sql thread. The slave is responsible for connecting to the master through the IO thread and requesting the content after a certain bin-log position. The master server receives the log request information sent by the slave IO thread, and the io thread combines the bin-log content with The position is returned to the slave IO thread

        The slave server receives the bin-log log content, writes the bin-log log content to the relay-log relay log, and creates a master.info file that records the master ip username and password master bin-log name, bin- log position.

         The slave side starts the SQL thread, monitors whether the relay-log log content is updated in real time, parses the SQL statement in the file, and executes it in the slave database.

Above:

 

Let's take a look at how to configure. Before configuring, we must ensure that mysql is installed on two servers.

The two servers I use here are

master: 10.55.185.80

slave: 10.55.185.81

first step:

The configuration mysql file my.ini explains:

配置说明:
#[必须]服务器唯一ID,每台服务器需不同
server-id = 1
#[必须]启用二进制文件
log-bin = /home/mysql/mysql-bin
#[不是必须]二进制文件启用混合模式
binlog_format = mixed
#[不是必须]二进制文件过期时间,单位是天
expire-logs-days = 14
#[不是必须]当每进行1次事务提交之后,MySQL将进行一次磁盘同步指令来将binlog_cache中的数据强制写入磁盘
sync-binlog = 1
#[不是必须]只将对应的数据库变动写入二进制文件。如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项
binlog-do-db = test,androidpnserver
#[必须]不需要记录二进制日志的数据库。如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项。一般为了保证主主同步不冲突,会忽略mysql数据库。
binlog-ignore-db = mysql,information_schema,performance_schema
#[必须]做主主备份的时候,因为每台数据库服务器都可能在同一个表中插入数据,如果表有一个自动增长的主键,那么就会在多服务器上出现主键冲突。
#解决这个问题的办法就是让每个数据库的自增主键不连续。上面两项说的是,假设需要将来可能需要10台服务器做备份,将auto-increment-increment设为10。而auto-increment-offset=1表示这台服务器的序号。从1开始,不超过auto-increment-increment。
auto-increment-increment = 10
auto-increment-offset = 1

 The content of the mysql configuration file of the main server:

[mysqld]
server-id=1
log-bin = mysql-bin
binlog-do-db = bd-ecxel
binlog-ignore-db = mysql,information_schema,performance_schema
auto-increment-increment = 10
auto-increment-offset = 2

The content of the mysql configuration file from the server:

[mysqld]
server-id=2
log-bin = mysql-bin
binlog-do-db = bd-ecxel
binlog-ignore-db = mysql,information_schema,performance_schema
auto-increment-increment = 10
auto-increment-offset = 2

Note: The server-id here must be unique

The second step:

First restart mysql:

#停止mysql服务
net stop mysql
#开启mysql服务
net start mysql
进入mysql
mysql -u root -p 

Note: Before the master-slave configuration of the database, make sure that the master-slave database is temporarily synchronized. You can unlock the master server's database first, and do not add, delete, or modify the database.

third step:

首先刷新,停止线程
mysql>stop slave;

mysql>reset slave;
刷新mysql的系统权限相关表
mysql>flush privileges;

 the fourth step:

Create a synchronization account on the master server: (If you are doing master-slave replication, then you only need to create a synchronization account on the master server

If you are doing master-master replication, then both master and slave need to create synchronization accounts )

grant replication slave on *.* to '<userName>'@'<hostIp>' identified by '<passWord>';`
​
# userName     用户名,默认root
# passWord     用户密码(均为数据库账户,密码)
# hostIp          需要同步的主机IP,可以写%,表示全部

Examples:

grant replication slave on *.* to 'root'@'%' identified by '123456';

 the fifth step:

Check the master status on the master server, record the binary file name   , and  remember that File and Position will be used on the slave

mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 73       | test         | manual,mysql     |
+------------------+----------+--------------+------------------+

The sixth step:

Set the slave library synchronization , pay attention to the synchronization point there, which is the | File| Position in the main library show master status information

(If it is a master-slave then only need to synchronize on the slave, if it is a master, then you need to perform this step) 


mysql> CHANGE MASTER TO
    ->     MASTER_HOST='10.55.185.80',
    ->     MASTER_USER='root',
    ->     MASTER_PASSWORD='123456',
    ->     MASTER_LOG_FILE='mysql-bin.000003',
    ->     MASTER_LOG_POS=73;

The host starts the slave synchronization thread

mysql>stop slave;
mysql>start slave;

The host view the slave status:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.55.185.80
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 11662
               Relay_Log_File: mysqld-relay-bin.000022
                Relay_Log_Pos: 11765
        Relay_Master_Log_File: mysql-bin.000013
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
        ...

This is the success of the two threads of the slave if both are yes!

Supplement: If there is an error when checking the status, then the cause of the error can be detected according to the error code,

There are two general error solutions:

The first: Ignore the error and continue to sync


stop slave;
#表示跳过一步错误,后面的数字可变
set global sql_slave_skip_counter =1;
start slave;
之后再用mysql> show slave status\G 查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
ok,现在主从同步状态正常了。

The second type: Specify skip error codes in the configuration file and continue to synchronize

主键冲突、表已存在等错误代码如1062,1032,1060等,可以在mysql主配置文件指定略过此类异常并继续下条sql同步,这样也可以避免很多主从同步的异常中断

[mysqld]

slave-skip-errors = 1062,1032,1060

重新启动mysql

service mysqld restart

之后再用mysql> show slave status\G 查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

 

Guess you like

Origin blog.csdn.net/qq_30631063/article/details/109250836