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分析過去?
一:在主庫上建立用于複制的賬号并在從庫上連接配接測試
- [[email protected] ~]# 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)
- [[email protected] ~]# 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檔案如下,注意紅色字型部分,重新開機資料庫執行個體
- [[email protected] ~]# 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
- [[email protected] ~]# service mysqld restart
- Shutting down MySQL.[ OK ]
- Starting MySQL..[ OK ]
- [[email protected] ~]# 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檔案如下,注意紅色字型部分,重新開機資料庫執行個體
- [[email protected] ~]# 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
- [[email protected] ~]# service mysqld restart
- Shutting down MySQL..[ OK ]
- Starting MySQL..[ OK ]
四:在從庫上指定主庫的連接配接資訊,并開啟同步程序
- [[email protected] ~]# 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)