{"id":4145,"date":"2020-02-24T09:33:33","date_gmt":"2020-02-24T02:33:33","guid":{"rendered":"https:\/\/tino.vn\/?post_type=ht_kb&#038;p=4145"},"modified":"2020-02-24T10:26:56","modified_gmt":"2020-02-24T03:26:56","slug":"mariadb-advance-mariadb-replication","status":"publish","type":"ht_kb","link":"https:\/\/tino.vn\/blog\/docs\/mariadb-advance-mariadb-replication\/","title":{"rendered":"MariaDB Advance: MariaDB Replication"},"content":{"rendered":"<p>Replication l\u00e0 m\u1ed9t t\u00ednh n\u0103ng cho ph\u00e9p n\u1ed9i dung c\u1ee7a m\u1ed9t ho\u1eb7c nhi\u1ec1u m\u00e1y ch\u1ee7 (\u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 masters) \u0111\u01b0\u1ee3c nh\u00e2n \u0111\u00f4i tr\u00ean m\u1ed9t ho\u1eb7c nhi\u1ec1u m\u00e1y ch\u1ee7 (\u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 slaves).<\/p>\n<p>B\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m so\u00e1t d\u1eef li\u1ec7u n\u00e0o c\u1ea7n sao ch\u00e9p. T\u1ea5t c\u1ea3 c\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u, m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u01a1 s\u1edf d\u1eef li\u1ec7u ho\u1eb7c c\u00e1c b\u1ea3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u \u0111\u1ec1u c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c sao ch\u00e9p m\u1ed9t c\u00e1ch ch\u1ecdn l\u1ecdc.<\/p>\n<p>C\u01a1 ch\u1ebf ch\u00ednh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong nh\u00e2n r\u1ed9ng l\u00e0 binary log . N\u1ebfu binary log \u0111\u01b0\u1ee3c b\u1eadt, t\u1ea5t c\u1ea3 c\u00e1c c\u1eadp nh\u1eadt cho c\u01a1 s\u1edf d\u1eef li\u1ec7u (thao t\u00e1c d\u1eef li\u1ec7u v\u00e0 \u0111\u1ecbnh ngh\u0129a d\u1eef li\u1ec7u) \u0111\u01b0\u1ee3c ghi v\u00e0o binary log d\u01b0\u1edbi d\u1ea1ng binlog events. C\u00e1c Slaves \u0111\u1ecdc binary log t\u1eeb m\u1ed7i master truy c\u1eadp d\u1eef li\u1ec7u \u0111\u1ec3 sao ch\u00e9p. M\u1ed9t relay log \u0111\u01b0\u1ee3c t\u1ea1o ra tr\u00ean slave server, s\u1eed d\u1ee5ng \u0111\u1ecbnh d\u1ea1ng gi\u1ed1ng nh\u01b0 c\u00e1c binary log, v\u00e0 \u0111i\u1ec1u n\u00e0y \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n sao ch\u00e9p. C\u00e1c t\u1ec7p nh\u1eadt k\u00fd chuy\u1ec3n ti\u1ebfp c\u0169 \u0111\u01b0\u1ee3c g\u1ee1 b\u1ecf khi kh\u00f4ng c\u00f2n c\u1ea7n thi\u1ebft.<\/p>\n<p>M\u1ed9t slave server theo d\u00f5i v\u1ecb tr\u00ed trong binlog master&#8217;s binlog\u00a0 c\u1ee7a s\u1ef1 ki\u1ec7n cu\u1ed1i c\u00f9ng \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng tr\u00ean slave. \u0110i\u1ec1u n\u00e0y cho ph\u00e9p slave server k\u1ebft n\u1ed1i l\u1ea1i v\u00e0 ti\u1ebfp t\u1ee5c l\u1ea1i t\u1eeb n\u01a1i n\u00f3 d\u1eebng l\u1ea1i sau khi qu\u00e1 tr\u00ecnh sao ch\u00e9p t\u1ea1m th\u1eddi b\u1ecb d\u1eebng l\u1ea1i. N\u00f3 c\u0169ng cho ph\u00e9p m\u1ed9t slave ng\u1eaft k\u1ebft n\u1ed1i, \u0111\u01b0\u1ee3c nh\u00e2n b\u1ea3n v\u00e0 sau \u0111\u00f3 c\u00f3 b\u1ea3n sao ti\u1ebfp t\u1ee5c slave m\u1edbi t\u1eeb c\u00f9ng m\u1ed9t ch\u1ee7 master.<\/p>\n<p>Masters v\u00e0 slaves kh\u00f4ng c\u1ea7n ph\u1ea3i li\u00ean l\u1ea1c v\u1edbi nhau. Ho\u00e0n to\u00e0n c\u00f3 th\u1ec3 \u0111\u01b0a m\u00e1y ch\u1ee7 ngo\u1ea1i tuy\u1ebfn ho\u1eb7c ng\u1eaft k\u1ebft n\u1ed1i kh\u1ecfi m\u1ea1ng v\u00e0 khi ch\u00fang quay tr\u1edf l\u1ea1i, vi\u1ec7c sao ch\u00e9p s\u1ebd ti\u1ebfp t\u1ee5c \u1edf n\u01a1i n\u00f3 d\u1eebng l\u1ea1i.<\/p>\n<h2 id=\"Khi_n\u00e0o_th\u00ec_n\u00ean_d\u00f9ng_Replication\">Khi n\u00e0o th\u00ec n\u00ean d\u00f9ng Replication<\/h2>\n<p>Replication \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong m\u1ed9t s\u1ed1 t\u00ecnh hu\u1ed1ng ph\u1ed5 bi\u1ebfn. S\u1eed d\u1ee5ng bao g\u1ed3m:<\/p>\n<ul>\n<li>Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng: B\u1eb1ng c\u00e1ch c\u00f3 m\u1ed9t ho\u1eb7c nhi\u1ec1u m\u00e1y ch\u1ee7 slave, vi\u1ec7c \u0111\u1ecdc c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c tr\u1ea3i r\u1ed9ng tr\u00ean nhi\u1ec1u m\u00e1y ch\u1ee7, gi\u1ea3m t\u1ea3i cho master. K\u1ecbch b\u1ea3n ph\u1ed5 bi\u1ebfn nh\u1ea5t cho m\u00f4i tr\u01b0\u1eddng \u0111\u1ecdc cao, vi\u1ebft th\u1ea5p l\u00e0 c\u00f3 m\u1ed9t ch\u1ee7, trong \u0111\u00f3 t\u1ea5t c\u1ea3 c\u00e1c ghi x\u1ea3y ra, sao ch\u00e9p th\u00e0nh nhi\u1ec1u n\u00f4 l\u1ec7, x\u1eed l\u00fd h\u1ea7u h\u1ebft c\u00e1c l\u1ea7n \u0111\u1ecdc.<\/li>\n<li>Ph\u00e2n t\u00edch d\u1eef li\u1ec7u: Ph\u00e2n t\u00edch d\u1eef li\u1ec7u c\u00f3 th\u1ec3 c\u00f3 qu\u00e1 nhi\u1ec1u \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn m\u00e1y ch\u1ee7 master v\u00e0 \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c x\u1eed l\u00fd t\u01b0\u01a1ng t\u1ef1 tr\u00ean m\u00e1y ch\u1ee7 slave, trong khi master v\u1eabn ti\u1ebfp t\u1ee5c kh\u00f4ng b\u1ecb \u1ea3nh h\u01b0\u1edfng b\u1edfi t\u1ea3i th\u00eam.<\/li>\n<li>H\u1ed7 tr\u1ee3 sao l\u01b0u: <a href=\"https:\/\/mariadb.com\/kb\/en\/backing-up-and-restoring\/\" rel=\"nofollow noopener\" target=\"_blank\">Sao l\u01b0u<\/a> c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng ch\u1ea1y h\u01a1n n\u1ebfu m\u00e1y ch\u1ee7 kh\u00f4ng ch\u1ee7 \u0111\u1ed9ng thay \u0111\u1ed5i d\u1eef li\u1ec7u. M\u1ed9t k\u1ecbch b\u1ea3n ph\u1ed5 bi\u1ebfn l\u00e0 sao ch\u00e9p d\u1eef li\u1ec7u v\u1ec1 slave, sau \u0111\u00f3 b\u1ecb ng\u1eaft k\u1ebft n\u1ed1i v\u1edbi master v\u1edbi d\u1eef li\u1ec7u \u1edf tr\u1ea1ng th\u00e1i \u1ed5n \u0111\u1ecbnh. Sao l\u01b0u sau \u0111\u00f3 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n t\u1eeb m\u00e1y ch\u1ee7 n\u00e0y.<\/li>\n<li>Ph\u00e2n ph\u1ed1i d\u1eef li\u1ec7u: Thay v\u00ec \u0111\u01b0\u1ee3c k\u1ebft n\u1ed1i v\u1edbi m\u1ed9t master t\u1eeb xa, thay v\u00e0o \u0111\u00f3, c\u00f3 th\u1ec3 sao ch\u00e9p d\u1eef li\u1ec7u c\u1ee5c b\u1ed9 v\u00e0 l\u00e0m vi\u1ec7c t\u1eeb d\u1eef li\u1ec7u n\u00e0y.<\/li>\n<\/ul>\n<h2 id=\"C\u00e1c_m\u00f4_h\u00ecnh_th\u1ebft_l\u1eadp_Replication\">C\u00e1c m\u00f4 h\u00ecnh th\u1ebft l\u1eadp Replication<\/h2>\n<h3 id=\"Standard_Replication\">Standard Replication<\/h3>\n<p><img decoding=\"async\" title=\"standard_replication\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2020\/02\/img_5e5324abb55ad.png\" alt=\"standard_replication\" \/><\/p>\n<ul>\n<li>Cung c\u1ea5p quy m\u00f4 \u0111\u1ecdc v\u00f4 h\u1ea1n.<\/li>\n<li>Cung c\u1ea5p t\u00ednh s\u1eb5n s\u00e0ng cao b\u1eb1ng c\u00e1ch n\u00e2ng c\u1ea5p\u00a0 slave to master.<\/li>\n<\/ul>\n<h3 id=\"Ring_Replication\">Ring Replication<\/h3>\n<p><img decoding=\"async\" title=\"ring_replication\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2020\/02\/img_5e5324ad5b502.png\" alt=\"ring_replication\" \/><\/p>\n<ul>\n<li>Cung c\u1ea5p \u0111\u1ecdc v\u00e0 vi\u1ebft t\u1ef7 l\u1ec7.<\/li>\n<li>Kh\u00f4ng x\u1eed l\u00fd xung \u0111\u1ed9t.<\/li>\n<li>N\u1ebfu m\u1ed9t ch\u1ee7 th\u1ea5t b\u1ea1i, nh\u00e2n r\u1ed9ng d\u1eebng l\u1ea1i.<\/li>\n<\/ul>\n<h3 id=\"Star_Replication\">Star Replication<\/h3>\n<p><img decoding=\"async\" title=\"star_replication\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2020\/02\/img_5e5324af04a47.png\" alt=\"star_replication\" \/><\/p>\n<ul>\n<li>Cung c\u1ea5p \u0111\u1ecdc v\u00e0 vi\u1ebft t\u1ef7 l\u1ec7.<\/li>\n<li>Kh\u00f4ng x\u1eed l\u00fd xung \u0111\u1ed9t.<\/li>\n<li>Ph\u1ea3i s\u1eed d\u1ee5ng c\u00e1c b\u1ed9 l\u1ecdc sao ch\u00e9p \u0111\u1ec3 tr\u00e1nh tr\u00f9ng l\u1eb7p d\u1eef li\u1ec7u.<\/li>\n<\/ul>\n<h3 id=\"Multi-Source_Replication\">Multi-Source Replication<\/h3>\n<p><img decoding=\"async\" title=\"multi_source_replication\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2020\/02\/img_5e5324b09b5d1.png\" alt=\"multi_source_replication\" \/><\/p>\n<ul>\n<li>Cho ph\u00e9p b\u1ea1n k\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb c\u00e1c ngu\u1ed3n kh\u00e1c nhau.<\/li>\n<li>C\u00e1c mi\u1ec1n kh\u00e1c nhau \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n \u0111\u1ed9c l\u1eadp song song tr\u00ean t\u1ea5t c\u1ea3 c\u00e1c n\u00f4 l\u1ec7.<\/li>\n<\/ul>\n<h1>Thi\u1ebft l\u1eadp Replication<\/h1>\n<p>Ch\u00fa \u00fd l\u00e0 Replication n\u00ean ch\u1ec9 thi\u1ebft l\u1eadp khi\u00a0 master v\u00e0 slave ch\u1ea1y tr\u00ean c\u00f9ng m\u1ed9t phi\u00ean b\u1ea3n ho\u1eb7c phi\u00ean b\u1ea3n slave m\u1edbi h\u01a1n phi\u00ean b\u1ea3n master. Tr\u01b0\u1eddng h\u1ee3p m\u00e1y ch\u1ee7 master c\u00f3 phi\u00ean b\u1ea3n cao h\u01a1n m\u00e1y ch\u1ee7 slave th\u00ec Replication s\u1ebd kh\u00f4ng t\u01b0\u01a1ng th\u00edch.<\/p>\n<p><strong>Thi\u1ebft l\u1eadp tr\u00ean server master.<\/strong><\/p>\n<ul>\n<li>Cho ph\u00e9p ghi binary log n\u1ebfu n\u00f3 ch\u01b0a \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t<\/li>\n<li>Cung c\u1ea5p cho ch\u1ee7 master server_id duy nh\u1ea5t . T\u1ea5t c\u1ea3 slave c\u0169ng ph\u1ea3i \u0111\u01b0\u1ee3c cung c\u1ea5p m\u1ed9t server_id. \u0110\u00e2y c\u00f3 th\u1ec3 l\u00e0 m\u1ed9t s\u1ed1 t\u1eeb 1 \u0111\u1ebfn 2<sup>32<\/sup>-1 v\u00e0 ph\u1ea3i l\u00e0 duy nh\u1ea5t cho m\u1ed7i m\u00e1y ch\u1ee7 trong nh\u00f3m replicating.<\/li>\n<li>Ch\u1ec9 \u0111\u1ecbnh m\u1ed9t t\u00ean duy nh\u1ea5t cho nh\u1eadt k\u00fd replication v\u1edbi &#8211;log-basename . N\u1ebfu \u0111i\u1ec1u n\u00e0y kh\u00f4ng \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh, t\u00ean hostname m\u00e1y ch\u1ee7 c\u1ee7a b\u1ea1n s\u1ebd \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng v\u00e0 s\u1ebd c\u00f3 v\u1ea5n \u0111\u1ec1 n\u1ebfu t\u00ean m\u00e1y ch\u1ee7 thay \u0111\u1ed5i.<\/li>\n<li>C\u00e1c Slaves s\u1ebd c\u1ea7n s\u1ef1 cho ph\u00e9p \u0111\u1ec3 k\u1ebft n\u1ed1i v\u00e0 b\u1eaft \u0111\u1ea7u sao ch\u00e9p t\u1eeb m\u1ed9t m\u00e1y ch\u1ee7. Th\u00f4ng th\u01b0\u1eddng, \u0111i\u1ec1u n\u00e0y \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n b\u1eb1ng c\u00e1ch t\u1ea1o m\u1ed9t ng\u01b0\u1eddi d\u00f9ng slave chuy\u00ean d\u1ee5ng v\u00e0 ch\u1ec9 c\u1ea5p quy\u1ec1n cho ng\u01b0\u1eddi d\u00f9ng \u0111\u00f3 \u0111\u1ec3 sao ch\u00e9p (REPLICATION SLAVE permission).<\/li>\n<\/ul>\n<p>C\u00e1c l\u1ec7nh d\u01b0\u1edbi \u0111\u00e2y\u00a0 s\u1ebd thi\u1ebft l\u1eadp tr\u00ean m\u00e1y ch\u1ee7 Master, tr\u01b0\u1edbc ti\u00ean ta th\u00eam c\u00e1c c\u1ea5u h\u00ecnh sau v\u00e0o file my.cnf m\u00e1y ch\u1ee7 master<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[mariadb]\r\nlog-bin\r\nserver_id=1\r\nlog-basename=master1\r\n\r\n# c\u1ea5u h\u00ecnh d\u01b0\u1edbi \u0111\u00e2y cho ph\u00e9p c\u00e1c k\u1ebft n\u1ed1i ngo\u00e0i v\u00e0o server.\r\nskip-networking=0\r\nskip-bind-address<\/pre>\n<p>Sau khi restart l\u1ea1i MariaDB tr\u00ean m\u00e1y ch\u1ee7 master, ch\u00fang ta truy c\u1eadp MariaDB v\u00e0 kh\u1edfi t\u1ea1o user cho ph\u00e9p ch\u1ec9 \u0111\u1ecdc \u0111\u1ec3 ph\u1ee5c v\u1ee5 replication.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># kh\u1edfi t\u1ea1o user\r\nCREATE USER 'replication_user'@'%' IDENTIFIED BY 'M\u1eadt_kh\u1ea9u';\r\n# c\u1ea5p quy\u1ec1n REPLICATION cho user v\u1eeba t\u1ea1o \u1edf tr\u00ean\r\nGRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';\r\n#Flush quy\u1ec1n\r\nFLUSH PRIVILEGES;<\/pre>\n<p>C\u00f3 m\u1ed9t s\u1ed1 t\u00f9y ch\u1ecdn c\u00f3 th\u1ec3 \u1ea3nh h\u01b0\u1edfng ho\u1eb7c ph\u00e1 v\u1ee1 nh\u00e2n r\u1ed9ng.\u00a0Ki\u1ec3m tra c\u00e1c c\u00e0i \u0111\u1eb7t sau \u0111\u1ec3 tr\u00e1nh c\u00e1c v\u1ea5n \u0111\u1ec1.<\/p>\n<ul>\n<li><a href=\"https:\/\/mariadb.com\/kb\/en\/server-system-variables\/#skip_networking\" rel=\"nofollow noopener\" target=\"_blank\">skip-networking<\/a>. N\u1ebfu skip-networking=1 . m\u00e1y ch\u1ee7 s\u1ebd gi\u1edbi h\u1ea1n k\u1ebft n\u1ed1i ch\u1ec9 v\u1edbi localhost v\u00e0 ng\u0103n t\u1ea5t c\u1ea3 c\u00e1c slaves t\u1eeb xa k\u1ebft n\u1ed1i.<\/li>\n<li><a href=\"https:\/\/mariadb.com\/kb\/en\/server-system-variables\/#bind_address\" rel=\"nofollow noopener\" target=\"_blank\">bind-address<\/a>. T\u01b0\u01a1ng t\u1ef1, n\u1ebfu \u0111\u1ecba ch\u1ec9 m\u00e1y ch\u1ee7 l\u1eafng nghe c\u00e1c k\u1ebft n\u1ed1i TCP \/ IP l\u00e0 127.0.0.1 (localhost), c\u00e1c k\u1ebft n\u1ed1i slaves t\u1eeb xa s\u1ebd th\u1ea5t b\u1ea1i.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Thi\u1ebft l\u1eadp tr\u00ean server slave<\/strong><\/p>\n<p>Cung c\u1ea5p cho slave m\u1ed9t server_id duy nh\u1ea5t. T\u1ea5t c\u1ea3 c\u00e1c m\u00e1y ch\u1ee7, cho d\u00f9 l\u00e0 master hay slave, \u0111\u1ec1u \u0111\u01b0\u1ee3c cung c\u1ea5p server_id. \u0110\u00e2y c\u00f3 th\u1ec3 l\u00e0 m\u1ed9t s\u1ed1 t\u1eeb 1 \u0111\u1ebfn 2<sup>32<\/sup>-1 v\u00e0 ph\u1ea3i l\u00e0 duy nh\u1ea5t cho m\u1ed7i m\u00e1y ch\u1ee7 trong replication. M\u00e1y ch\u1ee7 s\u1ebd c\u1ea7n ph\u1ea3i \u0111\u01b0\u1ee3c kh\u1edfi \u0111\u1ed9ng l\u1ea1i \u0111\u1ec3 thay \u0111\u1ed5i trong t\u00f9y ch\u1ecdn n\u00e0y c\u00f3 hi\u1ec7u l\u1ef1c.<\/p>\n<p>Thi\u1ebft l\u1eadp trong file my.cnf c\u1ee7a m\u00e1y ch\u1ee7 slave<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[mariadb]\r\nserver-id = 2<\/pre>\n<p>Sau khi restart l\u1ea1i MariaDB\u00a0 l\u00e0 ch\u00fang ta \u0111\u00e3 kh\u1edfi t\u1ea1o slave ho\u00e0n t\u1ea5t. C\u00f4ng vi\u1ec7c ti\u1ebfp theo l\u00e0 ch\u00fang ta quay tr\u1edf l\u1ea1i m\u00e1y ch\u1ee7 master v\u00e0 l\u1ea5y \u0111\u01b0\u1ee3c t\u1ea1o \u0111\u1ed9 c\u1ee7a binary log .<\/p>\n<h2 id=\"L\u1ea5y_t\u1ecda_\u0111\u1ed9_Binary_Log_c\u1ee7a_Master\"><strong><span style=\"font-size: 12pt;\">L\u1ea5y t\u1ecda \u0111\u1ed9 Binary Log c\u1ee7a Master<\/span><\/strong><\/h2>\n<p>B\u00e2y gi\u1edd b\u1ea1n c\u1ea7n ng\u0103n ch\u1eb7n m\u1ecdi thay \u0111\u1ed5i \u0111\u1ed1i v\u1edbi d\u1eef li\u1ec7u trong khi b\u1ea1n xem v\u1ecb tr\u00ed binary log. B\u1ea1n s\u1ebd s\u1eed d\u1ee5ng \u0111i\u1ec1u n\u00e0y \u0111\u1ec3 n\u00f3i v\u1edbi slave ch\u00ednh x\u00e1c \u0111i\u1ec3m n\u00e0o s\u1ebd b\u1eaft \u0111\u1ea7u sao ch\u00e9p d\u1eef li\u1ec7u.<\/p>\n<ul>\n<li>Tr\u00ean master, flush and lock t\u1ea5t c\u1ea3 c\u00e1c b\u1ea3ng b\u1eb1ng c\u00e1ch ch\u1ea1y FLUSH TABLES WITH READ LOCK.<\/li>\n<li>L\u1ea5y v\u1ecb tr\u00ed hi\u1ec7n t\u1ea1i trong binary log b\u1eb1ng c\u00e1ch ch\u1ea1y SHOW MASTER STATUS:<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">MariaDB [(none)]&gt; FLUSH TABLES WITH READ LOCK;\r\nQuery OK, 0 rows affected (0.001 sec)\r\n\r\nMariaDB [(none)]&gt; SHOW MASTER STATUS;\r\n+--------------------+----------+--------------+------------------+\r\n| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |\r\n+--------------------+----------+--------------+------------------+\r\n| master1-bin.000003 |      344 |              |                  |\r\n+--------------------+----------+--------------+------------------+\r\n1 row in set (0.001 sec)<\/pre>\n<ul>\n<li>Ghi l\u1ea1i chi ti\u1ebft File\u00a0v\u00e0 Position<\/li>\n<li>B\u00e2y gi\u1edd, Khi v\u1eabn \u0111ang lock tables, ch\u00fang ta sao ch\u00e9p d\u1eef li\u1ec7u t\u1eeb master sang sang slave. Xem <a href=\"https:\/\/tino.vn\/blog\/docs\/backup-va-restore-database-tren-mariadb\/\">Sao l\u01b0u, Kh\u00f4i ph\u1ee5c<\/a>\u00a0\u0111\u1ec3 bi\u1ebft chi ti\u1ebft v\u1ec1 c\u00e1ch th\u1ef1c hi\u1ec7n vi\u1ec7c n\u00e0y.<\/li>\n<li>L\u01b0u \u00fd cho live database(database \u0111ang ph\u1ee5c v\u1ee5): B\u1ea1n ch\u1ec9 c\u1ea7n t\u1ea1o m\u1ed9t b\u1ea3n sao d\u1eef li\u1ec7u, b\u1ea1n kh\u00f4ng c\u1ea7n ph\u1ea3i lock tables cho \u0111\u1ebfn khi slave \u0111\u00e3 nh\u1eadp d\u1eef li\u1ec7u.<\/li>\n<li>Khi d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c sao ch\u00e9p, b\u1ea1n c\u00f3 th\u1ec3 gi\u1ea3i ph\u00f3ng lock tables tr\u00ean m\u00e1y ch\u1ee7 master b\u1eb1ng c\u00e1ch ch\u1ea1y <a href=\"https:\/\/mariadb.com\/kb\/en\/transactions-lock\/\" rel=\"nofollow noopener\" target=\"_blank\">UNLOCK TABLES<\/a>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># nh\u1edb ch\u1ea1y l\u1ec7nh d\u01b0\u1edbi \u0111\u00e2y sau khi database \u0111\u00e3 \u0111\u01b0\u1ee3c import tr\u00ean m\u00e1y ch\u1ee7 slave\r\nUNLOCK TABLES;<\/pre>\n<p>&nbsp;<\/li>\n<\/ul>\n<p><strong>B\u1eaft \u0111\u1ea7u slave<\/strong><\/p>\n<p>Khi d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c import, b\u1ea1n \u0111\u00e3 s\u1eb5n s\u00e0ng b\u1eaft \u0111\u1ea7u replication. B\u1eaft \u0111\u1ea7u b\u1eb1ng c\u00e1ch ch\u1ea1y \u00a0<a href=\"https:\/\/mariadb.com\/kb\/en\/change-master-to\/\" rel=\"nofollow noopener\" target=\"_blank\">CHANGE MASTER TO<\/a> , \u0111\u1ea3m b\u1ea3o r\u1eb1ng <em>MASTER_LOG_FILE<\/em> kh\u1edbp v\u1edbi t\u1ec7p v\u00e0 <em>MASTER_LOG_POS<\/em> v\u1ecb tr\u00ed \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 b\u1edfi SHOW MASTER STATUS tr\u01b0\u1edbc \u0111\u00f3. V\u00ed d\u1ee5:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">MariaDB [(none)]&gt; STOP SLAVE;\r\nMariaDB [(none)]&gt; CHANGE MASTER TO\r\n  MASTER_HOST='IP_server_master',\r\n  MASTER_USER='replication_user',\r\n  MASTER_PASSWORD='M\u1eadt _kh\u1ea9u_user_replication_\u0111\u00e3_t\u1ea1o_tr\u01b0\u1edbc_\u0111\u00f3',\r\n  MASTER_PORT=3306,\r\n  MASTER_LOG_FILE='master1-bin.000003',\r\n  MASTER_LOG_POS=344,\r\n  MASTER_CONNECT_RETRY=10;<\/pre>\n<p>Sau khi thi\u1ebft l\u1eadp ho\u00e0n t\u1ea5t, b\u1ea1n c\u00f3 th\u1ec3 start slave v\u00e0 ki\u1ec3m tra replication<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">MariaDB [(none)]&gt; START SLAVE;\r\nQuery OK, 0 rows affected (0.000 sec)\r\n\r\nMariaDB [(none)]&gt; SHOW SLAVE STATUS \\G\r\n*************************** 1. row ***************************\r\n                Slave_IO_State: Waiting for master to send event\r\n                   Master_Host: 103.130.218.7\r\n                   Master_User: replication_user\r\n                   Master_Port: 3306\r\n                 Connect_Retry: 10\r\n               Master_Log_File: master1-bin.000003\r\n           Read_Master_Log_Pos: 477\r\n                Relay_Log_File: slave-relay-bin.000002\r\n                 Relay_Log_Pos: 690\r\n         Relay_Master_Log_File: master1-bin.000003\r\n              Slave_IO_Running: Yes\r\n             Slave_SQL_Running: Yes\r\n               Replicate_Do_DB:\r\n           Replicate_Ignore_DB:\r\n            Replicate_Do_Table:\r\n        Replicate_Ignore_Table:\r\n       Replicate_Wild_Do_Table:\r\n   Replicate_Wild_Ignore_Table:\r\n                    Last_Errno: 0\r\n                    Last_Error:\r\n                  Skip_Counter: 0\r\n           Exec_Master_Log_Pos: 477\r\n               Relay_Log_Space: 999\r\n               Until_Condition: None\r\n                Until_Log_File:\r\n                 Until_Log_Pos: 0\r\n            Master_SSL_Allowed: No\r\n            Master_SSL_CA_File:\r\n            Master_SSL_CA_Path:\r\n               Master_SSL_Cert:\r\n             Master_SSL_Cipher:\r\n                Master_SSL_Key:\r\n         Seconds_Behind_Master: 0\r\n Master_SSL_Verify_Server_Cert: No\r\n                 Last_IO_Errno: 0\r\n                 Last_IO_Error:\r\n                Last_SQL_Errno: 0\r\n                Last_SQL_Error:\r\n   Replicate_Ignore_Server_Ids:\r\n              Master_Server_Id: 1\r\n                Master_SSL_Crl:\r\n            Master_SSL_Crlpath:\r\n                    Using_Gtid: No\r\n                   Gtid_IO_Pos:\r\n       Replicate_Do_Domain_Ids:\r\n   Replicate_Ignore_Domain_Ids:\r\n                 Parallel_Mode: conservative\r\n                     SQL_Delay: 0\r\n           SQL_Remaining_Delay: NULL\r\n       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I\/O thread to update it\r\n              Slave_DDL_Groups: 1\r\nSlave_Non_Transactional_Groups: 0\r\n    Slave_Transactional_Groups: 0\r\n1 row in set (0.000 sec)<\/pre>\n<p>N\u1ebfu b\u1ea1n th\u1ea5y 2 d\u00f2ng\u00a0 running nh\u01b0 sau th\u00ec replication \u0111\u00e3 \u0111\u01b0\u1ee3c c\u1ea5u h\u00ecnh ho\u00e0n t\u1ea5t<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Slave_IO_Running: Yes\r\nSlave_SQL_Running: Yes<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Replication l\u00e0 m\u1ed9t t\u00ednh n\u0103ng cho ph\u00e9p n\u1ed9i dung c\u1ee7a m\u1ed9t ho\u1eb7c nhi\u1ec1u m\u00e1y ch\u1ee7 (\u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 masters) \u0111\u01b0\u1ee3c nh\u00e2n \u0111\u00f4i tr\u00ean m\u1ed9t ho\u1eb7c nhi\u1ec1u m\u00e1y ch\u1ee7 (\u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 slaves). B\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m so\u00e1t d\u1eef li\u1ec7u n\u00e0o c\u1ea7n sao ch\u00e9p. T\u1ea5t c\u1ea3 c\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u, m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u01a1 s\u1edf [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":16424,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"tags":[],"ht_kb_category":[5156],"class_list":["post-4145","ht_kb","type-ht_kb","status-publish","format-standard","has-post-thumbnail","hentry","ht_kb_category-mariadb"],"_links":{"self":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb\/4145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb"}],"about":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/comments?post=4145"}],"version-history":[{"count":0,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb\/4145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/media\/16424"}],"wp:attachment":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/media?parent=4145"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/tags?post=4145"},{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb_category?post=4145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}