linux服務-mysql主從
阿新 • • 發佈:2021-01-04
1.主從簡介
1.1誕生原因
資料庫隱患:訪問人多,伺服器宕機
伺服器時間長質量問題資料丟失
1.2主從作用
- 實時災備,用於故障切換
- 讀寫分離,提供查詢服務
- 備份,避免影響業務
1.3形式
- 1主1從
- 主主複製(高可用)
- 1主多從
- 多主1從(5.7版本支援)
- 聯級複製
2.主從原理
- 使用者在主機寫入(增刪改),資料庫改變,二進位制日誌記錄生成日誌備份(logdump)執行緒。從機的I/O執行緒請求讀取,主機收到請求後傳輸給從機。從機寫入中繼日誌(relaylog),從機的SQL執行緒讀取中繼日誌,解析後重現主機的操作
3.主從複製配置
環境
資料庫角色 | ip | 系統與資料庫版本 | 有無資料 |
---|---|---|---|
主 | 192.168.94.141 | rhel8.2 mysql5.7 |
有 |
從 | 192.168.94.140 | rhel8.2 mysql5.7 |
無 |
3.1建立同步使用者
- 看主表有的庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+ 5 rows in set (0.00 sec)
- 看從表有的庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
- 主從關掉防火牆selinux
[root@matster ~]# systemctl stop firewalld.service [root@matster ~]# setenforce 0
- 全備主庫
備份前上讀鎖
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.001 sec)
//全備
[root@localhost ~]# mysqldump -uroot -p123456 --all-databases>all-20210104.sql
[root@localhost ~]# ll
total 476
-rw-r--r-- 1 root root 482035 Jan 4 10:38 all-20210104.sql
-rw-------. 1 root root 1179 Jan 4 00:36 anaconda-ks.cfg
//將資料傳給從
[root@localhost ~]# scp /root/all-20210104.sql 'root'@'192.168.94.140':/root/
The authenticity of host '192.168.94.140 (192.168.94.140)' can't be established.
ECDSA key fingerprint is SHA256:3N9nRI4b+PknMMJ8yhtDWv/UjyjRNYizaOHP28glw0E.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.94.140' (ECDSA) to the list of known hosts.
[email protected]'s password:
all-20210104.sql 100% 471KB 68.1MB/s 00:00
- 同步主從資料
//從端恢復資料
[root@localhost ~]# mysql -uroot -p <all-20210104.sql
Enter password:
[root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| company |
| information_schema |
| mysql |
| performance_schema |
| school |
+--------------------+
- 建立主從同步使用者並授權(主上操作)
//建立使用者(主)
mysql> create user 'repl'@'192.168.94.140' IDENTIFIED BY 'repl123';
Query OK, 0 rows affected (0.00 sec)
//授權並重新整理
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.94.140';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
- 連線測試(從連主)
[root@localhost ~]# mysql -urepl -prepl123 -h192.168.94.141
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 9
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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>
3.2配置資料庫
- 配置主
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=10
[root@localhost ~]# systemctl restart mariadb.service
//檢視
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
- 配置從
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=20
relay-log=mysql-relay-bin
//設定從
MariaDB [(none)]> change master to \
-> master_host='192.168.94.141',\
-> master_user='repl',\
-> master_password='123456',\
-> master_log_file='mysql-bin.000001',\
-> master_log_pos=328;
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)
//檢視狀態
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.94.141
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 328
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
- 測試
//主增加記錄mia
MariaDB [school]> select * from school.student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 24 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | zhangshan | 26 |
| 5 | zhangshan | 20 |
| 6 | lisi | 27 |
| 7 | cchenshuo | 10 |
| 8 | wangwu | 3 |
| 9 | qiuyi | 15 |
| 10 | qiuxiaotian | 20 |
| 11 | eason | 28 |
+----+-------------+------+
11 rows in set (0.000 sec)
MariaDB [school]> insert student(name,age) value('mia',24);
Query OK, 1 row affected (0.001 sec)
//從檢視
MariaDB [school]> select * from school.student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 24 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | zhangshan | 26 |
| 5 | zhangshan | 20 |
| 6 | lisi | 27 |
| 7 | cchenshuo | 10 |
| 8 | wangwu | 3 |
| 9 | qiuyi | 15 |
| 10 | qiuxiaotian | 20 |
| 11 | eason | 28 |
| 12 | mia | 24 |
+----+-------------+------+
12 rows in set (0.000 sec)