mysql主备复制搭建(使用mysqld_muti)


   这里我使用单台服务器上的两个mysql实例进行搭建,主要用到了mysql自带的mysqld_multi

 

一、复制原理

开始搭建前有个mysql复制原理的基础知识需要补充:

mysql进行主备复制使用到了三个线程:

1.主库上的转存储线程:

    会将mysql server提交的事务写入到二进制文件中,这个二进制文件就叫做binlog。

2.备库上的连接线程:

    备库启动后,负责和主库通信,读取binlog,同时,将binlog存储进自己的一个叫中继日志的relaylog中。

3.备库上的relaylog重放线程:

   此线程会将relaylog中的事件在备库上进行回放,说白点就是重新执行一次

 

二、搭建步骤

1./etc新增文件mysqld_multi.cnf

将/user/share/mysql/my-innodb-heavy-4G.cnf文件复制到/etc,重新命名为xxx.cnf(任何你想要的名字)

在配置文件中新增三个实例


[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = multi_admin
password = multipass
log = /var/log/mysqld_multi.log

[mysqld3307]
port = 3307
pid-file = /var/lib/mysql3307/mysql3307.pid
socket = /var/lib/mysql3307/mysql3307.sock
datadir=/var/lib/mysql3307
user=mysql
set-variable=max_connections=27000
log_bin = mysql-bin
server_id = 3307

[mysqld3308]
port = 3308
pid-file = /var/lib/mysql3308/mysql3308.pid
socket = /var/lib/mysql3308/mysql3308.sock
datadir=/var/lib/mysql3308
user=mysql
set-variable=max_connections=28000
log_bin = mysql-bin
server_id = 3308
relay_log = /var/lib/mysql3308/mysql-relay-bin
log_slave_updates = 1
read_only = 1

[mysqld3309]
port = 3309
pid-file = /var/lib/mysql3309/mysql3309.pid
socket = /var/lib/mysql3309/mysql3309.sock
datadir=/var/lib/mysql3309
user=mysql
set-variable=max_connections=29000
log_bin = mysql-bin
server_id = 3309
relay_log = /var/lib/mysql3309/mysql-relay-bin
log_slave_updates = 1
read_only = 1



 这里我会将将mysqld3307这个实例做为主库,mysqld3308和mysql3309这个实例作为备库

 

 

2.做好了配置之后开启两个实例:

mysqld_multi --defaults-file=/etc/mysql/mysqld_muti.cnf start

 

3.开启复制前主库的准备工作:

   1.在主库上增加一个复制账号:

    使用sock文件登陆mysql:

       mysql -uroot -p -S /var/lib/mysql3307/mysql3307.sock(这个套接字文件还记得吗,是在之前定义实例的时候定义的)


mysql>grant replication slave,replication client on *.* to replication@‘localhost‘ identified by ‘replication‘

mysql>flush privileges;


  查看主库上的binlog是否开启:



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


 

4.开启复制:

登陆到备库:

mysql -uroot -p -S /var/lib/mysql3308/mysql3308.sock

 


  1. change master to master_host = "localhost",  

  2. master_user = ‘replication‘,  

  3. master_password = ‘replication‘,(你之前在主库上创建复制账号时指定的)  

  4. master_port = 3306;  

 

start slave;

 

查看复制是否开始工作:


mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: localhost
                  Master_User: replication
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 622
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 767
        Relay_Master_Log_File: mysql-bin.000001
             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: 622
              Relay_Log_Space: 922
              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:
1 row in set (0.00 sec)


 当看到Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes证明io通信线程和sql回放线程都已经启动。至此,主备复制结构配置完成


5.进行正常主从测试:

在mysql3308数据库停止复制


mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G;           
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: localhost
                  Master_User: replication
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 408
               Relay_Log_File: mysql-relay-bin.000012
                Relay_Log_Pos: 553
        Relay_Master_Log_File: mysql-bin.000005



在mysql3309数据库停止复制


mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: localhost
                  Master_User: replication
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 316



查看mysql日志情况:


150510  1:33:39 [Note] Error reading relay log event: slave SQL thread was killed
150510  1:33:39 [Note] Slave I/O thread killed while reading event
150510  1:33:39 [Note] Slave I/O thread exiting, read up to log ‘mysql-bin.000005‘, position 408


150510  1:35:41 [Note] Error reading relay log event: slave SQL thread was killed
150510  1:35:41 [Note] Slave I/O thread killed while reading event
150510  1:35:41 [Note] Slave I/O thread exiting, read up to log ‘mysql-bin.000006‘, position 316



     在这期间,主库mysql3307进行了flush logs操作,重新生成了mysql-bin日志,并对表进行添加,删除操作。然后启动从库的复制,进行查看。



150510  1:42:48 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000005‘ at position 408, relay log ‘/var/lib/mysql3308/mysql-relay-bin.000012‘ position: 553
150510  1:42:48 [Note] Slave I/O thread: connected to master ‘replication@localhost:3307‘,replication started in log ‘mysql-bin.000005‘ at position 408


150510  1:43:04 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000006‘ at position 316, relay log ‘/var/lib/mysql3309/mysql-relay-bin.000015‘ position: 461
150510  1:43:04 [Note] Slave I/O thread: connected to master ‘replication@localhost:3307‘,replication started in log ‘mysql-bin.000006‘ at position 316


    说明:从库从停止的时间点重新补回了停止期间的所有数据。


6.进行异常测试:

      停掉从库mysql3308和mysql3309的复制,并对主库进行日志清除操作:


flush logs

delete from sky where id=‘2000‘;

purge binary logs to ‘mysql-bin.000010‘;


     然后重新启动从库的复制,日志进行报错,数据丢失,主从失败。


150510  1:50:53 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file‘, Error_code: 1236


解决方法:

     只能停止从数据库,然后重新在从数据库中CHANGE MASTER TO 开始,指向正确的二进制文件及偏移量


change master  to  master_log_file=‘mysql-bin.000001‘,master_log_pos=106;


     之后的数据会恢复正常主从同步。

本文出自 “秋天的童话” 博客,请务必保留此出处http://wushank.blog.51cto.com/3489095/1649929

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。