準備:準備號兩臺安裝了mariadb數據庫的服務器,ip分部是主庫192.168.1.10,從庫:192.168.1.20mysql
1.中止mariasdb:systemctl stop mariadbsql
2.修改主服務器的配置文件,添加下列配置數據庫
[mysqld] # 自定義一個惟一id值 server-id=1 # 本地存儲bin-log日誌文件名 log-bin=qishi-logbin
3.啓動mariadb:systemctl srtart mariadb服務器
4.建立從庫讀取bin-log日誌文件的用戶並受權ide
#建立用戶 create user 'root'@'192.168.1.20' identified by '123456'; #用戶受權 grant replication slave on *.* to 'root'@'192.168.1.20'; # 刷新受權 flush privileges
5.進入主數據庫而且對數據庫進行鎖表只讀(防止在配置從庫時數據發生該表)spa
# 進入mysql數據庫 mysql -uroot -p123456 # 鎖表 flush table with read lock;
6.查看主庫狀態線程
7.將主數據庫原來數據導出rest
mysqldump -u root -p --all-databases > /opt/qishimaster.sql
8.中止從庫數據庫服務:systemctl stop mariadb日誌
9.在從庫數據庫添加配置文件code
# 與主庫同樣設置id值 server-id=3 # 設置爲只讀模式 read-only=true
10.啓動數據庫systemctl restart mariadb
11.將主數據庫備份數據複製到從數據庫中:scp 192.168.1.10:/opt/qishimaster.sql /opt/
12.進入到從數據庫中將數據導入到數據庫內:source /opt/qishimaster.sql
13.在從數據庫內配置主庫配置
#主庫ip change master to master_host='192.168.1.10', #用戶名 master_user='root', #密碼 master_password='123456', #bin-log文件名,在上圖看 master_log_file='qishi_logbin.000001', # 狀態碼 master_log_pos=479;
14.在從數據庫啓動同步開關:start slave;
15.解鎖主庫只讀:unlock tables;