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)
本文出自 “斩月” 博客,谢绝转载!