{"id":6213,"date":"2024-08-29T10:09:54","date_gmt":"2024-08-29T03:09:54","guid":{"rendered":"https:\/\/longvan.net\/cong-dong\/?p=6213"},"modified":"2024-12-12T15:44:23","modified_gmt":"2024-12-12T08:44:23","slug":"huong-dan-cau-hinh-master-slave-cua-mysql-tren-linux","status":"publish","type":"post","link":"https:\/\/longvan.net\/cong-dong\/huong-dan-cau-hinh-master-slave-cua-mysql-tren-linux\/","title":{"rendered":"H\u01b0\u1edbng d\u1eabn c\u1ea5u h\u00ecnh Master &#8211; Slave MySQL tr\u00ean Linux"},"content":{"rendered":"<h3>I. Gi\u1edbi thi\u1ec7u<\/h3>\n<p>MySQL Replication l\u00e0 m\u1ed9t t\u00ednh n\u0103ng tr\u00ean MySQL cho ph\u00e9o sao ch\u00e9p d\u1eef li\u1ec7u t\u1eeb b\u1ea3n ch\u00ednh (master) sang nhi\u1ec1u b\u1ea3n ph\u1ee5 (slave).<br \/>\nQu\u00fd kh\u00e1ch h\u00e0ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u1ec3 tri\u1ec3n khai nh\u01b0 m\u1ed9t m\u00f4 h\u00ecnh HA, d\u1ef1 ph\u00f2ng r\u1ee7i ro ho\u1eb7c ph\u01b0\u01a1ng \u00e1n gi\u1ea3m t\u1ea3i h\u1ec7 th\u1ed1ng SQL trong m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p.<br \/>\nTrong b\u00e0i vi\u1ebft n\u00e0y Long V\u00e2n s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1ch c\u1ea5u h\u00ecnh Master &#8211; Slave MySQL tr\u00ean Linux.<\/p>\n<h3>II. Chu\u1ea9n b\u1ecb<\/h3>\n<ul>\n<li>1 Server Linux \u0111\u00f3ng vai tr\u00f2 l\u00e0m Master.<\/li>\n<li>1 Server Linux \u0111\u00f3ng vai tr\u00f2 l\u00e0m Slave.<\/li>\n<li>C\u01a1 s\u1edf d\u1eef li\u1ec7u: MySQL 8. tr\u1edf l\u00ean.<\/li>\n<\/ul>\n<h3>III. C\u00e0i \u0111\u1eb7t MySQL Server<\/h3>\n<ol>\n<li>C\u00e0i \u0111\u1eb7t MySQL\n<ul>\n<li>Ubuntu\/Debian:\n<pre>sudo apt update\nsudo apt install mysql-server -y<\/pre>\n<\/li>\n<li>CentOS\/Redhat:\n<pre>sudo yum update\nsudo yum install mysql-server -y<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Kh\u1edfi \u0111\u1ed9ng MySQL\n<ol>\n<li>Ubuntu\/Debian:\n<pre>systemctl start mysql.service\nsystemctl enable mysql.service<\/pre>\n<\/li>\n<li>CentOS\/Redhat:\n<pre>systemctl start mysqld\nsystemctl status mysqld<\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6215 size-full aligncenter\" src=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Status_MySQL.png\" alt=\"\" width=\"602\" height=\"228\" srcset=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Status_MySQL.png 602w, https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Status_MySQL-300x114.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/p>\n<h3>IV. C\u1ea5u h\u00ecnh Master &#8211; Slave<\/h3>\n<p><strong><span style=\"font-size: 18px\">Server Master<\/span><\/strong><\/p>\n<ol>\n<li>Truy c\u1eado v\u00e0o file c\u1ea5u h\u00ecnh c\u1ee7a MySQL v\u00e0 \u0111i\u1ec1u ch\u1ec9nh c\u00e1c th\u00f4ng s\u1ed1 c\u1ea7n thi\u1ebft nh\u01b0 sau:\n<pre>sudo vi \/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6216 aligncenter\" src=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/config-master-slavemaster.png\" alt=\"\" width=\"602\" height=\"577\" srcset=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/config-master-slavemaster.png 602w, https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/config-master-slavemaster-300x288.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/p>\n<ul>\n<li>Trong \u0111\u00f3\n<ul>\n<li>bind-address: IP c\u1ee7a server Master<\/li>\n<li>log_error: file log l\u1ed7i (m\u1eb7c \u0111\u1ecbnh)<\/li>\n<li>server-id: id c\u1ee7a server ( b\u1ea1n c\u00f3 th\u1ec3 ch\u1ecdn b\u1ea5t k\u1ef3 s\u1ed1 n\u00e0o l\u00e0m gi\u00e1 tr\u1ecb nh\u01b0ng s\u1ed1 \u0111\u00f3 ph\u1ea3i l\u00e0 duy nh\u1ea5t kh\u00f4ng \u0111\u01b0\u1ee3c tr\u00f9ng v\u1edbi c\u00e1c id c\u1ee7a server kh\u00e1c)<\/li>\n<li>binlog_do_db: Database m\u00e0 b\u1ea1n mu\u1ed1n Replicate<\/li>\n<\/ul>\n<\/li>\n<li>B\u1ea1n c\u00f3 th\u1ec3 Replicate nhi\u1ec1u Database v\u1edbi ch\u1ec9nh s\u1eeda nh\u01b0 sau:\n<pre>binlog_do_db = db\nbinlog_do_db = db_1\nbinlog_do_db = db_2<\/pre>\n<\/li>\n<li>Sau khi \u0111i\u1ec1u ch\u1ec9nh th\u00f4ng s\u1ed1 ho\u00e0n t\u1ea5t ch\u00fang ta s\u1ebd restart l\u1ea1i MySQL \u0111\u1ec3 nh\u1eadn c\u1ea5u h\u00ecnh:\n<pre>sudo systecmtl restart mysql<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Ti\u1ebfp theo s\u1ebd v\u00e0o MySQL t\u1ea1o user v\u00e0 ph\u00e2n quy\u1ec1n b\u1eb1ng l\u1ec7nh &#8220;sudo mysql&#8221;:\n<pre>mysql&gt; CREATE USER 'slave_user'@'slave_server_ip' IDENTIFIED WITH mysql_native_password BY 'password';\nmysql&gt; GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'slave_server_ip';\nmysql&gt; FLUSH PRIVILEGES;<\/pre>\n<ul>\n<li>L\u01b0u \u00fd: thay th\u1ebf c\u00e1c th\u00f4ng s\u1ed1 c\u1ee7a slave_user th\u00e0nh th\u00e0nh th\u00f4ng tin c\u1ee7a server Slave nh\u00e9!<br \/>\nTi\u1ebfp theo ta s\u1ebd truy xu\u1ea5t to\u1ea1 \u0111\u1ed9 Binary Log t\u1eeb server Master<\/p>\n<pre>mysql&gt; FLUSH TABLES WITH READ LOCK;\nmysql&gt; SHOW MASTER STATUS;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6217 aligncenter\" src=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Status_Master.png\" alt=\"\" width=\"602\" height=\"117\" srcset=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Status_Master.png 602w, https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Status_Master-300x58.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/>\u0110\u00e2y s\u1ebd l\u00e0 v\u1ecb tr\u00ed m\u00e0 c\u00e1c server Slave b\u1eaft \u0111\u1ea7u sao ch\u00e9p database, ch\u00fang ta s\u1ebd l\u01b0u l\u1ea1i th\u00f4ng tin c\u1ee7a \u201cFile\u201d v\u00e0 \u201cPosition\u201d, v\u00ec s\u1ebd c\u1ea7n d\u00f9ng \u0111\u1ec3 c\u1ea5u h\u00ecnh cho server Slave.<\/li>\n<\/ul>\n<\/li>\n<li>T\u1ea1o Database c\u1ea7n sao l\u01b0u cho server Master:\n<pre>mysql&gt; CREATE DATABASE longvan_db;  \nmysql&gt; Use longvan_db; \nmysql&gt; CREATE TABLE longvan_table ( longvan_column varchar(30) ); \nmysql&gt; INSERT INTO longvan_table VALUES (\u2018longvan.net'), ('support.longvan.net');<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-6218 aligncenter\" src=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/check_Database_Master.png\" alt=\"\" width=\"294\" height=\"319\" srcset=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/check_Database_Master.png 424w, https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/check_Database_Master-277x300.png 277w\" sizes=\"auto, (max-width: 294px) 100vw, 294px\" \/><\/li>\n<\/ol>\n<p><strong>Server Slave<\/strong><\/p>\n<ol>\n<li>\u0110\u1ea7u ti\u00ean ch\u00fang ta s\u1ebd v\u00e0o file config c\u1ee7a Mysql\n<pre>vi \/etc\/mysql\/mysql.conf.d\/mysql.cnf<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6219 aligncenter\" src=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/configSQL_Slave.png\" alt=\"\" width=\"602\" height=\"208\" srcset=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/configSQL_Slave.png 602w, https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/configSQL_Slave-300x104.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/p>\n<ul>\n<li>\u1ede \u0111\u00e2y c\u00e1c th\u00f4ng s\u1ed1 s\u1ebd t\u01b0\u01a1ng t\u1ef1 v\u1edbi Master ngo\u1ea1i tr\u1eeb server-id l\u00e0 \u0111\u1eb7c th\u00f9 n\u00ean ph\u1ea3i thay \u0111\u1ed5i, v\u00e0 ngo\u00e0i ra th\u00eam ph\u1ea7n relay-log x\u00e1c \u0111\u1ecbnh v\u1ecb tr\u00ed l\u01b0u nh\u1eadt k\u00fd chuy\u1ec3n ti\u1ebfp c\u1ee7a b\u1ea3n sao Database.<\/li>\n<li>Restart l\u1ea1i MySQL \u0111\u1ec3 nh\u1eadn c\u1ea5u h\u00ecnh tr\u00ean server Slave\n<pre>sudo systemctl restart mysql<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Sau \u0111\u00f3 ch\u00fang ta v\u00e0o giao di\u1ec7n c\u1ee7a Mysql \u0111\u1ec3 c\u1ea5u h\u00ecnh cho server Slave k\u1ebft n\u1ed1i \u0111\u01b0\u1ee3c Master\n<pre>sudo mysql\nmysql&gt; CHANGE REPLICATION SOURCE TO\nmysql&gt; SOURCE_HOST='Master_server_ip',\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # ip c\u1ee7a server Master\nmysql&gt; SOURCE_USER='slave_user',\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # user replicate t\u1ea1o tr\u00ean server Master\nmysql&gt; SOURCE_PASSWORD='password',\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # password user replicate\nmysql&gt; SOURCE_LOG_FILE='mysql-bin.000004\u2019,\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 # th\u00f4ng s\u1ed1 File l\u01b0u \u1edf server Master\nmysql&gt; SOURCE_LOG_POS=157;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # th\u00f4ng s\u1ed1 Position \u0111\u00e3 l\u01b0u \u1edf Master<\/pre>\n<\/li>\n<li>Sau c\u00f9ng ch\u00fang ta s\u1ebd d\u00f9ng l\u1ec7nh <strong>START SLAVE;<\/strong> \u0111\u1ec3 b\u1eaft \u0111\u1ea7u ch\u1ea1y MySQL Slave v\u00e0 \u0111\u1ed3ng database<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6220 size-full aligncenter\" src=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Start_Slave.png\" alt=\"\" width=\"602\" height=\"504\" srcset=\"https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Start_Slave.png 602w, https:\/\/longvan.net\/cong-dong\/wp-content\/uploads\/2024\/08\/Start_Slave-300x251.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/>Tr\u00ean h\u00ecnh, server Slave \u0111\u00e3 replicate \u0111\u01b0\u1ee3c database c\u1ee7a server Master<\/li>\n<\/ol>\n<p>Nh\u01b0 v\u1eady Long V\u00e2n \u0111\u00e3 ho\u00e0n t\u1ea5t H\u01b0\u1edbng d\u1eabn c\u1ea5u h\u00ecnh Master \u2013 Slave c\u1ee7a MySQL tr\u00ean Linux. Ch\u00fac Qu\u00fd kh\u00e1ch th\u00e0ng c\u00f4ng!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I. Gi\u1edbi thi\u1ec7u MySQL Replication l\u00e0 m\u1ed9t t\u00ednh n\u0103ng tr\u00ean MySQL cho ph\u00e9o sao ch\u00e9p d\u1eef li\u1ec7u t\u1eeb b\u1ea3n ch\u00ednh (master) sang nhi\u1ec1u b\u1ea3n ph\u1ee5 (slave). Qu\u00fd kh\u00e1ch h\u00e0ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u1ec3 tri\u1ec3n khai nh\u01b0 m\u1ed9t m\u00f4 h\u00ecnh HA, d\u1ef1 ph\u00f2ng r\u1ee7i ro ho\u1eb7c ph\u01b0\u01a1ng \u00e1n gi\u1ea3m t\u1ea3i h\u1ec7 th\u1ed1ng SQL trong m\u1ed9t [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_oasis_is_in_workflow":0,"_oasis_original":0,"footnotes":""},"categories":[4],"tags":[368,5,327,150],"class_list":["post-6213","post","type-post","status-publish","format-standard","hentry","category-linux","tag-co-so-du-lieu","tag-linux","tag-master-slave","tag-mysql"],"_links":{"self":[{"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/posts\/6213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/comments?post=6213"}],"version-history":[{"count":78,"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/posts\/6213\/revisions"}],"predecessor-version":[{"id":7284,"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/posts\/6213\/revisions\/7284"}],"wp:attachment":[{"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/media?parent=6213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/categories?post=6213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/longvan.net\/cong-dong\/wp-json\/wp\/v2\/tags?post=6213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}