设为首页 收藏本站
查看: 609|回复: 0

[经验分享] Mysql5.5配置主从复制

[复制链接]
YunVN网友  发表于 2018-9-28 10:42:35 |阅读模式
  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分析过去?
  一:在主库上创建用于复制的账号并在从库上连接测试
  


  • [root@dg53 ~]# mysql
  • Welcome to the MySQL monitor.  Commands end with ; or \g.
  • Your MySQL connection id is 1
  • Server version: 5.5.25-log Source distribution

  • Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

  • Oracle is a registered trademark of Oracle Corporation and/or its
  • affiliates. Other names may be trademarks of their respective
  • owners.

  • Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  • mysql> grant replication slave on *.* to 'r_test'@'192.168.123.14' identified by '123456';
  • Query OK, 0 rows affected (0.00 sec)

  • [root@dg54 ~]# mysql -u r_test -h 192.168.123.13 -p
  • Enter password:
  • Welcome to the MySQL monitor.  Commands end with ; or \g.
  • Your MySQL connection id is 2
  • Server version: 5.5.25-log Source distribution

  • Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

  • Oracle is a registered trademark of Oracle Corporation and/or its
  • affiliates. Other names may be trademarks of their respective
  • owners.

  • Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  • mysql> show databases;
  • +--------------------+
  • | Database           |
  • +--------------------+
  • | information_schema |
  • | test               |
  • +--------------------+
  • 2 rows in set (0.01 sec)
  

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


  • [root@dg53 ~]# grep -v '^#' /etc/my.cnf |grep -v '^$'
  • [client]
  • port            = 3306
  • socket          = /tmp/mysql.sock
  • [mysqld]
  • port            = 3306
  • socket          = /tmp/mysql.sock
  • skip-external-locking
  • key_buffer_size = 256M
  • max_allowed_packet = 1M
  • table_open_cache = 256
  • sort_buffer_size = 1M
  • read_buffer_size = 1M
  • read_rnd_buffer_size = 4M
  • myisam_sort_buffer_size = 64M
  • thread_cache_size = 8
  • query_cache_size= 16M
  • thread_concurrency = 8
  • innodb_data_home_dir =
  • innodb_data_file_path = /dev/raw/raw6:1Graw
  • log-bin=mysql-bin
  • binlog_format=mixed
  • server-id=1
  • log-bin=mysql-bin
  • binlog-do-db=bbs
  • binlog-do-db=test
  • binlog-ignore-db=mysql
  • [mysqldump]
  • quick
  • max_allowed_packet = 16M
  • [mysql]
  • no-auto-rehash
  • [myisamchk]
  • key_buffer_size = 128M
  • sort_buffer_size = 128M
  • read_buffer = 2M
  • write_buffer = 2M
  • [mysqlhotcopy]
  • interactive-timeout

  • [root@dg53 ~]# service mysqld restart
  • Shutting down MySQL.[  OK  ]
  • Starting MySQL..[  OK  ]

  • [root@dg53 ~]# mysql
  • Welcome to the MySQL monitor.  Commands end with ; or \g.
  • Your MySQL connection id is 1
  • Server version: 5.5.25-log Source distribution

  • Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

  • Oracle is a registered trademark of Oracle Corporation and/or its
  • affiliates. Other names may be trademarks of their respective
  • owners.

  • Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  • mysql> show master status;
  • +------------------+----------+--------------+------------------+
  • | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  • +------------------+----------+--------------+------------------+
  • | mysql-bin.000011 |      107 | bbs,test     | mysql            |
  • +------------------+----------+--------------+------------------+
  • 1 row in set (0.00 sec)
  

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


  • [root@dg54 ~]# grep -v '^#' /etc/my.cnf |grep -v '^$'
  • [client]
  • port            = 3306
  • socket          = /tmp/mysql.sock
  • [mysqld]
  • port            = 3306
  • socket          = /tmp/mysql.sock
  • skip-external-locking
  • key_buffer_size = 256M
  • max_allowed_packet = 1M
  • table_open_cache = 256
  • sort_buffer_size = 1M
  • read_buffer_size = 1M
  • read_rnd_buffer_size = 4M
  • myisam_sort_buffer_size = 64M
  • thread_cache_size = 8
  • query_cache_size= 16M
  • thread_concurrency = 8
  • innodb_data_home_dir =
  • innodb_data_file_path = /dev/raw/raw6:1Graw
  • log-bin=mysql-bin
  • binlog_format=mixed
  • server-id       = 2
  • log-bin=mysql-bin
  • replicate-do-db=test
  • replicate-do-db=bbs
  • [mysqldump]
  • quick
  • max_allowed_packet = 16M
  • [mysql]
  • no-auto-rehash
  • [myisamchk]
  • key_buffer_size = 128M
  • sort_buffer_size = 128M
  • read_buffer = 2M
  • write_buffer = 2M
  • [mysqlhotcopy]
  • interactive-timeout

  • [root@dg54 ~]# service mysqld restart
  • Shutting down MySQL..[  OK  ]
  • Starting MySQL..[  OK  ]
  

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


  • [root@dg54 ~]# mysql
  • Welcome to the MySQL monitor.  Commands end with ; or \g.
  • Your MySQL connection id is 1
  • Server version: 5.5.25-log Source distribution

  • Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

  • Oracle is a registered trademark of Oracle Corporation and/or its
  • affiliates. Other names may be trademarks of their respective
  • owners.

  • Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  • mysql> change master to master_host='192.168.123.13',master_user='r_test',master_password='123456';
  • Query OK, 0 rows affected (0.07 sec)

  • mysql> slave start;
  • Query OK, 0 rows affected, 1 warning (0.00 sec)

  • mysql> show slave status \G;
  • *************************** 1. row ***************************
  •                Slave_IO_State: Waiting for master to send event
  •                   Master_Host: 192.168.123.13
  •                   Master_User: r_test
  •                   Master_Port: 3306
  •                 Connect_Retry: 60
  •               Master_Log_File: mysql-bin.000011
  •           Read_Master_Log_Pos: 107
  •                Relay_Log_File: dg54-relay-bin.000013
  •                 Relay_Log_Pos: 253
  •         Relay_Master_Log_File: mysql-bin.000011
  •              Slave_IO_Running: Yes
  •             Slave_SQL_Running: Yes
  •               Replicate_Do_DB: test,bbs
  •           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: 107
  •               Relay_Log_Space: 554
  •               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: 1
  • 1 row in set (0.00 sec)

  • ERROR:
  • No query specified
  

  五:验证数据同步结果,在5.1.36版本下,这里还需要在从库建相应的数据库,导入表数据
  


  • mysql> show databases;
  • +--------------------+
  • | Database           |
  • +--------------------+
  • | information_schema |
  • | bbs                |
  • | mysql              |
  • | performance_schema |
  • | test               |
  • +--------------------+
  • 5 rows in set (0.02 sec)

  • mysql> use bbs;
  • Database changed
  • mysql> show tables;
  • +---------------+
  • | Tables_in_bbs |
  • +---------------+
  • | user          |
  • +---------------+
  • 1 row in set (0.00 sec)

  • mysql> select count(*) from user;
  • +----------+
  • | count(*) |
  • +----------+
  • |      768 |
  • +----------+
  • 1 row in set (0.01 sec)
  




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-603170-1-1.html 上篇帖子: crontab来定时执行备份mysql 下篇帖子: MySQL使用快照备份
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表