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

二:修改主库my.cnf文件如下,注意红色字体部分,重启数据库实例

 
 
  1. [root@dg53 ~]# 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_size16M 
  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.  
  41. [root@dg53 ~]# service mysqld restart  
  42. Shutting down MySQL.[  OK  ]  
  43. Starting MySQL..[  OK  ]  
  44.  
  45. [root@dg53 ~]# mysql  
  46. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  47. Your MySQL connection id is 1  
  48. Server version: 5.5.25-log Source distribution  
  49.  
  50. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  51.  
  52. Oracle is a registered trademark of Oracle Corporation and/or its  
  53. affiliates. Other names may be trademarks of their respective  
  54. owners.  
  55.  
  56. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  57.  
  58. mysql> show master status;  
  59. +------------------+----------+--------------+------------------+  
  60. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  61. +------------------+----------+--------------+------------------+  
  62. | mysql-bin.000011 |      107 | bbs,test     | mysql            |  
  63. +------------------+----------+--------------+------------------+  
  64. 1 row in set (0.00 sec) 

三:修改从库my.cnf文件如下,注意红色字体部分,重启数据库实例

 
 
  1. [root@dg54 ~]# 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_size16M 
  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.  
  40. [root@dg54 ~]# service mysqld restart  
  41. Shutting down MySQL..[  OK  ]  
  42. Starting MySQL..[  OK  ] 

四:在从库上指定主库的连接信息,并开启同步进程

 
 
  1. [root@dg54 ~]# 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.  
  6. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.  
  7.  
  8. Oracle is a registered trademark of Oracle Corporation and/or its  
  9. affiliates. Other names may be trademarks of their respective  
  10. owners.  
  11.  
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  13.  
  14. mysql> change master to master_host='192.168.123.13',master_user='r_test',master_password='123456';  
  15. Query OK, 0 rows affected (0.07 sec)  
  16.  
  17. mysql> slave start;  
  18. Query OK, 0 rows affected, 1 warning (0.00 sec)  
  19.  
  20. mysql> show slave status \G;  
  21. *************************** 1. row ***************************  
  22.                Slave_IO_State: Waiting for master to send event  
  23.                   Master_Host: 192.168.123.13  
  24.                   Master_User: r_test  
  25.                   Master_Port: 3306  
  26.                 Connect_Retry: 60  
  27.               Master_Log_File: mysql-bin.000011  
  28.           Read_Master_Log_Pos: 107  
  29.                Relay_Log_File: dg54-relay-bin.000013  
  30.                 Relay_Log_Pos: 253  
  31.         Relay_Master_Log_File: mysql-bin.000011  
  32.              Slave_IO_Running: Yes  
  33.             Slave_SQL_Running: Yes  
  34.               Replicate_Do_DB: test,bbs  
  35.           Replicate_Ignore_DB:   
  36.            Replicate_Do_Table:   
  37.        Replicate_Ignore_Table:   
  38.       Replicate_Wild_Do_Table:   
  39.   Replicate_Wild_Ignore_Table:   
  40.                    Last_Errno: 0  
  41.                    Last_Error:   
  42.                  Skip_Counter: 0  
  43.           Exec_Master_Log_Pos: 107  
  44.               Relay_Log_Space: 554  
  45.               Until_Condition: None  
  46.                Until_Log_File:   
  47.                 Until_Log_Pos: 0  
  48.            Master_SSL_Allowed: No  
  49.            Master_SSL_CA_File:   
  50.            Master_SSL_CA_Path:   
  51.               Master_SSL_Cert:   
  52.             Master_SSL_Cipher:   
  53.                Master_SSL_Key:   
  54.         Seconds_Behind_Master: 0  
  55. Master_SSL_Verify_Server_Cert: No  
  56.                 Last_IO_Errno: 0  
  57.                 Last_IO_Error:   
  58.                Last_SQL_Errno: 0  
  59.                Last_SQL_Error:   
  60.   Replicate_Ignore_Server_Ids:   
  61.              Master_Server_Id: 1  
  62. 1 row in set (0.00 sec)  
  63.  
  64. ERROR:   
  65. 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.  
  13. mysql> use bbs;  
  14. Database changed  
  15. mysql> show tables;  
  16. +---------------+  
  17. | Tables_in_bbs |  
  18. +---------------+  
  19. | user          |  
  20. +---------------+  
  21. 1 row in set (0.00 sec)  
  22.  
  23. mysql> select count(*) from user;  
  24. +----------+  
  25. | count(*) |  
  26. +----------+  
  27. |      768 |  
  28. +----------+  
  29. 1 row in set (0.01 sec) 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值