實驗環境:mysql
三臺服務器,一主兩從sql
master:192.168.200.111數據庫
slave1:192.168.200.112vim
slave2:192.168.200.113服務器
實驗步驟:ide
1.首先關閉全部服務器的防火牆和seLinux測試
iptables -Fspa
systemctl stop firewalld線程
setenforce 0rest
2.創建時間同步環境
在主服務器上安裝NTP時間同步服務器,通常都會有
yum -y install ntp
vim /etc/ntp.conf
在文件最末尾處添加:
server 127.127.1.0
fudge 127.127.1.0 startum 8
3.啓動ntp服務
systemctl start ntpd
chkconfig ntpd on
配置MySQL主服務器:
1.修改配置文件
vim /etc/my.cnf
文件末尾添加內容:
[mysqld]
log-bin=mysql-bin
log-slave-updates=true
server-id =11
#server-id不能和從服務器的相同
2.重啓服務
systemctl restart mariadb.service
3.給從服務器作受權
登陸到數據庫:
mysql -uroot -p'數據庫密碼'
grant replication slave on *.* to 'myslave'@'192.168.200.11_' identified by '數據庫密碼'
#給與192.168.200.11範圍的主機複製的功能
刷新受權表:
flush privileges;
查看主服務器的信息:
show master status;
配置從服務器slave1:
1.修改配置文件
vim /etc/my.cnf
文件末行添加內容:
[mysqld]
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
server-id =12
2.重啓服務
systemctl restart mariadb
3.登陸數據庫指定主服務器
mysql -uroot -p'密碼'
change master to master_host='192.168.200.111',master_user='myslave',master_password='123123',master_log_file='mysql-bin.000003',master_log_pos=245;
#指定主服務器的IP,組,主服務器數據庫的密碼,對應的日誌文件號和position號
4.開啓從服務功能
start slave;
5.檢查線程是否開啓
show slave status \G;
slave2同上操做
環境測試:
1.在MySQL主服務器建立db_test數據庫
create database db_test;
2.查看數據庫
show databases;
3.分別在兩臺從服務器上查看是否有該數據庫,作到數據同步
show databases;