linux 中mysql的主从复制

Posted sudaguo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux 中mysql的主从复制相关的知识,希望对你有一定的参考价值。

一.主从复制

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的主从复制的主要内容,如果未能解决你的问题,请参考以下文章

MySQL中主从复制不同步?

[Linux]-MySQL主从复制

Linux搭建MySql主从复制

Linux——MySQL主从复制与读写分离实现高可用性,负载均衡的功能

MySQL的Binlog与主从复制

linux 中mysql的主从复制