mysql主从复制、半同步复制、并行复制、多组复制

一、主从复制

由于mysql源码编译耗时较长所以直接用rpm包安装

master:


[root@server2 ~]# ls
mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
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
[root@server2 ~]# tar xf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
[root@server2 ~]# yum install mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-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-server-5.7.17-1.el6.x86_64.rpm -y
[root@server2 ~]# grep "temporary password" /var/log/mysqld.log  //查找数据库原始密码
[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.

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) :

 ... 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) :

 ... skipping.

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) :

 ... skipping.
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) :

 ... 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) :

 ... skipping.
All done!
[root@server2 ~]# vim /etc/my.cnf  //末尾添加
server-id=2  //服务器 id,主从需不同
log-bin=mysql-bin  //日志
[root@server2 ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@server2 ~]# mysql -pTong+12345
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave on *.* to repl@'172.25.254.%' identified by 'Tong+12345';//创建一个repl用户可以在其他254的机器上远程登陆,赋予replication slave 权限
Query OK, 0 rows affected, 1 warning (0.17 sec)

mysql> show master status;

slave:

初始化过程同server2:
[root@server3 mysql]# vim /etc/my.cnf
server-id=3
[root@server3 mysql]# /etc/init.d/mysqld restart
[root@server3 mysql]# mysql -pTong+12345
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>changemastertomaster_host='172.25.254.2',master_user='repl',master_password="Tong+12345",master_log_file='mysql-bin.000001',master_log_pos=145;    //与 master 建立认证
Query OK, 0 rows affected, 2 warnings (0.26 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.2
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 450
               Relay_Log_File: server3-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes  //必须为yes
            Slave_SQL_Running: Yes   //必须为yes

 主从同步完成

二、Gtid主从复制

master:
[root@server2 ~]# vim /etc/my.cnf  //末尾添加如下内容
gtid_mode=ON
enforce-gtid-consistency=true
[root@server2 ~]# /etc/init.d/mysqld restart


slave:
[root@server3 ~]# vim /etc/my.cnf   //末尾添加如下内容
gtid_mode=ON
enforce-gtid-consistency=true
[root@server3 ~]# /etc/init.d/mysqld restart
[root@server3 mysql]# mysql -pTong+12345
mysql> stop slave;
Query OK, 0 rows affected (0.11 sec)

mysql>changemastertomaster_host='172.25.254.2',master_user='student',master_password='Tong+12345',MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.20 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.2
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: server3-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

 

 三、半同步复制

master:

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

mysql> show global variables like '%semi%';   //查看参数

 默认等待时间为十秒,十秒之后自动转换成异步复制,不能保证完全同步

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

mysql> show global status like '%semi%';

 slave:

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

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

mysql> show global variables like '%semi%';
mysql> stop slave io_thread; //重启io线程
Query OK, 0 rows affected (0.11 sec)

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

解决SQL、IO状态为NO的步骤:
 Slave:     mysql> stop slave;
            mysql> reset master;
mysql> reset slave;
mysql> start slave;
解决数据不同步的步骤:
(1)手动将master、slave的表和库等复制导入,保证数据同步
(2)Master:mysql> reset master;      //重置
     Slave:     mysql> stop slave;
                mysql> reset master;
mysql> reset slave;
mysql> start slave;
mysql> show slave status\G;
***************** 1. row *****************
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes  //必须是yes
//如果都是 yes,表示从库的 I/O,Slave_SQL 线程都正确开启.表明数据库正在同步 

并行复制

多组复制

[root@server2 ~]# /etc/init.d/mysqld stop
[root@server2 ~]# cd /var/lib/mysql
[root@server2 mysql]# rm -fr *
[root@server2 mysql]# vim /etc/my.cnf
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="cb59c76b-9cc1-11e8-9524-525400aebb10"//uuid
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.69.2:24901"
loose-group_replication_group_seeds="172.25.254.2:24901,172.25.254.3:24901,172.25.254.4:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
loose-group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8"


[root@server2 mysql]# /etc/init.d/mysqld start
[root@server2 mysql]# grep password /var/log/mysqld.log
[root@server2 mysql]# mysql -p
Enter password:   //输入查找到的密码
mysql> show databases;  //查看数据库时会报错,是因为没有给超级用户授权
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user root@localhost identified by 'Tong+12345';
Query OK, 0 rows affected (0.18 sec)

mysql> show databases;


mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'Tong+12345';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.84 sec)

mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='Tong+12345' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (1.16 sec)

mysql>  INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.30 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (2.56 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from performance_schema.replication_group_members;

 Server3、server4步骤和server2步骤基本相同,差别如下:

1、文件/etc/my.cnf中server_id不同loose-group_replication_local_address后面跟的参数是本机ip
2、在进入数据库后,加载完插件后,(不执行直接执行SET GLOBAL group_replication_bootstrap_group=ON;)START GROUP_REPLICATION;查看成员信息,全部都是ONLINE的即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值