| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
1、簡介
? ? ? MySQL作為世界上使用最為廣泛的數據庫之一,免費是其原因之一。但不可忽略的是它本身的功能的確很強大。隨著技術的發展,在實際的生產環境中,由單臺MySQL數據庫服務器不能滿足實際的需求。此時數據庫集群就很好的解決了這個問題了。采用MySQL分布式集群,能夠搭建一個高并發、負載均衡的集群服務器(這篇博客暫時不涉及)。在此之前我們必須要保證每臺MySQL服務器里的數據同步。數據同步我們可以通過MySQL內部配置就可以輕松完成,主要有主從復制和主主復制。
回到頂部
2、環境說明
? ? ?Linux版本CentOS6.6、MySQL 5.5
? ? ?ip:192.168.95.11、192.168.95.12
回到頂部
3、主從復制
回到頂部
? ? ?3.1、MySQL
? ? ? ? ? ? 已經安裝好,并且沒有任何數據
回到頂部
? ? ?3.2、配置文件
? ? ? ? ? ? 一般Linux中的MySQL配置文件都在/etc/my.cnf(windows中的配置文件為mysql.ini)
? ? ? ? ? ? log-bin=mysql-bin 開啟二進制日志
? ? ? ? ? ?注意:二進制日志必須開啟,因為數據的同步實質上就是其他的MySQL數據庫服務器將這個數據變更的二進制日志在本機上再執行一遍。
? ? ? ? ? ?192.168.95.11? 為主數據庫服務器
? ? ? ? ? ?192.168.95.12? 為從數據庫服務器
回到頂部
? ? 3.3、開始構建主從復制
? ? ? ? ? ? 第一步:
? ? ? ? ? ??在192.168.95.11中創建一個192.168.95.12主機中可以登錄的MySQL用戶
? ? ? ? ? ??密碼:mysql12
? ? ? ? ? ??mysqlgt;GRANT REPLICATION SLAVE ON *.* TO ‘mysql12’@’192.168.95.12’ IDENTIFIED BY ‘mysql12’;
? ? ? ? ? ??mysqlgt;FLUSH PRIVILEGES;
? ? ? ? ? ??第二步:
? ? ? ? ? ??查看192.168.95.11MySQL服務器二進制文件名與位置
? ? ? ? ? ??mysqlgt;SHOW MASTER STATUS;
? ? ? ? ? ??
? ? ? ? ? ??第三步:
? ? ? ? ? ??告知二進制文件名與位置
? ? ? ? ? ??在192.168.95.12中執行:
? ? ? ? ? ??mysqlgt;CHANGE MASTER TO
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_HOST=’192.168.95.11’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_USER=’mysql12’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_PASSWORD=’mysql12’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_LOG_FILE=’mysql-bin.000048’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_LOG_POS=432;
? ? ? ? ? ??
? ? ? ? ? ??完成主從復制配置
回到頂部
? ? ? 3.4、測試主從復制
? ? ? ? ? ??在192.168.95.12中
? ? ? ? ? ??mysqlgt;SLAVE START;?? #開啟復制
? ? ? ? ? ??mysqlgt;SHOW SLAVE STATUSG?? #查看主從復制是否配置成功
? ? ? ? ? ??
? ? ? ? ? ??當看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明狀態正常
? ? ? ? ? ??實際測試:
? ? ? ? ? ??--登陸192.168.95.11主MySQL
? ? ? ? ? ??mysqlgt;SHOW DATABASES;
? ? ? ? ? ??
? ? ? ? ? ??--登陸192.168.95.12從MySQL
? ? ? ? ? ??mysqlgt;SHOW DATABASES;
? ? ? ? ? ??
? ? ? ? ? ??-----------------------------------------------------
? ? ? ? ? ??192.168.95.11主MySQL操作:
? ? ? ? ? ??mysqlgt;create database aa;
? ? ? ? ? ??mysqlgt;create table tab1(id int auto_increment,name varchar(10),primary key(id));
? ? ? ? ? ??mysqlgt;show databases;
? ? ? ? ? ??mysqlgt;show tables;
? ? ? ? ? ??
? ? ? ? ? ??192.168.95.12從MySQL操作:
? ? ? ? ? ??mysqlgt;show databases;
? ? ? ? ? ??mysqlgt;show tables;
? ? ? ? ? ??
? ? ? ? ? ??由上面兩個結果圖可得知,兩主機達到了數據同步。主從復制的配置就是如此的簡單。
回到頂部
4、MySql主主復制
回到頂部
? ? ?4.1、實現原理
? ? ? ? ? ??主主復制即在兩臺MySQL主機內都可以變更數據,而且另外一臺主機也會做出相應的變更。聰明的你也許已經想到該怎么實現了。對,就是將兩個主從復制有機合并起來就好了。只不過在配置的時候我們需要注意一些問題,例如,主鍵重復,server-id不能重復等等。
回到頂部
? ? ?4.2、配置文件
? ? ? ? ? ? --192.168.95.11
? ? ? ? ? ??server-id=11?? #任意自然數n,只要保證兩臺MySQL主機不重復就可以了。
? ? ? ? ? ?log-bin=mysql-bin?? #開啟二進制日志
? ? ? ? ? ?auto_increment_increment=2?? #步進值auto_imcrement。一般有n臺主MySQL就填n
? ? ? ? ? ?auto_increment_offset=1?? #起始值。一般填第n臺主MySQL。此時為第一臺主MySQL
? ? ? ? ? ?binlog-ignore=mysql?? #忽略mysql庫【我一般都不寫】
? ? ? ? ? ?binlog-ignore=information_schema?? #忽略information_schema庫【我一般都不寫】
? ? ? ? ? ?replicate-do-db=aa?? #要同步的數據庫,默認所有庫
? ? ? ? ? ?--192.168.95.12
? ? ? ? ? ?server-id=12
? ? ? ? ? ?log-bin=mysql-bin
? ? ? ? ? ?auto_increment_increment=2
? ? ? ? ? ?auto_increment_offset=2
? ? ? ? ? ?replicate-do-db=aa
回到頂部
? ? 4.3、開始構建主主復制
? ? ? ? ? ?因為主主復制是兩個主從復制組合一起,所以我就接著上面主從復制接著配置。
? ? ? ? ? ?第一步:
? ? ? ? ? ?在192.168.95.12中創建一個192.168.95.11主機中可以登錄的MySQL用戶
? ? ? ? ? ?用戶:mysql11
? ? ? ? ? ?密碼:mysql11
? ? ? ? ? ?mysqlgt;GRANT REPLICATION SLAVE ON *.* TO ‘mysql11’@’192.168.95.11’ IDENTIFIED BY ‘mysql11’;
? ? ? ? ? ?mysqlgt;FLUSH PRIVILEGES;
? ? ? ? ? ?第二步:
? ? ? ? ? ?在192.168.95.12查看二進制日志名和位置
? ? ? ? ? ?mysqlgt;show master status;
? ? ? ? ? ?
? ? ? ? ? ?第三步:
? ? ? ? ? ?告知二進制文件名與位置
? ? ? ? ? ?在192.168.95.11中執行:
? ? ? ? ? ?mysqlgt;CHANGE MASTER TO
? ? ? ? ? ?MASTER_HOST=’192.168.95.12’,
? ? ? ? ? ?MASTER_USER=’mysql11’,
? ? ? ? ? ?MASTER_PASSWORD=’mysql11’,
? ? ? ? ? ?MASTER_LOG_FILE=’mysql-bin.000084’,
? ? ? ? ? ?MASTER_LOG_POS=107;
? ? ? ? ? ?
? ? ? ? ? ?完成主主復制配置
回到頂部
? ? 4.4、測試主主復制
? ? ? ? ? ?mysqlgt;SHOW SLAVE STATUSG?? #查看主從復制是否配置成功
? ? ? ? ? ?192.168.95.11
? ?
? ? ? ? ? ?192.168.95.12
?
?
? ? ? ? ? ?
? ? ? ? ? ?當看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明狀態正常
? ? ? ? ? ?測試:
? ? ? ? ? ?--192.168.95.11
? ? ? ? ? ?mysqlgt;use aa;
? ? ? ? ? ?mysqlgt;select*from tab1;
? ? ? ? ? ?tab1無數據
? ? ? ? ? ?--192.168.95.12
? ? ? ? ? ?mysqlgt;use aa;
? ? ? ? ? ?mysqlgt;select*from tab1;
? ? ? ? ? ?tab1無數據
? ? ? ? ? ?--192.168.95.11插入數據
? ? ? ? ? ?mysqlgt;insert into tab1 (name) value(‘11’),(‘11’),(‘11’);
? ? ? ? ? ?--192.168.95.12插入數據
? ? ? ? ? ?mysqlgt;insert into tab1 (name) value(‘22’),(‘22’),(‘22’);
? ? ? ? ? ?查看數據:
? ? ? ? ? ?
? ? ? ? ? ?主主復制配置成功!
回到頂部
5、注意事項
? ? ?1、主主復制配置文件中auto_increment_increment和auto_increment_offset只能保證主鍵不重復,卻不能保證主鍵有序。
? ? ?2、當配置完成Slave_IO_Running、Slave_SQL_Running不全為YES時,show slave statusG信息中有錯誤提示,可根據錯誤提示進行更正。
? ? ?3、Slave_IO_Running、Slave_SQL_Running不全為YES時,大多數問題都是數據不統一導致。
? ? ?常見出錯點:
? ? ?1、兩臺數據庫都存在db數據庫,而第一臺MySQL db中有tab1,第二臺MySQL db中沒有tab1,那肯定不能成功。
? ? ?2、已經獲取了數據的二進制日志名和位置,又進行了數據操作,導致POS發生變更。在配置CHANGE MASTER時還是用到之前的POS。
? ? ?3、stop slave后,數據變更,再start slave。出錯。
? ? ?終極更正法:重新執行一遍CHANGE MASTER就好了。
?
As one of the most widely used databases in the world, MySQL is free. But it can't be ignored that its function is very powerful. With the development of technology, in the actual production environment, a single MySQL database server can not meet the actual needs. At this time, the database cluster solves this problem very well. With MySQL distributed cluster, you can build a cluster server with high concurrency and load balance (not involved in this blog for the time being). Before that, we must ensure the data synchronization in each MySQL server. Data synchronization can be easily completed through MySQL internal configuration, mainly including master-slave replication and master-slave replication.
2. Environmental description
Two Linux virtual hosts
Linux version CentOS 6.6, MySQL 5.5
? ? ?ip:192.168.95.11、192.168.95.12
Back to the top
3. Master-slave replication
Back to the top
? ? ?3.1、MySQL
It's installed and there's no data
Back to the top
3.2 configuration file
In general, MySQL configuration files in Linux are in / etc / my.cnf (mysql.ini in Windows)
Log bin = MySQL bin enable binary log
Note: binary log must be enabled, because data synchronization is essentially another MySQL database server that executes the binary log of data change on the local machine again.
192.168.95.11 main database server
192.168.95.12 is the slave database server
Back to the top
3.3 start to build master-slave replication
Step 1:
Create a MySQL user in 192.168.95.11 who can log in to the 192.168.95.12 host
User: mysql12
Password: mysql12
? ? ? ? ? ??mysqlgt;GRANT REPLICATION SLAVE ON *.* TO ‘mysql12’@’192.168.95.12’ IDENTIFIED BY ‘mysql12’;
? ? ? ? ? ??mysqlgt;FLUSH PRIVILEGES;
Step 2:
View the binary file name and location of 192.168.95.11 MySQL server
? ? ? ? ? ??mysqlgt;SHOW MASTER STATUS;
? ? ? ? ? ??
Step 3:
Tell binary name and location
In 192.168.95.12:
? ? ? ? ? ??mysqlgt;CHANGE MASTER TO
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_HOST=’192.168.95.11’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_USER=’mysql12’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_PASSWORD=’mysql12’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_LOG_FILE=’mysql-bin.000048’,
? ? ? ? ? ? ? ? ? ? ?gt;MASTER_LOG_POS=432;
? ? ? ? ? ??
Complete master-slave replication configuration
Back to the top
3.4 test master-slave replication
In 192.168.95.12
MySQL gt; slave start; ? enable replication
MySQL gt; show slave status g ? check whether the master-slave replication is configured successfully
? ? ? ? ? ??
When you see slave IO running: Yes, slave SQL running: Yes, the status is normal
Actual test:
-- log in to 192.168.95.11 main MySQL
? ? ? ? ? ??mysqlgt;SHOW DATABASES;
? ? ? ? ? ??
-- log in to 192.168.95.12 and log in from mysql
? ? ? ? ? ??mysqlgt;SHOW DATABASES;
? ? ? ? ? ??
? ? ? ? ? ??-----------------------------------------------------
192.168.95.11 main MySQL operation:
? ? ? ? ? ??mysqlgt;create database aa;
? ? ? ? ? ??mysqlgt;use aa;
? ? ? ? ? ??mysqlgt;create table tab1(id int auto_increment,name varchar(10),primary key(id));
? ? ? ? ? ??mysqlgt;show databases;
? ? ? ? ? ??mysqlgt;show tables;
? ? ? ? ? ??
192.168.95.12 operation from MySQL:
? ? ? ? ? ??mysqlgt;show databases;
? ? ? ? ? ??mysqlgt;show tables;
? ? ? ? ? ??
From the above two results, we can see that the two hosts have achieved data synchronization. The configuration of master-slave replication is so simple.
Back to the top
4. MySQL primary primary replication
Back to the top
4.1 implementation principle
The primary and primary replication can change data in both MySQL hosts, and the other host will make corresponding changes. Smart you may have thought about how to achieve it. Yes, just combine the two master-slave replication. However, we need to pay attention to some problems during configuration, such as duplicate primary key, server ID, etc.
Back to the top
4.2 configuration file
? ? ? ? ? ? --192.168.95.11
Server id = 11 ? any natural number n, as long as the two MySQL hosts are not repeated.
Log bin = MySQL Bin - enable binary log
Auto increment = 2 step value auto increment. Generally, if there are n main mysql, fill in n
Auto increment offset = 1 initial value. Generally fill in the nth main mysql. This is the first main MySQL
Binlog ignore = MySQL ? ignore MySQL database [I don't usually write it]
Binlog ignore = information ﹐ schema ﹐ ignore the information ﹐ schema library [I don't usually write it]
Replicate do DB = AA ? database to be synchronized, all databases by default
? ? ? ? ? ?--192.168.95.12
? ? ? ? ? ?server-id=12
? ? ? ? ? ?log-bin=mysql-bin
? ? ? ? ? ?auto_increment_increment=2
? ? ? ? ? ?auto_increment_offset=2
? ? ? ? ? ?replicate-do-db=aa
Restart MySQL after configuration
Back to the top
4.3. Start to build primary primary replication
Since the primary and secondary replication is a combination of two primary and secondary replication, I will continue the above primary and secondary replication and then configure.
Step 1:
In 192.168.95.12, create a MySQL user that can log in to the 192.168.95.11 host
User: mysql11
Password: mysql11
? ? ? ? ? ?mysqlgt;GRANT REPLICATION SLAVE ON *.* TO ‘mysql11’@’192.168.95.11’ IDENTIFIED BY ‘mysql11’;
? ? ? ? ? ?mysqlgt;FLUSH PRIVILEGES;
Step 2:
Check binary log name and location at 192.168.95.12
? ? ? ? ? ?mysqlgt;show master status;
? ? ? ? ? ?
Step 3:
Tell binary name and location
In 192.168.95.11:
? ? ? ? ? ?mysqlgt;CHANGE MASTER TO
? ? ? ? ? ?MASTER_HOST=’192.168.95.12’,
? ? ? ? ? ?MASTER_USER=’mysql11’,
? ? ? ? ? ?MASTER_PASSWORD=’mysql11’,
? ? ? ? ? ?MASTER_LOG_FILE=’mysql-bin.000084’,
? ? ? ? ? ?MASTER_LOG_POS=107;
? ? ? ? ? ?
Complete primary primary replication configuration
Back to the top
4.4 test primary primary replication
Start slave start respectively;
MySQL gt; show slave status g ? check whether the master-slave replication is configured successfully
? ? ? ? ? ?192.168.95.11
? ?
? ? ? ? ? ?192.168.95.12
?
?
? ? ? ? ? ?
When you see slave IO running: Yes, slave SQL running: Yes, the status is normal
Test:
? ? ? ? ? ?--192.168.95.11
? ? ? ? ? ?mysqlgt;use aa;
? ? ? ? ? ?mysqlgt;select*from tab1;
Tab1 no data
? ? ? ? ? ?--192.168.95.12
? ? ? ? ? ?mysqlgt;use aa;
? ? ? ? ? ?mysqlgt;select*from tab1;
Tab1 no data
-- 192.168.95.11 insert data
? ? ? ? ? ?mysqlgt;insert into tab1 (name) value(‘11’),(‘11’),(‘11’);
-- 192.168.95.12 insert data
? ? ? ? ? ?mysqlgt;insert into tab1 (name) value(‘22’),(‘22’),(‘22’);
View data:
The data results of the two hosts are the same!
? ? ? ? ? ?
Primary primary replication configuration succeeded!
Back to the top
5. Precautions
1. Auto increment and auto increment offset in the primary and primary replication configuration file can only ensure that the primary key is not duplicate, but not orderly.
2. When the slave IO running and slave SQL running are not all yes, there is an error prompt in the show slave status g message, which can be corrected according to the error prompt.
3. When slave IO running and slave SQL running are not all yes, most problems are caused by data inconsistency.
Common mistakes:
1. Both databases have DB databases, but the first MySQL DB has tab1, and the second MySQL DB does not have tab1, which is certainly not successful.
2. The binary log name and location of the data have been obtained, and the data operation has been carried out, resulting in the change of POS. The previous POS is still used when configuring change master.
3. After stop slave, the data changes, and then start slave. Mistakes.
The ultimate correction: just execute change master again.
?
(the above are some of my own opinions. If there are any shortcomings or mistakes, please point out.)
--------------------------------------------------------------
主機參考,收集國內外VPS,VPS測評,主機測評,云服務器,虛擬主機,獨立服務器,國內外服務器,高性價比建站主機相關優惠信息@zhujicankao.com
詳細介紹和測評國外VPS主機,云服務器,國外服務器,國外主機的相關優惠信息,商家背景,網絡帶寬等等,也是目前國內最好的主機云服務器VPS參考測評資訊優惠信息分享平臺
這幾篇文章你可能也喜歡:
- HOSTDARE:在美國洛杉磯的VPS主持人40%折扣,年度付款起價為10.4美元起,起價為10美元。
- DIYVM:日本/美國/香港的副總裁以50元/月的價格開始,香港的服務器從499元/月開始
- HOSTKVM韓國本地VPS在有限的時間內可享受10%的折扣,您可以享受2核4G配置,價格為11.7美元,適合國際業務擴張(VPS品牌在韓國都優越)
- VMISS便宜的VPS:從多個國家 /地區的多個計算機室中進行選擇,并支持CN2 GIA,AS9929等的高速線路。
- [5.1] Edgenat香港VPS/US VPS/韓國VPS每年折扣30%,香港獨立服務器的起價為300元/月
本文由主機參考刊發,轉載請注明:MySQL主從復制與主主復制 http://m.k2270.com/12763.html
主機參考

















評論前必須登錄!
注冊