MySQL Replication 的基本知识及简单配置(1)

MySQL的复制功能是MySQL高级功能之一,任何使用MySQL的企业一般都会用到该功能,复制功能有什么作用呢,假如你只有一台MySQL服务器,那么当你的这台服务器突然因为某种原因宕机了,你的业务就会停止。这不是我们想遇到的情况,因而复制功能就会帮我们尽可能的避免这样种情形,当然还有很多其他的用处。
1.作为热备份(hot standby)
如果一开始就设置成master-slave 模式,即使master宕机了,那么我们可以快速切换到slave服务器,从而避免业务停止。
2.可以生成报表
一般情况上,每个公司都会对业务数据进行统计分析,生成按年度、月度、周度及日等进行各种维度的统计工作,那么如果读写都在master 上的数据进行操作,势必会影响服务器的性能。而且报表的统计在实时性上,一般要求不高,所以单独在slave上进行统计,这样既不会打扰master 还提高了统计的效率。
3.提供数据库的访问效率
随着业务数据的不断增多,数据库访问效率也会下降,为了提高数据库的读效率,可以部署成读写分离,master可以进行写操作,而slave可以进行读操作。

最简单的读写分离实现

这里写图片描述

图片来自 OReilly.MySQL.High.Availability.Tools.for.Building.Robust.Data.Centers.2nd.Edition

主要有三个步骤:
1. 配置master服务器
2. 配置slave 服务器
3. slave连接到master
4. 测试

准备两台centos6.8服务器,并分别安装MySQL5.7。分别修改成主机名为master (固定Ip 192.168.0.110),slave1(固定IP 192.168.0.111)

master

[root@m
aster ~]# hostname
master
[root@master ~]# ifconfig eth0 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:21:52:0F  
          inet addr:192.168.0.110  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe21:520f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:212 errors:0 dropped:0 overruns:0 frame:0
          TX packets:118 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:19754 (19.2 KiB)  TX bytes:11862 (11.5 KiB)

[root@master ~]# 

slave1

[root@slave1 ~]# hostname
slave1
[root@slave1 ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:97:A7:8C  
          inet addr:192.168.0.111  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe97:a78c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:288 errors:0 dropped:0 overruns:0 frame:0
          TX packets:115 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22649 (22.1 KiB)  TX bytes:11233 (10.9 KiB)

1.配置master服务器

启动mysql 日志,编辑/etc/my.cnf

[root@master ~]# vim /etc/my.cnf

##增加下面两行

log_bin=mysql-bin #二进制日志
server-id = 110  # 该值唯一,只要和slave不同就行,我这里用当前服务器ip尾数,易于区分

##保存后,重启MySQL服务
[root@master ~]# /etc/init.d/mysqld restart

##备份master上的MySQL数据库
[root@master tmp]# mysqldump --single-transaction --master-data=2 --triggers --routines --all-databases -uroot -p> all.sql 
Enter password: 

把all.mysql 上传到slave1服务器/tmp 目录下
[root@master tmp]# scp all.sql [email protected]:/tmp
The authenticity of host '192.168.0.111 (192.168.0.111)' can't be established.
RSA key fingerprint is 7a:ba:3c:38:67:1a:b5:1b:de:25:5e:6a:cc:c3:8b:ed.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.111' (RSA) to the list of known hosts.
[email protected]'s password: 
all.sql                                          100%  758KB 758.1KB/s   00:00 

##登录master上的mysq
[root@master ~]# mysql -uroot -p
Enter password: 

##创建同步账号

mysql> create user 'dbbackup'@'192.168.0.%' identified by 'mysql.password';
Query OK, 0 rows affected (0.05 sec)

mysql> grant replication slave on *.* to dbbackup@'192.168.0.%';
Query OK, 0 rows affected (0.01 sec)

##查看master状态
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000005
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

2.配置slave1

[root@slave1 ~]# vim /etc/my.cnf

##增加下面两行

log_bin=mysql-bin #二进制日志
server-id = 111  # 与其他服务不同

##保存后,重启MySQL服务
[root@slave1 ~]# /etc/init.d/mysqld restart

##还原数据库,为了和master节点保持一致

[root@slave1 tmp]# mysqldump --all-databases -uroot -p< all.sql

3.slave连接到master

在slave1上,登录MySQL数据库

查找当前数据库日志文件及节点位置,可以通过备份的all.sql

[root@slave1 tmp]# less all.sql 
--
-- Position to start replication or point-in-time recovery from
--

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=154;

MASTER_LOG_FILE=’mysql-bin.000005’, MASTER_LOG_POS=154

mysql> change master to master_host='192.168.0.110',
    -> master_user='dbbackup',
    -> master_password='mysql.password',
    -> master_log_file='mysql-bin.000005',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.18 sec)

master_host=’192.168.0.110’ #master 主机名或者IP地址 如果填写主机名,需要在hosts进行map
master_user=’dbbackup’ #用于replication的账号
master_password=’mysq.password’ #用于replication的密码
master_log_file=’mysql-bin.000005’ #可以在备份数据all.sql上查询
master_log_pos=154 #同步的当前日志文件的位置

##停止slave
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
##启动slave
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
##查看salve 状态

mysql> show slave status\G;  
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.110
                  Master_User: dbbackup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 416
               Relay_Log_File: slave1-relay-bin.000002
                Relay_Log_Pos: 582
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

当这两个参数都为Yes 表示正常运行
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

4.测试

在master上创建数据库test


mysql> create database test;
Query OK, 1 row affected (0.05 sec)

在slave1 上查询test 数据库是否存在

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)
发布了24 篇原创文章 · 获赞 4 · 访问量 8307

猜你喜欢

转载自blog.csdn.net/zhaitianyong/article/details/71123609