mysql 主從複製

基礎知識:
需求:
最初只有主數據庫,這樣即使有數據備份,對業務保證也不夠高(業務不大,不需要做主主)。所以新增一臺數據庫備用服務器,做主從複製。
準備:
在master數據庫服務器的操作
添加用於複製的用戶
grant replication client,replication slave on *.* to 'angus'@'%.%.%.%' identified by 'password';
修改server  id (只要主從server id不一樣就行,可以再從服務器該。)
開啓binary logging(一般都開啓過)
 
在slaver數據庫服務器的操作
修改server id
開啓relay-log
開啓binary logging(主從切換 做準備)
 
操作:
1,在從服務器上配置好數據庫,最後和主數據庫是一個版本。
2,sed -i '/^server-id/s#1#2#g' /etc/my.cnf
3,開啓relay-log 在/etc/my.cnf 的[mysqld]區域添加下面代碼
relay-log=relay-bin
relay-log=relay-bin.index
sed -i '/^server-id/a\relay-log=relay-bin\nrelay-log=relay-bin.index' /etc/my.cnf
4,對主數據進行一次完全備份
[root@localhost ~]# mysql -uroot    -p
Enter password:    
Welcome to the MySQL monitor.    Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.66 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> flush tables with read lock;
Query OK, 0 rows affected (0.02 sec)

mysql> quit
Bye
[root@localhost ~]# mysqldump -hlocalhost -uroot -p --all-databases --single-transaction --master-data=2 > /root/all.sql
Enter password:
[root@localhost ~]# ls
all.sql    anaconda-ks.cfg     install.log    install.log.syslog    Music    Pictures    Public    Templates    Videos    yum.sh
[root@localhost ~]# mysql -uroot -p
Enter password:    
Welcome to the MySQL monitor.    Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.66-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> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@localhost ~]#
把all.sql 傳送到從服務器上,並導入到mysql服務器上。
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                                                                     [    OK    ]
Starting mysqld:                                                                                     [    OK    ]
[root@localhost ~]# mysql < all.sql    
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                                                                     [    OK    ]
Starting mysqld:                                                                                     [    OK    ]
[root@localhost ~]# mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.61-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 |
| info.angus.com    |
| know.angus.com    |
| mysql              |
| test               |
+--------------------+
7 rows in set (0.02 sec)
設置從數據庫複製數據的主數據(MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=342這些內容可以在less all.sql中找到-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106;)
mysql> change master to master_host='x.x.x.x',master_user='angus',master_password='password',MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=342;
Query OK, 0 rows affected (0.03 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW SLAVE STATUS\G
如果沒有報錯,此時主從複製基本上都成功了。
 
主從切換:
由於各種原因,住數據庫掛了,並且段時間內無法修復,爲了不影響,線上服務,需要及時做主從切換。
[root@localhost ~]# mysql -p
Enter password:    
Welcome to the MySQL monitor.    Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.61-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> STOP SLAVE IO_THREAD;    //確保從服務器已經處理了中繼日誌中的所有語句
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW PROCESSLIST;         //檢查SHOW PROCESSLIST語句的輸出,直到你看到Has read all relay log
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User                | Host            | db     | Command | Time | State                                                                                                                                 | Info                         |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
|    5 | system user |                     | NULL | Connect | 2349 | Has read all relay log; waiting for the slave I/O thread to update it | NULL                         |
|    8 | root                | localhost | NULL | Query     |        0 | NULL                                                                                                                                    | SHOW PROCESSLIST |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> RESET MASTER;
Query OK, 0 rows affected (0.08 sec)

mysql> RESET SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                                                                     [    OK    ]
Starting mysqld:                                                                                     [    OK    ]
此時 從服務器升級爲主服務器,一般情況下從服務的性能沒有主服務器的好,所以當主服務器正常後,首先進入服務找到故障前一刻master狀態。
mysql> show master status;
+------------------+----------+--------------+------------------+
| File                         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |            106 |                            |                                    |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
然後讓原來的主服務器做從服務器(開啓中繼日誌),複製數據(首先的有執行復制權限的用戶如angus1)
mysql> change master to master_host='x.x.x.x',master_user='angus1',master_password='password',MASTER_LOG_FILE='mysql-bin.000002 , MASTER_LOG_POS=106;    
Query OK, 0 rows affected (0.03 sec)    
等所有數據複製完成後,再做次主從切換。
 
附:
mysql對數據庫過濾:有兩種形式,一般使用下面一種。
複製過濾器
binlog-do-db = mydb 表示只限制與mydb的修改操作纔會記錄到二進制日誌
binlog-inore-db = mydb

中繼日誌 過濾
replicate-do-db = mydb
replicate-ignore-db = mydb
replicate-do-table = table
replicate-ignore-table = table
replicate-wild-do-table = db_pattern.tb1_pattern
replicate-wild-ignore-table = db_pattern.tb1_pattern

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章