天天看點

Mysql5.5配置主從複制

Mysql提供了主從複制的功能,作用類似oracle的dataguard,但是配置和管理遠比dataguard簡單,沒有所謂的實體備庫和邏輯備庫之分,也沒有提供相應的資料保護模式,隻有master和slave資料庫角色,這種架構廣泛應用于各大門戶,遊戲網站中,提供資料庫的讀寫分離功能;相比之下oracle的讀寫功能到了11g版本才能借助active dataguard完美實作,否則就隻能用logical standby,但又有許多的資料類型和操作不被邏輯備庫支援!先前使用過mysql5.1.36版本的master-slave架構做CMS資料庫的讀寫分離,有着非常痛苦的使用經曆,經常由于各種各樣的原因的導緻主從資料不同步,然後又沒有提供額外的同步機制(确切的說是沒學會),于是經常在下班時間重構主從架構;下一步将在mysql5.5平台測試mha架構,故本文記錄下如何在mysql5.5平台下建構主從資料庫複制環境;另外mysql的主從也可看為是mysql的實時備份,有一定的資料災備功能,mysql本身沒有提供類似oracle rman的熱備份工具,因而多數場景下會使用主從對資料庫做一個實時備份,以備不時隻需!PS:一直比較不解的mysql如何做基于時間或者SCN的不完全恢複?難道真要一個個binlog分析過去?

一:在主庫上建立用于複制的賬号并在從庫上連接配接測試

  1. [[email protected] ~]# mysql  
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  3. Your MySQL connection id is 1  
  4. Server version: 5.5.25-log Source distribution  
  5. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  6. Oracle is a registered trademark of Oracle Corporation and/or its  
  7. affiliates. Other names may be trademarks of their respective  
  8. owners.  
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  10. mysql> grant replication slave on *.* to 'r_test'@'192.168.123.14' identified by '123456';  
  11. Query OK, 0 rows affected (0.00 sec)  
  12. [[email protected] ~]# mysql -u r_test -h 192.168.123.13 -p  
  13. Enter password:   
  14. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  15. Your MySQL connection id is 2  
  16. Server version: 5.5.25-log Source distribution  
  17. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  18. Oracle is a registered trademark of Oracle Corporation and/or its  
  19. affiliates. Other names may be trademarks of their respective  
  20. owners.  
  21. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  22. mysql> show databases;  
  23. +--------------------+  
  24. | Database           |  
  25. +--------------------+  
  26. | information_schema |  
  27. | test               |  
  28. +--------------------+  
  29. 2 rows in set (0.01 sec) 

二:修改主庫my.cnf檔案如下,注意紅色字型部分,重新開機資料庫執行個體

  1. [[email protected] ~]# grep -v '^#' /etc/my.cnf |grep -v '^$'  
  2. [client]  
  3. port            = 3306 
  4. socket          = /tmp/mysql.sock  
  5. [mysqld]  
  6. port            = 3306 
  7. socket          = /tmp/mysql.sock  
  8. skip-external-locking  
  9. key_buffer_size = 256M 
  10. max_allowed_packet = 1M 
  11. table_open_cache = 256 
  12. sort_buffer_size = 1M 
  13. read_buffer_size = 1M 
  14. read_rnd_buffer_size = 4M 
  15. myisam_sort_buffer_size = 64M 
  16. thread_cache_size = 8 
  17. query_cache_size= 16M 
  18. thread_concurrency = 8 
  19. innodb_data_home_dir =  
  20. innodb_data_file_path = /dev/raw/raw6:1Graw  
  21. log-bin=mysql-bin  
  22. binlog_format=mixed 
  23. server-id=1 
  24. log-bin=mysql-bin  
  25. binlog-do-db=bbs 
  26. binlog-do-db=test 
  27. binlog-ignore-db=mysql 
  28. [mysqldump]  
  29. quick  
  30. max_allowed_packet = 16M 
  31. [mysql]  
  32. no-auto-rehash  
  33. [myisamchk]  
  34. key_buffer_size = 128M 
  35. sort_buffer_size = 128M 
  36. read_buffer = 2M 
  37. write_buffer = 2M 
  38. [mysqlhotcopy]  
  39. interactive-timeout  
  40. [[email protected] ~]# service mysqld restart  
  41. Shutting down MySQL.[  OK  ]  
  42. Starting MySQL..[  OK  ]  
  43. [[email protected] ~]# mysql  
  44. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  45. Your MySQL connection id is 1  
  46. Server version: 5.5.25-log Source distribution  
  47. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  48. Oracle is a registered trademark of Oracle Corporation and/or its  
  49. affiliates. Other names may be trademarks of their respective  
  50. owners.  
  51. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  52. mysql> show master status;  
  53. +------------------+----------+--------------+------------------+  
  54. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  55. +------------------+----------+--------------+------------------+  
  56. | mysql-bin.000011 |      107 | bbs,test     | mysql            |  
  57. +------------------+----------+--------------+------------------+  
  58. 1 row in set (0.00 sec) 

三:修改從庫my.cnf檔案如下,注意紅色字型部分,重新開機資料庫執行個體

  1. [[email protected] ~]# grep -v '^#' /etc/my.cnf |grep -v '^$'  
  2. [client]  
  3. port            = 3306 
  4. socket          = /tmp/mysql.sock  
  5. [mysqld]  
  6. port            = 3306 
  7. socket          = /tmp/mysql.sock  
  8. skip-external-locking  
  9. key_buffer_size = 256M 
  10. max_allowed_packet = 1M 
  11. table_open_cache = 256 
  12. sort_buffer_size = 1M 
  13. read_buffer_size = 1M 
  14. read_rnd_buffer_size = 4M 
  15. myisam_sort_buffer_size = 64M 
  16. thread_cache_size = 8 
  17. query_cache_size= 16M 
  18. thread_concurrency = 8 
  19. innodb_data_home_dir =  
  20. innodb_data_file_path = /dev/raw/raw6:1Graw  
  21. log-bin=mysql-bin  
  22. binlog_format=mixed 
  23. server-id       = 2 
  24. log-bin=mysql-bin  
  25. replicate-do-db=test   
  26. replicate-do-db=bbs 
  27. [mysqldump]  
  28. quick  
  29. max_allowed_packet = 16M 
  30. [mysql]  
  31. no-auto-rehash  
  32. [myisamchk]  
  33. key_buffer_size = 128M 
  34. sort_buffer_size = 128M 
  35. read_buffer = 2M 
  36. write_buffer = 2M 
  37. [mysqlhotcopy]  
  38. interactive-timeout  
  39. [[email protected] ~]# service mysqld restart  
  40. Shutting down MySQL..[  OK  ]  
  41. Starting MySQL..[  OK  ] 

四:在從庫上指定主庫的連接配接資訊,并開啟同步程序

  1. [[email protected] ~]# mysql  
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  3. Your MySQL connection id is 1  
  4. Server version: 5.5.25-log Source distribution  
  5. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  6. Oracle is a registered trademark of Oracle Corporation and/or its  
  7. affiliates. Other names may be trademarks of their respective  
  8. owners.  
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  10. mysql> change master to master_host='192.168.123.13',master_user='r_test',master_password='123456';  
  11. Query OK, 0 rows affected (0.07 sec)  
  12. mysql> slave start;  
  13. Query OK, 0 rows affected, 1 warning (0.00 sec)  
  14. mysql> show slave status \G;  
  15. *************************** 1. row ***************************  
  16.                Slave_IO_State: Waiting for master to send event  
  17.                   Master_Host: 192.168.123.13  
  18.                   Master_User: r_test  
  19.                   Master_Port: 3306  
  20.                 Connect_Retry: 60  
  21.               Master_Log_File: mysql-bin.000011  
  22.           Read_Master_Log_Pos: 107  
  23.                Relay_Log_File: dg54-relay-bin.000013  
  24.                 Relay_Log_Pos: 253  
  25.         Relay_Master_Log_File: mysql-bin.000011  
  26.              Slave_IO_Running: Yes  
  27.             Slave_SQL_Running: Yes  
  28.               Replicate_Do_DB: test,bbs  
  29.           Replicate_Ignore_DB:   
  30.            Replicate_Do_Table:   
  31.        Replicate_Ignore_Table:   
  32.       Replicate_Wild_Do_Table:   
  33.   Replicate_Wild_Ignore_Table:   
  34.                    Last_Errno: 0  
  35.                    Last_Error:   
  36.                  Skip_Counter: 0  
  37.           Exec_Master_Log_Pos: 107  
  38.               Relay_Log_Space: 554  
  39.               Until_Condition: None  
  40.                Until_Log_File:   
  41.                 Until_Log_Pos: 0  
  42.            Master_SSL_Allowed: No  
  43.            Master_SSL_CA_File:   
  44.            Master_SSL_CA_Path:   
  45.               Master_SSL_Cert:   
  46.             Master_SSL_Cipher:   
  47.                Master_SSL_Key:   
  48.         Seconds_Behind_Master: 0  
  49. Master_SSL_Verify_Server_Cert: No  
  50.                 Last_IO_Errno: 0  
  51.                 Last_IO_Error:   
  52.                Last_SQL_Errno: 0  
  53.                Last_SQL_Error:   
  54.   Replicate_Ignore_Server_Ids:   
  55.              Master_Server_Id: 1  
  56. 1 row in set (0.00 sec)  
  57. ERROR:   
  58. No query specified 

五:驗證資料同步結果,在5.1.36版本下,這裡還需要在從庫建相應的資料庫,導入表資料

  1. mysql> show databases;  
  2. +--------------------+  
  3. | Database           |  
  4. +--------------------+  
  5. | information_schema |  
  6. | bbs                |  
  7. | mysql              |  
  8. | performance_schema |  
  9. | test               |  
  10. +--------------------+  
  11. 5 rows in set (0.02 sec)  
  12. mysql> use bbs;  
  13. Database changed  
  14. mysql> show tables;  
  15. +---------------+  
  16. | Tables_in_bbs |  
  17. +---------------+  
  18. | user          |  
  19. +---------------+  
  20. 1 row in set (0.00 sec)  
  21. mysql> select count(*) from user;  
  22. +----------+  
  23. | count(*) |  
  24. +----------+  
  25. |      768 |  
  26. +----------+  
  27. 1 row in set (0.01 sec)