mysql主从复制

主从复制原理:

mysql 主从复制工作原理:master将每次进行的数据库操作记录到binlog文件中,slave通过读取master的binlog文件获得master进行哪些数据库操作,slave执行binlog文件中的相应操作,这样slave就和master的数据库就保持了同步。当master开启了binlog之后,master就会启动一个io线程负责将master进行的数据库操作写入binlog中。slave启动一个io线程负责从master读取binlog的内容,将读取的binlog内容写入到slave上的中继日志(relay_log)中,slave的sql线程负责执行relay_log中的操作。

配置:

master 配置:

1.修改mysql的配置文件,在配置文件中加入如下配置项,(之后重启mysql)

server-id=1 #server-id不一定是1,可以是其他的数字,只要保证唯一即可,可以使用ip地址的最后八位
log-bin=mysqlbinlog  #开始binlog,binlog文件命名为mysqlbinlog
binlog_format=fixed  #binlog格式
read-only=ON         #

binlog-do-db=mydata
#binlog-ignore-db=xxx
auto-increment-offset=1
auto-increment-increment=2

2.导出当前要进行复制的数据库

$ mysqldump -uroot -p mydata>>/root/mydata.sql

3.创建进行binlog操作的用户(slave 以此用户身份来读取master上面的binlog文件)

>>> create user 'replicator'@'%' identified by 'xxxx'
>>> grant replication slave on *.* to 'replicator'@'%' identified by 'xxxx'
>>> flush privileges

4.记录下master的binlog文件名(),和binlog当前位置

>>> show master status;
 binlog文件名          binlog当前位置
+--------------------+----------+--------------+------------------+
| File(binlog文件名)| Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mysqlbinlog.000003 |      520 | mydata       |                  |
+--------------------+----------+--------------+-----------------

5.至此master配置完成

slave配置:

1.修改mysql配置文件,加入如下配置项(重启mysql)

server-id=2
replicate-do-db=mydata

2.导出master的数据

>>> source /root/mydata.sql #或者mysql -uroot -p mydata<mydata.sql

3.slave中配置要读取的master binlog的信息

$ mysql -uroot -p
>>> change master to
    master_host='192.168.252.12', #master的ip
    master_user='replicator',  #master中的创建的用于读取binlog的用户名
    master_password='xxxxxx',
    master_log_file='mysqlbinlog.0000003',
    master_log_file=520;
>>> start slave;

4.查看slave是否配置成功

$ mysql -uroot -p
>>> show slave status;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.252.12
                  Master_User: replicator
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysqlbinlog.000003
          Read_Master_Log_Pos: 795
               Relay_Log_File: mariadb-relay-bin.000006
                Relay_Log_Pos: 1081
        Relay_Master_Log_File: mysqlbinlog.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: mydata
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 795
              Relay_Log_Space: 1663
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified
查看 Slave_IO_Running和Slave_SQL_Running 是否为Yes,为yes说明配置成功,为no说明slave没有配置成功。

注意事项:

1.进行配置时要注意iptable的规则的配置,否则slave连接master的时候连接不上。
2.master的server-id和slave的server-id不能相同

原文地址:https://www.cnblogs.com/whereareyoufrom/p/5240380.html