MySQL主從複製
案例概述
在企業網站中,後端MySQL數據庫只有一臺時,會有以下問題:
- 單點故障服務不可用
- 無法處理大量的併發數據請求
- 數據丟失
改造辦法 - 增加MySQL數據庫服務器,對數據進行備份,形成主備
- 確保主備MySQL數據庫服務器數據是一樣的
- 主服務器宕機了,備份服務器繼續工作,數據有保障
MySQL的主從複製與讀寫分離是密切相關的
更高級的解決方案
通過主從複製的方式來同步數據,在通過讀寫分離提升數據庫的併發能力
Amoeba 變形蟲
案例實施
1.所有服務器關閉firewalld或者進行規則設置
2.建立時間同步環境 - 在主服務器上安裝ntp時間同步服務器
- 使用yum安裝ntp服務
- 修改ntp.conf,設置主服務器爲時間同步源
- 在從服務器上進行時間同步
- 使用yum安裝ntpdate並進行時間同步
在三臺數據庫服務器上安裝mysql
- 使用yum安裝ntpdate並進行時間同步
- 編譯安裝mysql
- 優化調整
- 初始化數據庫
- 啓動mysql服務並進行root用戶密碼設置
配置mysql master主服務器 - 修改/etc/my.cnf配置文件,增加服務器id,配置二進制日誌選項
- 登錄mysql服務,授權所有的從服務器複製二進制日誌的權限
配置兩臺從服務器 - 修改/etc/my.cnf配置文件,增加服務器id,配置二進制日誌選項
- 登錄mysql,配置主從同步
實操
實驗環境:準備5臺centos 7系統的虛擬機
一臺主服務器、兩臺從服務器、一臺amoeba、一臺客戶機
實驗拓撲圖:
三臺mysql服務器先都安裝ntp、ntpdate
用於哦配置時間同步[root@master ~]# yum install ntp -y [root@slave1 ~]# yum install ntp ntpdate -y [root@slave2 ~]# yum install ntp ntpdate -y
修改ntp配置文件
在主服務器下設置ntp配置文件,然後開啓ntpd,關閉防火牆[root@master ~]# vim /etc/ntp.conf server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst ‘server 127.127.247.0 //設置本地是時鐘源,這裏的127.127指自己的192.168 ’fudge 127.127.247.0 stratum 8 //設置時間環的時間層級(時間環)爲8 [root@master ~]# systemctl start ntpd [root@master ~]# systemctl stop firewalld [root@master ~]# setenforce 0 [root@master ~]# systemctl enable ntpd Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
配置從服務器slave1,直接開啓ntpd服務,進行時間同步,去匹配主服務器(ip地址)時間
[root@slave1 ~]# systemctl start ntpd [root@slave1 ~]# systemctl stop firewalld [root@slave1 ~]# setenforce 0 [root@slave1 ~]# systemctl enable ntpd Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service. [root@slave1 ~]# /usr/sbin/ntpdate 192.168.247.160 8 Jan 18:39:26 ntpdate[114393]: the NTP socket is in use, exiting
slave1同步完成,接下來同步slave2
[root@slave2 ~]# systemctl start ntpd [root@slave2 ~]# systemctl enable ntpd Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service. [root@slave2 ~]# systemctl stop firewalld [root@slave2 ~]# setenforce 0 [root@slave2 ~]# /usr/sbin/ntpdate 192.168.247.160 8 Jan 18:40:38 ntpdate[82655]: the NTP socket is in use, exiting
接下來就是安裝MySQL和優化的過程
由於過程冗雜,詳細過程請查看本人之前博客:
https://blog.51cto.com/14557905/2458283
安裝mysql完畢,並且做完一系列優化
開始做主從同步
1.修改主服務器配置文件
[root@master mysql-5.6.26]# vim /etc/my.cnf
//寫在mysqld下
log-bin=master-bin
//上面是開啓二進制文件
log-slave-update=true
//開啓從服務器更新
server-id = 11
//服務器id爲11(id不可以重複)
重啓服務
[root@master mysql-5.6.26]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
主服務器登陸mysql,給從服務器創建用戶並且允許複製所有數據(ON . )
[root@master mysql-5.6.26]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.247.%' IDENTIFIED BY 'abc123';
//允許 複製,從服務器以myslave用戶的身份,從192.168.247.0的網段,使用abc123的密碼,去複製所有的數據庫以及下面的表(*.*)
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
//刷新數據庫
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
//查看主服務器的位置點,從服務器的同步位置點就是下面的412
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 412 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置從服務器slave1,服務器id不能一致,開啓中繼日誌,索引中繼日誌
[root@slave1 mysql-5.6.26]# vim /etc/my.cnf
[mysqld]
//寫在mysqld下
log-bin=mysql-bin
server-id = 22
//另一臺slave2 id 爲23
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[root@slave1 mysql-5.6.26]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
從服務器登陸mysql,添加主服務器(ip地址,使用主服務器的賬戶mysalve,輸入主服務器賬戶的密碼,確定同步的二進制文件,同步的位置點)
[root@slave1 mysql-5.6.26]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> change master to master_host='192.168.247.160',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=412;
//添加主服務器
Query OK, 0 rows affected, 2 warnings (0.01 sec)
開啓從服務器功能
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
查看從服務器狀態
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.247.160
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 412
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000001
‘ Slave_IO_Running: Yes //顯示slave功能已開啓
‘ Slave_SQL_Running: Yes //顯示slave功能已開啓
Exec_Master_Log_Pos: 412
Relay_Log_Space: 455
Master_Server_Id: 11
Master_UUID: e9a82741-3223-11ea-af25-000c29524d89
Master_Info_File: /home/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
1 row in set (0.00 sec)
同步配置完成,接下來測試
主服務器,創建一個school數據庫
mysql> create database school;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
從服務器直接查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
MySQL服務器的主從同步就做完成了