萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL主從復制

MySQL主從復制

   實驗環境:

  master and the slave server machine have the same configuration as followings:

  [root@master1 ~]# uname -a

  Linux master1 2.6.18-8.el5 #1 SMP Fri Jan 26 14:15:21 EST 2007 i686 i686 i386 GNU/Linux

  mysql> select @@version;

  +---------------------------------------+

  | @@version |

  +---------------------------------------+

  | 5.6.19-enterprise-commercial-advanced |

  +---------------------------------------+

  1 row in set (0.00 sec)

  master IP: 192.168.92.11

  slave IP: 192.168.92.111

  二,主從服務器分別作的事情

  Master sever:

  changes data

  keeps log of changes

  slave server:

  ask master for events

  executes events

  三,復制的類型(同步|異步|半同步)

  Synchronouse replication

  1,data is replicated and appllied then committed

  2,provides consistency ,but slower

  3,provided by MySQL Cluster

  Asynchronous replication

  1,transactions committed immediately and replicated

  2,no consistency,but faster

  3,provided by MySQL Server

  SemiSyncReplication

  1,provided by Google

  四,復制所需要的日志

  Binary log的作用:

  1,log every change (select 是不記錄的,只記錄改變的)

  2,split into transaction groups

  兩個復制相關的二進制文件:

  File: master_bin.NNNNNN

  1,The actual contents of the binlog

  File: master_bin.index

  1,an index file over the files above

  五,復制所用到的線程

  Master: I/O thread

  Slave: I/O thread and SQL Thread

  master.info contains:

  1,Read coordinates: which contains master log name and master log position

  2,Connection information: which contains the following two information:

  a,host,user,password ,port

  b,SSL keys and certificates

  relay-log.info contains:

  1,Group master coordinates: which contains master log name and master log position

  2,Group relay log coordinates: which contains relay log name and relay log position

  六,具體的實施步驟:

  The following 8 Steps are need to be taken to configure the master slave replication:

  1,Fix my.cnf file for master and slave

  2,add user and grants on master

  3,make sure the related configuration

  4,on the master,use the SHOW MASTER STATUS statement to determine the current binary log file name and position:

  5,load backup dump file into master

  6,configure slave

  7,start slave

  8,check slave status show slave statusG

  1,Fix my.cnf file for master and slave

  Master configuration --required(必選擇)

  log_bin = master_bin

  server_id =11

  配置好了後,重新啟動mysql服務

  [root@master1 ~]# cd /etc/rc.d/init.d/

  [root@master1 init.d]# service mysql restart

  Shutting down MySQL.. [ OK ]

  Starting MySQL......... [ OK ]

  slave configuration --required(必選擇)

  server_id=111 The master and slave must have the different server_id

  配置好了後,重新啟動mysql服務

  [root@slave1 init.d]# service mysql restart

  Shutting down MySQL.. [ OK ]

  Starting MySQL......... [ OK ]

  2,add user and grants on master

  mysql> CREATE USER 'repl'@'192.168.92.111' IDENTIFIED BY 'slavepass';

  mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.92.111';

  3,make sure the related configuration

  show variables like 'server%';

  show variables like 'log%';

  show grants for 'repl'@'192.168.92.111';

  mysql> show variables like 'server%';

  +----------------+--------------------------------------+

  | Variable_name | Value |

  +----------------+--------------------------------------+

  | server_id | 11 |

  | server_id_bits | 32 |

  | server_uuid | 303c6931-0d5e-11e4-9f5c-000c29f09a2c |

  +----------------+--------------------------------------+

  3 rows in set (0.00 sec)

  show variables like 'log%'; 看log_bin是否開啟用

  mysql> show variables like 'log%';

  +----------------------------------------+---------------------------------+

  | Variable_name | Value |

  +----------------------------------------+---------------------------------+

  | log_bin | ON |

  | log_bin_basename | /var/lib/mysql/master_bin |

  | log_bin_index | /var/lib/mysql/master_bin.index |

  | log_bin_trust_function_creators | OFF |

  | log_bin_use_v1_row_events | OFF |

  | log_error | /var/lib/mysql/master1.err |

  | log_output | FILE |

  | log_queries_not_using_indexes | OFF |

  | log_slave_updates | OFF |

  | log_slow_admin_statements | OFF |

  | log_slow_slave_statements | OFF |

  | log_throttle_queries_not_using_indexes | 0 |

  | log_warnings | 1 |

  +----------------------------------------+---------------------------------+

  13 rows in set (0.00 sec)

  mysql> show grants for 'repl'@'192.168.92.111';

  +------------------------------------------------------------------------------------------------------------------------------+

  | Grants for [email protected] |

  +------------------------------------------------------------------------------------------------------------------------------+

  | GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.92.111' IDENTIFIED BY PASSWORD '*809534247D21AC735802078139D8A854F45C31F3' |

  +------------------------------------------------------------------------------------------------------------------------------+

  1 row in set (0.00 sec)

  4,on the master,use the SHOW MASTER STATUS statement to determine the current binary log file name and position:

  mysql> show master statusG

  *************************** 1. row ***************************

  File: master_bin.000001

  Position: 589

  Binlog_Do_DB:

  Binlog_Ignore_DB:

  Executed_Gtid_Set:

  1 row in set (0.00 sec)

  5,load backup dump file into master

  一定要先創建一個數據庫

  mysql> create database cddl;

  Query OK, 1 row affected (0.02 sec)

  還原數據庫到master上:

  mysql -h 192.168.92.11 -uroot -ppassword cddl< /mysql_installer/cddl20140702.sql

  6,configure slave

  CHANGE MASTER TO

  MASTER_HOST='192.168.92.11',

  MASTER_USER='repl',

  MASTER_PASSWORD='slavepass',

  MASTER_PORT=3306,

  MASTER_LOG_FILE='master_bin.000001',

  MASTER_LOG_POS=589,

  MASTER_CONNECT_RETRY=10;

  7,start slave

  mysql> start slave;

  Query OK, 0 rows affected (0.25 sec)

  8,check slave status

  mysql> show slave statusG;

  *************************** 1. row ***************************

  Slave_IO_State: Waiting for master to send event

  Master_Host: 192.168.92.11

  Master_User: repl

  Master_Port: 3306

  Connect_Retry: 10

  Master_Log_File: master_bin.000002

  Read_Master_Log_Pos: 120

  Relay_Log_File: slave1-relay-bin.000002

  Relay_Log_Pos: 43341241

  Relay_Master_Log_File: master_bin.000

copyright © 萬盛學電腦網 all rights reserved