1. 程式人生 > >linux 中mysql的主從復制

linux 中mysql的主從復制

功能 status meta telnet 混合 運行 warning usr 問題

一.主從復制

1.修改主庫配置文件
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log_bin=mysql-bin
binlog_format=row
server_id=1


2.重啟數據庫
[[email protected] ~]# /etc/init.d/mysqld restart

3.連接數據庫
[[email protected] ~]# mysql -uroot -p1

4.創建主從復制用戶
mysql> grant replication slave on *.* to [email protected]‘%‘ identified by ‘123‘;

5.記錄binlog名字和binlog位置點
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 | 317 | | | |
+------------------+----------+--------------+------------------+-------------------+

6.在從庫上執行change master to語句
change master to
master_host=‘10.0.0.51‘,
master_user=‘rep‘,
master_password=‘123‘,
master_log_file=‘mysql-bin.000001‘,

master_log_pos=317;

7.從庫開啟主從復制(IO線程,SQL線程)
mysql> start slave;

8.查看主從復制的狀態
mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

9.IO線程報錯:

1.網絡問題:ping 192.168.13.60
2.端口問題:telnet 192.168.13.60 3306
3.用戶名密碼問題:mysql -urep -p123 -h192.168.13.60

1)用戶名密碼輸入錯誤:
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘root‘@‘10.0.0.52‘ (using password: YES)


2)跳過反向解析
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘root‘@‘db02‘ (using password: NO)

vim /etc/my.cnf
[mysqld]#mysql5.6
skip-name-resolve
skip-name-resolv
#mysql5.7
skip_name_resolve

/etc/init.d/mysqld restart


從庫:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| rep |
| rep1 |
| rep2 |
| test |
+--------------------+


主庫:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ZLS |
| mysql |
| nb |
| oldboy |
| performance_schema |
| rep |
| rep1 |
| rep2 |
| world |
| zls1 |
+--------------------+
11 rows in set (0.00 sec)


#重點,在做主從復制之前,一定要做全備
mysqldump -uroot -p1 -S /opt/test.sock -A > /tmp/full.sql
mysql> source /tmp/full.sql

二.使用binlog恢復任意時間點的數據

1.binlog的工作模式

1)STATEMENT語句模式:默認
mysql> create database binlog;
mysql> create table binlog(id int);
mysql> insert into binlog values(1),(2),(3);

查看binlog:
[[email protected] data]# mysqlbinlog mysql-bin.000002

2)ROW行級模式:mysql5.7默認
[[email protected] data]# vim /etc/my.cnf
binlog_format=row
[[email protected] data]# /etc/init.d/mysqld restart
mysql> create database binlog1;
mysql> use binlog1;
mysql> create table binlog1(id int);
mysql> insert into binlog1 values(1),(2),(3);

查看binlog:
[[email protected] data]# mysqlbinlog mysql-bin.000003

行級模式:記錄的是SQL語句每一行的變化過程
[[email protected] data]# mysqlbinlog --base64-output=decode-rows -vvv mysql-bin.000003
BEGIN
/*!*/;
# at 406
#190510 10:48:41 server id 1 end_log_pos 459 CRC32 0xc0c7e4bc Table_map: `binlog1`.`binlog1` mapped to number 70
# at 459
#190510 10:48:41 server id 1 end_log_pos 509 CRC32 0x51306b79 Write_rows: table id 70 flags: STMT_END_F
### INSERT INTO `binlog1`.`binlog1`
### SET
### @1=1 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `binlog1`.`binlog1`
### SET
### @1=2 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `binlog1`.`binlog1`
### SET
### @1=3 /* INT meta=0 nullable=1 is_null=0 */
# at 509
#190510 10:48:41 server id 1 end_log_pos 540 CRC32 0x010ed7db Xid = 22
COMMIT/*!*/;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+

模擬數據變化:
mysql> update binlog1 set id=10 where id=1;
mysql> select * from binlog1;
+------+
| id |
+------+
| 10 |
| 2 |
| 3 |
+------+

mysql> delete from binlog1 where id=2;
mysql> select * from binlog1;
+------+
| id |
+------+
| 10 |
| 3 |
+------+

mysql> drop table binlog1;
mysql> drop database binlog1;

結束位置點:540
起始位置點:120



3)MIXED混合模式:將語句模式和行級模式 混合使用


2.如何查看binlog
[[email protected] data]# mysqlbinlog mysql-bin.000002
[[email protected] data]# mysqlbinlog --base64-output=decode-rows -vvv mysql-bin.000003

三.部署MHA


db01,02,03:yum install perl-DBD-MySQL -y

db03:
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

1.做主從復制的先決條件:

主庫:
1)開啟binlog
2)開啟server_id
3)創建主從復制用戶

從庫:
1)必須開啟binlog
2)從庫開啟server_id(與主庫不相同)
3)從庫必須要創建主從復制用戶
4)開IO,SQl線程 start slave;

db01:
[mysqld]
log_bin=mysql-bin
binlog_format=row
server_id=1


db02:
[mysqld]
log_bin=mysql-bin
binlog_format=row
server_id=2


db03:
[mysqld]
log_bin=mysql-bin
binlog_format=row
server_id=3

2.MHA工作原理
當Master出現故障時,它可以自動將最新數據的Slave提升為新的Master,然後將所有其他的Slave重新指向新的Master。

db01
1.開啟binlog
2.主從復制用戶
3.server_id 不同

db02
1.開啟binlog
2.主從復制用戶
3.server_id 不同

db03
1.開啟binlog
2.主從復制用戶
3.server_id 不同


=============================================================
3.MHA的工具

Manager工具包主要包括以下幾個工具:

masterha_check_ssh #檢查MHA的ssh-key
masterha_check_repl #檢查主從復制情況
masterha_manger #啟動MHA
masterha_check_status #檢測MHA的運行狀態
masterha_master_monitor #檢測master是否宕機
masterha_master_switch #手動故障轉移
masterha_conf_host #手動添加server信息
masterha_secondary_check #建立TCP連接從遠程服務器
masterha_stop #停止MHA
Node工具包主要包括以下幾個工具:

save_binary_logs #保存宕機的master的binlog
apply_diff_relay_logs #識別relay log的差異
filter_mysqlbinlog #防止回滾事件
purge_relay_logs #清除中繼日誌


MHA 是 C/S結構的服務
manager
node

4.主從配置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 829135 | | | |
+------------------+----------+--------------+------------------+-------------------+


mysql> change master to
-> master_host=‘10.0.0.51‘,
-> master_user=‘rep‘,
-> master_password=‘123‘,
-> master_log_file=‘mysql-bin.000004‘,
-> master_log_pos=829420;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

5.只讀 和 禁用刪除relaylog功能
#禁用自動刪除relay log 功能(3個庫都執行)
mysql> set global relay_log_purge = 0;
#設置只讀(只能在從庫執行)
mysql> set global read_only=1;
#編輯配置文件
[[email protected] ~]# vim /etc/my.cnf
#在mysqld標簽下添加
[mysqld]
#禁用自動刪除relay log 永久生效
relay_log_purge = 0

6.安裝node包
[[email protected] ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
[[email protected] ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
[[email protected] ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

7.安裝manager包(避免裝在主庫上)
[[email protected] ~]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

8.創建命令軟連接
[[email protected] ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/
[[email protected] ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/

[[email protected] ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/
[[email protected] ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/

[[email protected] ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/
[[email protected] ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/

9.創建mha工作目錄
[[email protected] ~]# mkdir /etc/mha

10.編輯mha配置文件
[[email protected] ~]# vim /etc/mha/app1.cnf
[server default]
manager_log=/etc/mha/manager.log
manager_workdir=/etc/mha/app1
master_binlog_dir=/usr/local/mysql/data
user=mha
password=mha
ping_interval=2
repl_password=123
repl_user=rep
ssh_user=root

[server1]
hostname=10.0.0.51
port=3306

[server2]
hostname=10.0.0.52
port=3306

[server3]
hostname=10.0.0.53
port=3306


11.在mysql中創建一個mha管理用戶(三臺),只需要在主庫上創建
mysql> grant all on *.* to [email protected]‘%‘ identified by ‘mha‘;


12.創建密鑰對,做免密登錄

[[email protected] ~]# ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa >/dev/null 2>&1
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]

[[email protected] ~]# ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa >/dev/null 2>&1
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]

[[email protected] ~]# ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa >/dev/null 2>&1
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]

13.測試免密登錄
[[email protected] ~]# ssh [email protected]
[[email protected] ~]# ssh [email protected]
[[email protected] ~]# ssh [email protected]

[[email protected] ~]# ssh [email protected]
[[email protected] ~]# ssh [email protected]
[[email protected] ~]# ssh [email protected]

[[email protected] ~]# ssh [email protected]
[[email protected] ~]# ssh [email protected]
[[email protected] ~]# ssh [email protected]

14.使用mha工具檢測ssh
[[email protected] ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf

15.使用mha工具檢測主從復制
[[email protected] ~]# masterha_check_repl --conf=/etc/mha/app1.cnf

16.啟動mha
[[email protected] ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/mha/manager.log 2>&1 &

17.檢查MHA啟動狀態
[[email protected] ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:25635) is running(0:PING_OK), master:10.0.0.51


18.主庫綁定vip
/sbin/ifconfig eth0:0 10.0.0.55/24

19.給腳本執行權限
[[email protected] app1]# chmod +x master_ip_failover

20.添加配置文件
master_ip_failover_script=/etc/mha/app1/master_ip_failover

1.腳本語法問題
2.腳本的格式問題 安裝格式轉換命令:
[[email protected] app1]# yum install -y dos2unix
[[email protected] app1]# dos2unix master_ip_failover
dos2unix: converting file master_ip_failover to Unix format ...
3.腳本的權限問題 chmod +x master_ip_failover

[[email protected] app1]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:26448) is running(0:PING_OK), master:10.0.0.52

(怕忘,一切為了打卡記錄)

linux 中mysql的主從復制