mysql主从复制

一、主从复制的原理

主服务器数据库的每次操作都会记录在其二进制文件mysql-bin.xxx(该文件可以在mysql目录下的data目录中看到)中,从服务器的I/O线程使用专用账号登录到主服务器中读取该二进制文件,并将文件内容写入到自己本地的中继日志relay-log文件中,然后从服务器的SQL线程会根据中继日志中的内容执行SQL语句。

二、主从复制目的

主从服务器设置的稳健性得以提升,如果主服务器发生故障,可以把本来作为备份的从服务器提升为新的主服务器。在主从服务器上分开处理用户的请求,读的话,可以直接读取备机数据,可获得更短的响应时间。

三、主从复制的作用

1、可以作为备份机制,相当于热备份

2、可以用来做读写分离,均衡数据库负载

四、架构环境

主服务器:192.168.1.35,mysql已经安装
从服务器:192.168.1.36,mysql已经安装

五、配置

1、主从复制配置:

修改主服务器的配置文件/etc/my.cnf
log-bin=mysql-bin    //启用二进制日志
server-id       = 35    //服务器唯一ID,一般取IP最后一段
修改从服务器的配置文件/etc/my.cnf
log-bin=mysql-bin    //启用二进制日志
server-id       = 36    //服务器唯一ID,一般取IP最后一段
重启主从服务器的数据库
# /etc/init.d/mysqld restart

六、主服务器上操作

1、创建用户并授权

#mysql -uroot -p
mysql> grant replication slave on *.* to root@192.168.1.36 identified by '123456';
mysql> flush privileges;

2、查询主数据库的状态

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

记录下File以及Position的值,在后面从服务器操作的时候需要用到

七、从服务器上操作

1、mysql登录并执行同步的sql语句,启动slave

#mysql -uroot -p
mysql>change master to master_host='192.168.1.35',master_user='root',master_password='123456',master_log_file='mysql-bin.000002',masterster_log_pos=550;
mysql> start slave;

2、主从同步检查

mysql> show slave statusG 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.35
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 550
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: 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: 550
              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: 35
                  Master_UUID: 946b699a-eaad-11e7-927d-000c292b387b
             Master_Info_File: /alidata/server/mysql/data/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)

来检查从服务器的同步情况,一个正常的输出结果应该如下面的形式:
Slave_IO进程以及slave_SQL进程都必须正常运行,在状态输出中表现为:“slave”;否则都是不正确的状态(如一个值是Yes,另一个是No则不行)。

八、如果主数据库服务器已经存在用户数据,那么在进行主从复制时,需要做以下处理:

1、主数据库锁表操作,不让数据再进行写入动作。

mysql>flush tables withread lock; 

2、查看主数据库的状态

mysql>show master status;

记下File以及Position的值,以备从服务器使用。

3、把主服务器的数据文件复制到从服务器,最好先用tar归档压缩处理一下

4、取消主数据库锁定

mysql>unlock tables;

从服务器的操作和前面的步骤一样,略过。

七、测试是否实现主从复制

在主数据库服务器上创建库和表,然后插入记录,再登录到从服务器,查看是否也建立相一致的库和表以及记录。

1、主服务器上的操作

mysql> create database hhh;
mysql> CREATE TABLE IF NOT EXISTS `runoob_tbl`(
    ->    `runoob_id` INT UNSIGNED AUTO_INCREMENT,
    ->    `runoob_title` VARCHAR(100) NOT NULL,
    ->    `runoob_author` VARCHAR(40) NOT NULL,
    ->    `submission_date` DATE,
    ->    PRIMARY KEY ( `runoob_id` )
    -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;

(2)从服务器上可以看到

 

原文地址:https://www.cnblogs.com/guantou1992/p/9729394.html