1. 主從原理 1.1 主從介紹 所謂 mysql 主從就是建立兩個完全一樣的資料庫,其中一個為主要使用的資料庫,另一個為次要的資料庫,一般在企業中,存放比較重要的數據的資料庫伺服器需要配置主從,這樣可以防止因資料庫伺服器宕機導致數據丟失,還能保證業務量太多、數據太多和訪問人數太多時服務的質量(服務 ...
1. 主從原理
1.1 主從介紹
所謂 mysql 主從就是建立兩個完全一樣的資料庫,其中一個為主要使用的資料庫,另一個為次要的資料庫,一般在企業中,存放比較重要的數據的資料庫伺服器需要配置主從,這樣可以防止因資料庫伺服器宕機導致數據丟失,還能保證業務量太多、數據太多和訪問人數太多時服務的質量(伺服器響應速度),還能提供故障切換、讀寫分離、和備份等等功能
1.2 主從作用
- 實時災備,用於故障切換
- 讀寫分離,提供查詢服務
- 備份,避免影響業務
1.3 主從形式
一主一從
主主複製:當作備份使用,當主伺服器出現故障時,另一個主伺服器會自動頂上。
一主多從:用來實現讀寫分離,當寫操作較少時,讀操作較多時使用,主伺服器用來實現寫操作,從伺服器用來實現讀操作。
多主一從:用來實現讀寫分離,當寫操作較多時,讀操作較少時使用,主伺服器用來實現寫操作,從伺服器用來實現讀操作。5.7 開始支持
聯級複製:是指從主場地複製過來的又從該場地再次複製到其他場地,即 A 場地把數據複製到 B 場地,B 場地又把這些數據或其中部分數據再複製到其他場地。
1.4 主從複製原理
- 主庫將所有的寫操作記錄到 binlog 日誌中並生成一個 log dump 線程,將 binlog 日誌傳給從庫的 I/O 線程
- 從庫生成兩個線程,一個 I/O 線程,一個 SQL 線程
- I/O 線程去請求主庫的 binlog,並將得到的 binlog 日誌寫到 relay log(中繼日誌) 文件中
- SQL 線程,會讀取 relay log 文件中的日誌,並解析成具體操作,來實現主從的操作一致,達到最終數據一致的目的
2. 主從複製配置
主從複製配置步驟:
-
確保從資料庫與主資料庫里的數據一樣
-
在主資料庫里創建一個同步賬號授權給從資料庫使用
-
配置主資料庫(修改配置文件)
-
配置從資料庫(修改配置文件)
需求:
搭建兩台 MySQL 伺服器,一臺作為主伺服器,一臺作為從伺服器,主伺服器進行寫操作,從伺服器進行讀操作
環境說明:
資料庫角色 IP 應用與系統版本 有無數據
主資料庫 192.168.23.4 Centos7.8 mysql-5.7 有數據
從資料庫 192.168.23.5 Centos7.8 mysql-5.7 無數據
2.1Mysql安裝
註意:需把安裝包分別上傳到兩台伺服器上分別執行下列命令進行安裝
[root@localhost soft]# yum remove mysql-libs
[root@localhost soft]# rpm -qa | grep mariadb
進行安裝
將下載好的mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar上傳
安裝相應軟體
[root@localhost soft]# yum install -y openssl-devel.x86_64 openssl.x86_64 yum install -y libaio.x86_64 libaio-devel.x86_64 yum install -y perl.x86_64 perl-devel.x86_64 yum install -y perl-JSON.noarch yum install -y autoconf yum install -y wget yum install -y net-tools
關閉防火牆
[root@localhost soft]# systemctl stop firewalld
[root@localhost soft]# systemctl disable firewalld
解壓軟體包:
[root@localhost soft]# tar -xvf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar
[root@localhost soft]# rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm
[root@localhost soft]# rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm
[root@localhost soft]# rpm -ivh mysql-community-client-5.7.22-1.el7.x86_64.rpm
[root@localhost soft]# rpm -ivh mysql-community-server-5.7.22-1.el7.x86_64.rpm
[root@localhost soft]# rpm -ivh mysql-community-libs-compat-5.7.22-1.el7.x86_64.rpm
[root@localhostsoft]#rpm -ivh mysql-community-embedded-compat-5.7.22-1.el7.x86_64.rpm
[root@localhost soft]# rpm -ivh mysql-community-devel-5.7.22-1.el7.x86_64.rpm
[root@localhost soft]# rpm -ivh mysql-community-test-5.7.22-1.el7.x86_64.rpm
查看安裝版本:
[root@localhost soft]# mysql -V
編輯配置文件
vim /etc/my.cnf
#跳過登錄驗證 無需密碼即可登錄mysql
skip-grant-tables
# 設置預設字元集UTF-8
character_set_server=utf8
collation-server=utf8_general_ci
# 設置預設字元集UTF-8
init_connect='SET NAMES utf8'
# 設置資料庫日誌過期天數為14天
server_id=1
expire_logs_days=14
[client]
default-character-set=utf8
##添加
skip-grant-tables
character_set_server=utf8
collation-server=utf8_general_ci
init_connect='SET NAMES utf8'
server_id=1
expire_logs_days=14
[client]
default-character-set=utf8
啟動mysql
[root@localhost soft]# systemctl start mysqld.service
[root@localhost soft]# systemctl status mysqld
獲取mysql的root用戶的初始密碼
[root@localhost soft]# grep 'temporary password' /var/log/mysqld.log
以獲取mysql的root用戶的初始密碼登錄資料庫
[root@localhost soft]# mysql -u root -pAcqWlI64o:kG
修改root密碼
mysql> flush privileges;
mysql> set password for root@localhost=password('123456');
使密碼即時生效
mysql> flush privileges;
允許以root身份遠程登錄mysql
mysql> grant all privileges on *.* to root@'%' identified by "123456";
mysql> flush privileges;
2.2 mysql 主從配置
2.2.1 確保從資料庫於主資料庫的數據一樣
為確保從資料庫與主資料庫里的數據一樣,先全備主資料庫並還原到從資料庫中
//主庫為master
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]#
//從庫為slave
[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# bash
[root@slave ~]#
//先查看主庫有哪些庫
[root@master soft]# mysql -uroot -p123456 -e‘show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
//在查看從庫
[root@slave soft]# mysql -uroot -p123456 -e‘show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//全備主庫 //全備主庫時需要另開一個終端,給資料庫加上讀鎖,避免在備份期間有其他人在寫入導致數據不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
//此鎖表的終端必須在備份完成以後才能退出
//備份主庫並將備份文件傳送到從庫
[root@master soft]# mysqldump -uroot -p123456 --all-databases > /opt/acc-2022.sql;
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ls /opt/
[root@master soft]# scp /opt/acc-2022.sql [email protected]:/opt/
//解除主庫的鎖表狀態,直接退出互動式界面即可
mysql> quit
Bye
[root@master ~]#
//在從庫上恢復主庫的備份並查看從庫有哪些庫,確保與主庫一致
[root@slave ~]# mysql -uroot -p123456 < /opt/acc-2022.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave ~]# mysql -uroot -p123456 -e'show databases';
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
[root@slave ~]#
2.2.3 配置主資料庫
[root@master soft]# vim /etc/my.cnf
//在[mysql]這段後面添加
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin //添加 啟用binlog日誌
server-id=1 //添加 資料庫伺服器唯一標識符,主庫的server-id值必須比從庫的小
# Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重啟數主庫的mysql服務
[root@master soft]# systemctl restart mysqld
[root@master soft]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
[root@master soft]#
//查看主庫狀態
[root@master soft]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
2.2.4 配置從資料庫
[root@slave ~]# vim /etc/my.cnf
//在[mysql]這段後面添加
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2 //添加 從庫的server-id比主庫的大
relay-log=mysql-relay-bin //添加
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重啟mysql服務
[root@slave ~]# systemctl restart mysqld
[root@slave ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
[root@slave ~]#
//配置並啟動主從複製
[root@slave ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to
-> master_host='192.168.23.4',
-> master_user='hqd',
-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>
//查看伺服器狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.23.4
Master_User: hqd
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
3 測試驗證
在主伺服器創建‘hqd’名稱的庫 查看從伺服器是否同步
mysql> create database hqd;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hqd |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql>
在從資料庫中查看數據是否同步
[root@slave ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hqd |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql>