MariaDB 实现主从复制

2024-08-14 15:31:55 浏览数 (3)

實驗目的:

MariaDB為MySQL的一個分支,其完全開源、無版權之虞且操作上與 MySQL 一脈相承,實際應用中非常廣泛,軟件本身很小,安裝容易,使用簡單。

但其也有缺點,指令行方式操作,無原生GUI,如存在 Error ,則排查困難,各項命令參數亦不易理解。

此次實驗主要針對可能的分散性數據庫以MariaDB進行論證,探索其適用性。

實驗步驟:

  1. 在一臺Win電腦(192.168.101.200)安裝 MariaDB;
  2. 在一臺CentOS7電腦(192.168.101.201)安裝MariaDB;
  3. 將兩臺MariaDB分析建庫、建表,記錄 MASTER_LOG_FILE,MASTER_LOG_POS,以一臺為主,另一臺為從,設置主從複製。參考資料:(https://blog.51cto.com/suifu/1830682)

a) 首先給用戶授權:

Grant all privileges on *.* to ‘bruce’@’%’ identified by ‘cclcclccl’;

Flush privileges;

b) 然後在從機用IP(192.168.101.201:3306)連接,不通,經查,須設置 firewall:

# 將MariaDB端口開放

Firewall-cmd –zone=public –permanent –add-port=3306/tcp

# 重啟防火墻

Service firewalld restart

c) 重啟MariaDB:

Systemctl stop mariadb.service

Systemctl start mariadb.service

d) 在主機(192.168.101.201)設置備份:

Mysqldump -u bruce -p ccl –single-transaction –flush-logs –databases products –master-data=2 > 2.sql

Cat 2.sql | grep “CHANGE”

顯示:

--CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000003’, MASTER_LOG_POS=245;

記下。

在實驗過程中,MASTER_LOG_FILE一直為空,後來才知道需要在my.cnf中加入一行:log-bin=mysql-bin

e) 在從機(192.168.101.200)設置 Slave:

CHANGE MASTER 'Master201' TO MASTER_HOST='192.168.101.201',MASTER_USER='bruce',MASTER_PASSWORD= cclcclccl’,MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=245;

# 啟動

Start slave ‘Master201’;

# 顯示 Slave 狀態:

Show slave ‘Master201’ status G;

# 查看 Slave_IO_Running: Slave_SQL_Running: 兩項須都為 Yes.

f) 期間出現了錯誤:

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set'

后經查需要在主從機設置 server_id, 在 CentOS 的 my.cnf 增加一行:server_id=10, 在從機執行: set global server_id=11; 須不重複。

g) 最終,在主機

CREATE TABLE test(

Id int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,

Name nvarchar(20))

INSERT INTO TEST VALUES (1,’ABC’),(2,’DEF’),(3,’GHI’),(4,’XYZ’);

INSERT INTO TEST VALUES (5,’ABCDEFG’);

h) 在從機:

MariaDB [products]> select * from test;

---- ------

| id | Name |

---- ------

| 1 | ABC |

| 2 | DEF |

| 3 | GHI |

| 4 | XYZ |

---- ------

4 rows in set (0.000 sec)

MariaDB [products]> select * from test;

---- ---------

| id | Name |

---- ---------

| 1 | ABC |

| 2 | DEF |

| 3 | GHI |

| 4 | XYZ |

| 5 | ABCDEFG |

---- ---------

5 rows in set (0.000 sec)

i) END

實驗總結:

本次實驗完成了MariaDB間主從複製,但操作過程較繁瑣,且有些問題莫名其妙,查了很多資料才得以排除。從易用性和理解性上講,MSSQL更勝一籌,有原生GUI管理工具,在開發IDE中集中了DB對象資源管理器,易于調試;且和開發語言結合較緊密,有成熟的ORM可使用,可實現敏捷開發。

【Reference】:

https://blog.51cto.com/suifu/1830682

https://blog.csdn.net/mergerly/article/details/50068589

https://blog.51cto.com/coosh/1740217

https://blog.csdn.net/billfanggs/article/details/8905991

https://blog.csdn.net/edwzhang/article/details/17226975

https://blog.csdn.net/edwzhang/article/details/17226975

https://www.linuxidc.com/Linux/2012-02/54729.htm

https://www.jb51.net/article/27242.htm

https://www.cnblogs.com/mapu/p/9184212.html

https://www.jianshu.com/p/ada9f34d8563

https://www.cnblogs.com/cheyunhua/p/9896167.html

http://blog.chinaunix.net/uid-451-id-3143431.html

https://blog.51cto.com/asmboy001/197750

https://www.cnblogs.com/chuanzhang053/p/8710434.html

http://blog.chinaunix.net/uid-25266990-id-3465550.html

0 人点赞