文章目錄
1.mysql主從簡介
1.mysql主從複製主要⽤用途
a.⽤用於備份,避免影響業務
b.實時災備,⽤用於故障切換
c.讀寫分離,提供查詢服務
2.mysql主從複製存在的問題
a.主庫宕機後, 數據可能丟失
b.主庫寫壓⼒力力⼤大, 複製可能會延時
3.mysql主從複製解決⽅方法
a.半同步複製、或者全同步複製. 要求: Mysql5.7版本
b.並⾏行行複製, 解決從庫複製延遲的問題. 建議: 5.7版
4.mysql主從複製原理理
1.在主庫上把將更更改 DDL DML DCL 記錄到⼆二進制⽇日誌 Binary Log 中。
2.備庫 I/O 線程將主庫上的⼆二進制⽇日誌複製到⾃自⼰己的中繼⽇日誌 Relay Log 中。
3.備庫 SQL 線程讀取中繼⽇日誌中的事件,將其重放到備庫數據庫之上
2.SQL語句
SQL語句有三種類型:
DDL:數據定義語言,數據定義語言
DML:Data Manipulation Language,數據操縱語言
DCL:數據控制語言,數據控制語言
SQL語句類型 | 對應操作 |
---|---|
DDL DROP 、 ALTER | 創建 刪除、 修改 |
DML INSERT 、 DELETE | 向表中插入數據 、 刪除表中數據 |
SELECT DCL GRANT 、 REVOKE | 查詢表中數據 授權 、 移除授權 |
3.主從形式
1.一主一從
2.主主複製
3.一主多從—擴展系統讀取的性能,因爲讀是在從庫讀取的
4.多主一從—5.7開始支持
聯級複製
4.myslq安裝和配置
環境說明
數據角色名稱 | ip | 系統版本 |
---|---|---|
主-mysql-master | 192.168.69.134 | CentOS7 |
從-mysql-slave | 192.168.69.132 | redhat7 |
安裝參照上篇文章搭建MySQL服務
4.1安裝mysql備份數據
[root@mysql-master ~]# mysql -uroot -pqinyong -e 'show databases;' \\查看主庫
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zabbix |
[root@mysql-slave ~]# mysql -uroot -pqinyong -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@mysql-master ~]# mysqldump -uroot -pqinyong --all-databases >/root/all-qy-20190227.sql
[root@mysql-master ~]# ls |grep all
all-qy-20190227.sql
[root@mysql-master ~]# scp all-qy-20190227.sql [email protected]:/root/ //拷貝到從數據庫目錄下
[root@mysql-slave ~]# ls
all-qy-20190227.sql anaconda-ks.cfg a.txt
解除主庫的鎖表狀態,直接退出交互式界面即可
mysql> quit
Bye
在從庫上恢復主庫的備份並查看從庫有哪些庫,確保與主庫一致
[root@mysql-slave ~]# mysql -uroot -pqinyong <all-qy-20190227.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-slave ~]# mysql -uroot -pqinyong -e 'show databases;' //驗證從庫和主庫一致
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zabbix |
4.2在主數據庫裏創建一個同步賬號授權給從數據庫使用
mysql> CREATE USER 'qinyong'@'192.168.69.132' IDENTIFIED BY 'qy123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'qinyong'@'192.168.69.132';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4.3配置主數據庫
[root@localhost ~]# vim /etc/my.cnf
//在[mysqld]這段的後面加上如下內容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin //啓用binlog日誌
server-id=1 //數據庫服務器唯一標識符,主庫的server-id值必須比從庫的大
[root@mysql-master ~]# systemctl restart mysqld \\重啓服務
[root@mysql-master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
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)
4.4配置從數據庫
[root@mysql-slave ~]# vim /etc/my.cnf
//添加如下內容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=5 //設置從庫的唯一標識符,從庫的server-id值必須小於主庫的該值
relay-log=mysql-relay-bin //啓用中繼日誌relay-log
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@mysql-slave ~]# systemctl restart mysqld
[root@mysql-slave ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
mysql> change master to master_host='192.168.69.134',master_user='qinyong',master_password='qy123',master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.69.134
Master_User: qinyong
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //此處必須爲Yes
Slave_SQL_Running: Yes //此處必須爲Yes
4.5測試驗證
在主庫
mysql> select * from student;
Empty set (0.00 sec)
mysql> insert into student values (1,'sean',20),(2,'tom',23),(3,'jerry',30);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)
在從庫驗證查看是否同步
mysql> use zabbix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from student; \\查看同步成功
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)
5設置zabbix監控主從同步
5.1監控Slave_IO_Running和Slave_SQL_Running是否正常
[root@mysql-slave ~]# vi .my.cnf \\設置免密碼查看
[client]
user=root
password=qinyong
[root@mysql-slave ~]# vim SQL.sh
a=$(mysql -e 'show slave status\G'|grep ' Slave_IO_Running'|awk -F: '{print $2}')
b=$(mysql -e 'show slave status\G'|grep 'Slave_SQL_Running'|awk -F: 'NR==1 {print $2}')
if [ $a == Yes -a $b == Yes ]
then
echo '0'
else
echo '1'
exit 1
fi
[root@mysql-slave ~]# chmod +x SQL.sh
[root@mysql-slave ~]# ./SQL.sh
1
[root@mysql-slave ~]# mysql -e 'start slave'
[root@mysql-slave ~]# ./SQL.sh
0
[root@mysql-slave ~]# vim /usr/local/etc/zabbix_agentd.conf
# UnsafeUserParameters=0去掉註釋並且改成=1
UnsafeParameter=check_mysql-slave,/bin/bash /root/SQL.sh mysql-slave
5.1.2在zabbix的web管理界面
添加主機、添加監控項、添加觸發器
詳情參照添加腳本監控
[root@mysql-slave ~]# mysql -e 'start slave'