MySQL主從介紹、準備工作、配置主、配置從、測試主從同步

來源:互聯網
上載者:User

標籤:mysql主從複製

MySQL主從介紹

    MySQL主從又叫做Replication、AB複製。簡單講就是A和B兩台機器做主從後,在A上寫資料,另外一台B也會跟著寫資料,兩者資料即時同步。MySQL主從是基於binlog的,主上須開啟binlog才能進行主從。

主從過程:

需要保持資料的一致性,才能複製資料

1、主伺服器將更改操作記錄到binlog中

2、從伺服器將主伺服器的binlog事件(SQL語句)同步到本機(從伺服器)並記錄在relaylog(中繼日誌)中

3、從伺服器根據relaylog裡面的SQL語句按順序執行

說明: 該過程有三個線程,主上有一個log dump線程,用來和從的i/o線程傳輸binlog;從上有兩個線程,其中i/o線程用來同步主的binlog並產生relaylog,另外一個SQL線程用來把relaylog裡面的SQL語句執行。

650) this.width=650;" src="https://s2.51cto.com/oss/201711/22/481561cedd4edc4138e9f4a38fe699a2.png-wh_500x0-wm_3-wmp_4-s_3073073493.png" title="1.png" alt="481561cedd4edc4138e9f4a38fe699a2.png-wh_" />


應用的情境:

1、備份重要的資料

2、分擔主程式庫伺服器的讀的壓力,讓從伺服器讀,主伺服器寫操作


準備工作

主伺服器:192.168.3.74  安裝mysql5.6

從伺服器:192.168.3.83   安裝mysql5.6


配置主

1、[[email protected] system]# vi /etc/my.cnf

[mysqld]


server-id=132

log_bin=jacklinux


2、重啟:

[[email protected] system]# /etc/init.d/mysqld restart

Shutting down MySQL.. SUCCESS! 

Starting MySQL.. SUCCESS! 

[[email protected] system]# ls -lt /data/mysql/  #二進位檔案,不能查看

-rw-rw---- 1 mysql mysql       19 Nov 22 09:33 jacklinux.index    

-rw-rw---- 1 mysql mysql      120 Nov 22 09:33 jacklinux.000001


3、建立一個資料庫為實驗做準備:

[[email protected] mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql

[[email protected] mysql]# mysql -uroot -p123456 -e "create database jacktest"

[[email protected] mysql]# mysql -uroot -p123456 jacktest < /tmp/zrlog.sql


4、建立同步帳號

mysql>  grant replication slave on *.* to ‘repl‘@‘192.168.3.83‘ identified by ‘123456‘;

#只給複製許可權,對所有庫所有表,ip為從伺服器的ip,帳號為repl,密碼:123456

Query OK, 0 rows affected (0.00 sec)


mysql> flush tables with read lock;

#先進行鎖表,等主從伺服器資料保持一致,再解鎖表

Query OK, 0 rows affected (0.00 sec)


mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| jacklinux.000001 |    10989 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

#需要記錄file、position位置



配置從

1、[[email protected] ~]# vim /etc/my.cnf

[mysqld]

server-id=130

#必須得和主伺服器上的server-id不一樣

2、[[email protected] ~]# scp 192.168.3.74:/tmp/*.sql .

3、mysql> create database zrlog;

4、恢複資料庫

[[email protected] ~]# mysql -uroot zrlog < zrlog.sql

實現主從同步:

mysql> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host=‘192.168.3.74‘,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘jacklinux.000001‘,master_log_pos=10989;

#master_log_file、master_log_pos:主伺服器上

mysql> start slave;

mysql> show slave status\G

   Slave_IO_Running: Yes

   Slave_SQL_Running: Yes

5、解鎖主庫的表(在主上操作):

mysql> unlock tables;

此時主從複製搭建完成,下一步驗證。


測試主從同步

主伺服器:

binlog-do-db=   僅同步指定的庫

binlog-ignore-db=    忽略指定的庫


從伺服器:

replicate_do_db=    同步指定的庫

replicate_ignore_db=  忽略指定的庫

replicate_do_table=    同步指定的表

replicate_ignore_table=   忽略指定的表


replicate_wild_do_table=  如aming.%,支援萬用字元    

replicate_wild_ignore_table=  忽略

驗證:

主伺服器上建立表zrtest,並插入資料

mysql> use zrlog

mysql> create table zrtest(id int);

Query OK, 0 rows affected (0.02 sec)


mysql> desc zrtest;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| id    | int(11) | YES  |     | NULL    |       |

+-------+---------+------+-----+---------+-------+

1 row in set (0.01 sec)


mysql> insert zrtest value(1);

Query OK, 1 row affected (0.01 sec)


mysql> insert zrtest value(20);

Query OK, 1 row affected (0.00 sec)


mysql> select * from zrtest;

+------+

| id   |

+------+

|    1 |

|   20 |

+------+


從伺服器上查看是否有zrtest表,還有資料

mysql> use zrlog;

mysql> select * from zrtest;

+------+

| id   |

+------+

|    1 |

|   20 |

+------+


驗證完成,可以同步

本文出自 “探索發現新事物” 部落格,請務必保留此出處http://jacksoner.blog.51cto.com/5802843/1983983

MySQL主從介紹、準備工作、配置主、配置從、測試主從同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.