MySQL的读写分离---主从复制、主主复制

1.复制是基于BinLog日志

存在三种日志格式:
Statement:存储Sql语句,存储日志量是最小的。有可能复制不一致
Row:存储event数据,存储日志量大,但是不能很直接进行读取;
Mixed:介于Row和statment之间

2.复制的基础

一、主从复制

1.原理

     mysql要做到主从复制,其实依靠的是二进制日志,即:假设主服务器叫A,从服务器叫B;主从复制就是

     B跟着A学,A做什么,B就做什么。那么B怎么同步A的动作呢?现在A有一个日志功能,把自己所做的增删改查的动作

     全都记录在日志中,B只需要拿到这份日志,照着日志上面的动作施加到自己身上就可以了。这样就实现了主从复制。

2.主从服务器分别作以下操作:

  1.1、版本一致
  1.2、初始化表,并在后台启动mysql
  1.3、修改root的密码

3.修改MySQL的配置文件

我使用了阿里云服务器(主)和本地虚拟主机(从)进行的配置:

vim my.conf

配置完成后,service  mysqld  restart 重启服务

同样的,进入从服务器,配置从服务器的my.cnf,重复步骤1即可,

唯一的区别是,server-id要改成从服务器的ip尾位,即server-id=12;其他两项是一样的,保存,并重启mySQL;

 2. 在主服务器上为从服务器分配一个账号,就像一把钥匙,从服务器拿着这个钥匙,才能到主服务器上来共享主服务器的日志文件。

进入主服务器的mysql界面,

 

3.查看主服务器的bin日志的信息(执行完之后记录下这两值,然后在配置完从服务器之前不要对主服务器进行任何操作,因为每次操作数据库时这两值会发生改变)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.设置从服务器

进入从服务器mysql 

命令:mysql -u root

关闭slave(如果你以前配置过主从的话,一定要先关闭)

命令:stop slave;

配置:

mysql> change master to
    -> master_host='139.224.112.219',
    -> MASTER_USER="root",
    -> MASTER_PASSWORD='密码',
    -> master_log_file='mysql-bin.000001',  #参考资料没有加点,在我这里保错了,点必须加上
    -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

参数解释:MASTER_HOST  :  设置要连接的主服务器的ip地址

       MASTER_USER  :  设置要连接的主服务器的用户名

       MASTER_PASSWORD  :  设置要连接的主服务器的密码

       MASTER_LOG_FILE  :  设置要连接的主服务器的bin日志的日志名称,即第3步得到的信息

       MASTER_LOG_POS  :  设置要连接的主服务器的bin日志的记录位置,即第3步得到的信息,(这里注意,最后一项不需要加引号。否则配置失败)

开启重服务器

start slave;

5.查看是否配置成功

mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 139.224.112.219
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes  #上面两项均为yes,说明配置成功,否则,请重复前面的步骤。
              Replicate_Do_DB:
          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: 120
              Relay_Log_Space: 460
              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: 219
                  Master_UUID: 1ea79143-9526-11e7-a4c4-00163e002bbc
             Master_Info_File: /usr/local/mysql/data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)



我测试,在主服务器上,成功。

 

原文地址:https://www.cnblogs.com/myvic/p/7746653.html