主庫開啟bin-log二進制日志功能,並建立slave賬號,並授權從庫連接主庫,從庫通過change master得到主庫的相關同步信息,
然后連接主庫進行驗證,主庫產生的新數據會導入到bin-log二進制文件中,同時主庫會開啟lo線程,從庫也會開啟lo線程以及
sql線程,從庫中的lo線程與主庫的lo線程連接一旦主庫庫數據有所變更則從庫將變更的數據復制到relary-bin(中繼日志)中,
sql線程會讀取relay log(中繼日志)文件中的日志,並解析成具體操作,至此整個同步過程完成
設備:
master:192.168.200.125
slave1:192.168.200.124
slave2:192.168.200.111
Master主操作:
關閉防火牆:
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
安裝mariadbb:
[root@localhost ~]# yum install mariadb mariadb-server -y #此處安裝了mariadb數據庫系統
安裝ntp時間同步軟件:主要為了讓主服務器和從服務器之間時間一致。那么從服務器中繼日志和主的二進制日志數據一致
[root@localhost ~]# yum install ntp -y
[root@localhost ~]# vim /etc/ntp.conf
在末尾加入如下兩行:主要讓自己變成ntp的服務端使得從服務器能獲取主服務器的時間
server 127.127.1.0 fudge 127.127.1.0 stratum 8
啟動NTP服務:
[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# systemctl enable ntpd
配置主配置文件開啟二進制日志:
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=1 #server的id號 log-bin=mysql-binlog #前綴 log-slave-updates=true #允許從對log_bin進行更新
重啟服務器:
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
創建Replication用戶:
[root@localhost ~]# mysql -uroot -p123123
MariaDB [(none)]> grant replication slave on *.* to 'myslave'@'192.168.200.%' identified by '123123'; #允許192.168.200.網段的myslave連接數據庫連接密碼為123123
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;;
Query OK, 0 rows affected (0.00 sec)
ERROR: No query specified
MariaDB [(none)]> show master status; #獲取masterDB的相關信息
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000001 | 479 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
考慮問題有沒有之前留下的老數據:備份master原有所有數據到兩台slave上
[root@localhost ~]# mysqldump -uroot -p123123 --all-databases > /root/alldbbackup.sql
[root@localhost ~]# scp /root/alldbbackup.sql root@192.168.200.124:/root/
[root@localhost ~]# scp /root/alldbbackup.sql root@192.168.200.111:/root/
主服務器創建一個新的數據庫用以給從數據庫測試同步情況:
MariaDB [mysql]> create database liuxiang;
Query OK, 1 row affected (0.00 sec)
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| liuxiang |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
Slave1和Slave2操作:兩台從服務器操作一致
關閉防火牆:
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
安裝mariadbb:
[root@localhost ~]# yum install mariadb mariadb-server -y
安裝ntpdate:
[root@localhost ~]# yum install ntpdate -y
在兩個salve節點上配置與Master進行時間同步:
[root@localhost ~]# ntpdate 192.168.200.125
15 Oct 14:15:10 ntpdate[11932]: adjust time server 192.168.200.125 offset -0.018919 sec
做周期計划:
[root@localhost ~]# crontab -l
* */5 * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1 #pool.ntp.org是互聯網上的時間同步器
導入master原數據腳本到mysql庫中:
[root@localhost ~]# mysql -uroot -p123123 < /root/alldbbackup.sql
測試連接主數據庫運行測試:連接成功配置正確
[root@localhost ~]# mysql -u myslave -p123123 -h 192.168.200.125
修改配置文件開啟中繼日志:
[root@localhost ~]# vim /etc/my.cnf
[mysqld] server-id=2 #slave2從服務器此處改為3 relay-log=relay-log-bin #前綴 relay-log-index=slave-relay-bin.index #索引文件
重啟mariadb服務:
[root@localhost ~]# systemctl restart mariadb
啟動從庫,運行從庫數據同步:
[root@localhost ~]# mysql -uroot -p12
MariaDB [(none)]> stop slave; #停掉自己從的數據庫角色
Query OK, 0 rows affected, 1 warning (0.09 sec)
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.200.125',
-> MASTER_USER='myslave',
-> MASTER_PASSWORD='123123',
-> MASTER_LOG_FILE='mysql-binlog.000001', #masterDB的相關信息
-> MASTER_LOG_POS=721; #masterDB的相關信息
Query OK, 0 rows affected (0.10 sec)
MariaDB [(none)]> start slave; #開啟自己從的數據庫角色
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.125
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000001
Read_Master_Log_Pos: 721
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 532
Relay_Master_Log_File: mysql-binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 721
Relay_Log_Space: 824
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0 #報錯編號
Last_IO_Error: #報錯位置
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
ERROR: No query specified
從測試與主數據庫同步情況:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| client |
| liuxiang |
| mydb |
| mysql |
| performance_schema |
| shuifei |
| test |
| var |
| yg |
+--------------------+
11 rows in set (0.00 sec)