mysql的主从复制、基于Gtid的主从复制、半同步复制

1.mysql主从复制的作用:

(1)实时灾备,用于故障切换
(2) 读写分离,提供查询服务
(3) 备份,避免影响业务
2.主从部署必要条件:

(1) 主库开启binlog日志(设置log-bin参数)
(2) 主从server-id不同
(3)从库服务器能连通主库

3.主从复制实验:
实验环境:mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
server2:主数据库 master
server4:备数据库 slave

基本的主从复制原理实验

以下操作主、从数据库都需要进行

配置mysql实验环境:
tar xf  mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
yum install  mysql-community-client-5.7.17-1.el6.x86_64.rpm  
             mysql-community-common-5.7.17-1.el6.x86_64.rpm 
             mysql-community-libs-5.7.17-1.el6.x86_64.rpm
             mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
             mysql-community-server-5.7.17-1.el6.x86_64.rpm
vim /etc/my.cnf
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=1 #和slave的id保持不同
log-bin=mysql-bin
/etc/init/mysqld  start

这里写图片描述
这里写图片描述

[root@server2 ~]# grep password /var/log/mysqld.log 
2018-08-08T08:43:04.408394Z 1 [Note] A temporary password is generated for root@localhost: yYaBEaJ9cC  #这里是数据库的密码
2018-08-08T08:43:44.183933Z 0 [Note] Execution of init_file '/var/lib/mysql/install-validate-password-plugin.tvhQPf.sql' started.

[root@server2 ~]# mysql_secure_installation #数据库初始化
Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password. #这里的密码必须由大小写字母、数字、特殊字符组成不小于8位的密码

New password: 

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

master数据库的操作

mysql> grant replication slave on *.*  to lily@'172.25.23.%' identified by 'WN123*westos';
Query OK, 0 rows affected, 1 warning (0.06 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      843 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

创建数据库

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

mysql> use test;
Database changed
mysql> create table userlist(
    -> username varchar(15) not null,
    -> password varchar(25) not null);
Query OK, 0 rows affected (0.72 sec)

从数据库:
注意:这里必须指定日志和号码,master_log_file=’mysql-bin.000005’,master_log_pos=194,和gtid不同的是,gtid是动态查找,无需指定

change master to master_host='172.25.23.2',master_user='lily',master_password='WN123*westos',master_log_file='mysql-bin.000003',master_log_pos=843;
show slave status\G;#查看状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.23.2
                  Master_User: lily
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 194
               Relay_Log_File: server4-relay-bin.000004
                Relay_Log_Pos: 407
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 194

use test;
select * from userlist;#和主数据库保持一致

这里写图片描述

基于Gtid的mysql主从复制

在之前的实验环境进行操作:
主、从 mysql 操作

vim /etc/my.conf
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=1
log-bin=mysql-bin

gtid_mode=ON
enforce-gtid-consistency=true
/etc/init.d/mysql restart

此时主服务器在test库的userlist表中已有三组数据,具体情况查看上方操作
这里写图片描述

从服务器:

mysql> stop slave;
Query OK, 0 rows affected (0.35 sec)

mysql> 
mysql> change master to master_host='172.25.23.2',master_user='lily',master_password='WN123*westos',MASTER_AUTO_POSITION = 1;
Query OK, 0 rows affected, 2 warnings (0.51 sec)

mysql> start slave;
Query OK, 0 rows affected (0.33 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.23.2
                  Master_User: lily
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 194
               Relay_Log_File: server4-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes #这里保证开启
            Slave_SQL_Running: Yes # 这里保证开启

这里写图片描述
主数据库:对数据库信息进行更改,然后从数据库进行验证

mysql> use test;
Database changed
mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user2    | 234      |
| user3    | 567      |
| user1    | 123      |
+----------+----------+
3 rows in set (0.00 sec)
#对数据进行删除,然后在从数据库看结果
mysql> delete from userlist where username='user1';
Query OK, 1 row affected (0.13 sec)

这里写图片描述
从数据库验证:

mysql> use test;
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 userlist;
+----------+----------+
| username | password |
+----------+----------+
| user2    | 234      |
| user3    | 567      |
+----------+----------+
2 rows in set (0.00 sec)

mysql> 

这里写图片描述

mysql的半同步复制:

半同步复制简介

  何为半同步复制模式呢?在此我们先了解异步复制模式,这是MySQL的默认复制选项。异步复制即是master数据库把binlog日志发送给slave数据库,然后就没有了然后了。在此暴露一个问题,当slave服务器发生故障了,那么肯定会导致主从数据库服务器的数据不一致。

  为了解决上面的问题,MySQL5.5引入一种叫做半同步复制模式。开启这种模式,可以保证slave数据库接收完master数据库发送过来的binlog日志并写入自己的中继日志中,然后反馈给master数据库,告知已经复制完毕。

  开启这种模式后,当出现超时,主数据库将会自动转为异步复制模式,直到至少有一台从服务器接受到主数据库的binlog,并且反馈给主数据库。这时主数据库才会切换回半同步复制模式。

主数据库

mysql> show variables like 'have_dynamic_loading';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| have_dynamic_loading | YES   |
+----------------------+-------+
1 row in set (0.00 sec)

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.41 sec)

mysql> set global rpl_semi_sync_master_enabled=ON;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%semi%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled              | ON         |
| rpl_semi_sync_master_timeout              | 10000      |
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)

这里是插件的所在位置,安装数据库5.7版本自带
这里写图片描述
这里写图片描述

从服务器:

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.13 sec)

mysql> set global rpl_semi_sync_slave_enabled=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.00 sec)

这里写图片描述
测试:
从数据库关闭slave

mysql> stop slave;
Query OK, 0 rows affected (0.06 sec)

主数据库进行数据库的操作,这里我们插入数据(此时会暂停大概10秒左右才会成功,因为从数据库关闭,主数据库没有等到响应,所以切换为异步模式,不再继续等待,并且参数也会有所改变)

mysql> insert into userlist values('user1','123');
Query OK, 1 row affected (10.40 sec)

mysql> show status like '%rpl_semi_sync%';

这里写图片描述

从数据库测试:当开启服务时,主数据库重新接受到返回数据,又切换到半同步模式

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

mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user2    | 234      |
| user3    | 567      |
| user1    | 123      |
+----------+----------+
3 rows in set (0.00 sec)

mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user2    | 234      |
| user3    | 567      |
+----------+----------+
2 rows in set (0.00 sec)

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ningyuxuan123/article/details/81533960
今日推荐