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>

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/502681.html
標籤:其他
