一個master對應2個slave 異步複製

二.搭建一個master對應2個slave
mysql5.5複製配置

1.規劃網絡和主從機器
master:10.10.54.64
slave:10.10.54.67
slave:10.10.54.63
2.master config

log-bin=master-bin
server-id=1
binlog_formate=mixed

[root@gyf  ~]# /etc/init.d/mysqld restart

3.

slave config10.10.54.67
[root@gyf  ~]# vim /etc/my.cnf
log-bin=slave-bin
binlog_formate=mixed
server-id=10必須大於主
[root@gyf  ~]# /etc/init.d/mysqld restart

slave config10.10.54.63
log-bin=slave-bin
binlog_formate=mixed
server-id=11必須大於主
[root@gyf  ~]# /etc/init.d/mysqld restart

4.在master上面建立一個複製用戶並授予權限
mysql> grant replication slave on *.* to 'gyf'@'10.10.54.67' identified by 'aaa12345';
mysql> grant replication slave on *.* to 'gyf'@'10.10.54.63' identified by 'aaa12345';
mysql> flush privileges;
//在從上測試是否能用複製用戶登陸
[root@gyf  ~]# mysql -ugyf -paaa12345 -h10.10.54.64

5.查看master上二進制日誌和position位置
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      751 |              |                  |
+-------------------+----------+--------------+------------------+

CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=751;
拓展:重置master  reset master
6.備份master上的數據,把備份maste數據庫還原到從庫上
[root@gyf  ~]# mysqldump -uroot -paaa12345 --databases employees |mysql -uroot -paaa12345 -h10.10.54.67
[root@gyf  ~]# mysqldump -uroot -paaa12345 --databases master |mysql -uroot -paaa12345 -h10.10.54.67

或者////
[root@nan86 tmp]# mysqldump -uroot -paaa12345 --master-data=2 --single-transaction --flush-logs --database employees >employees.sql

[root@gyf  ~]# mysqldump -uroot -paaa12345 --databases employees |mysql -uroot -paaa12345 -h10.10.54.67

[root@gyf  tmp]# mysql -uroot -paaa12345 -h10.10.54.67<employees.sql
ERROR 1130 (HY000): Host '10.10.54.64' is not allowed to connect to this MySQL server

mysql> grant all privileges on *.*  to root@10.10.54.64 identified by 'aaa12345';
mysql> flush privileges;

[root@nan86 tmp]# mysql -uroot -paaa12345 -h10.10.54.67<employees.sql
/////
7.在slave上面change master操做
mysql> change master to master_host='10.10.54.64',master_user='gyf',
    master_password='aaa12345', master_log_file='master-bin.000001',master_log_pos=751;
8.在slave上啓動slave


mysql> start slave;  
9.查看slave狀態
肯定slave上的I/O線程和SQL線程狀態爲YES
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.54.64
                  Master_User: gyf
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 751
               Relay_Log_File: gyf-relay-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

//能夠單獨停掉某一個線程
mysql> STOP SLAVE IO_THREAD;
mysql> STOP SLAVE SQL_THREAD;





###測試
在master上
mysql> create database aa;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| a                  |
| aa                 |
| employees          |
| master             |
| mysql              |
| performance_schema |
| test               |
| tt                 |
+--------------------+


在slave上顯示
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| a                  |
| aa                 |
| employees          |
| master             |
| mysql              |
| performance_schema |
| test               |
| tt                 |
+--------------------+


6 rows in set (0.00 sec)
在master上建立刪除添加數據,在slave上都能同步。

mysql

相關文章
相關標籤/搜索