微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mysql主从复制

一、实验环境

1、主服务器

  • MysqL-master:192.168.200.111
  • MysqL-slave1:192.168.200.112
  • MysqL-slave2:192.168.200.113

2、所有主机安装mariadb

[root@localhost ~]# yum -y install mariadb*
[root@localhost ~]# systemctl start mariadb

3、所有服务器关闭防火墙和安全机制

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

 

二、建立时间同步环境 ,在主服务器上安装配置NTP时间同步服务器

1、在master上配置

安装时间服务器

[root@MysqL-m ~]# yum -y install ntp

修改配置文件

[root@MysqL-m ~]# vim /etc/ntp.conf    //手动添加
server 127.127.1.0
fudge 127.127.1.0 stratum 8

启动NTP服务

[root@MysqL-m ~]# systemctl start ntpd

2、在两个slave节点上配置(以slave1为例)

[root@MysqL-s1 ~]# yum -y install ntpdate
[root@MysqL-s1 ~]# ntpdate 192.168.200.111
19 Aug 18:57:08 ntpdate[22380]: no server suitable for synchronization found

 

三、配置master主服务器

1、修改配置文件(开启二进制日志)

[root@MysqL-m ~]# vim /etc/my.cnf
[MysqLd]
server-id=1
log-bin=MysqL-binlog
log-slave-updates=true

[root@MysqL-m ~]# systemctl restart mariadb

2、给从服务器授权

[root@MysqL-m ~]# MysqL -uroot -p

MariaDB [(none)]> grant replication slave on *.* to 'myslave'@'192.168.200.%' identified by '123123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;   //刷新授权表
Query OK, 0 rows affected (0.00 sec)

3、查看日志位置

MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File                | Position | binlog_Do_DB | binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| MysqL-binlog.000003 |      615 |              |                  |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

 

四、配置slave从服务器

1、从库连接主库进行测试

[root@MysqL-s1 ~]# MysqL -u myslave -p123123 -h 192.168.200.111

-----------------------------------------------------------

[root@MysqL-s2 ~]# MysqL -u myslave -p123123 -h 192.168.200.111

2、修改配置文件(开启中继日志)

[root@MysqL-s1 ~]# vim /etc/my.cnf
server-id       = 2
relay-log=relay-log-bin 
relay-log-index=slave-relay-bin.index

[root@MysqL-s1 ~]# systemctl restart mariadb

-----------------------------------------------

[root@MysqL-s2 ~]# vim /etc/my.cnf
server-id       = 3
relay-log=relay-log-bin 
relay-log-index=slave-relay-bin.index

[root@MysqL-s2 ~]# systemctl restart mariadb

3、

[root@MysqL-s1 ~]# MysqL -uroot -p

MariaDB [(none)]> stop slave;   //停掉自己从slave的角色
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> CHANGE MASTER TO
    MASTER_HOST='192.168.200.111',
    MASTER_USER='myslave',
    MASTER_PASSWORD='123123',
    MASTER_LOG_FILE='MysqL-binlog.000003',  //日志文件的位置
    MASTER_LOG_POS=615;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.111
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: MysqL-binlog.000003
          Read_Master_Log_Pos: 615
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 532
        Relay_Master_Log_File: MysqL-binlog.000003
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes

-----------------------------------------------------------------------

[root@MysqL-s2 ~]# MysqL -uroot -p

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> CHANGE MASTER TO
    MASTER_HOST='192.168.200.111',
    MASTER_USER='myslave',
    MASTER_PASSWORD='123123',
    MASTER_LOG_FILE='MysqL-binlog.000003',
    MASTER_LOG_POS=615;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.111
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: MysqL-binlog.000003
          Read_Master_Log_Pos: 615
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 532
        Relay_Master_Log_File: MysqL-binlog.000003
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes

============================================================================
reset slave //清除所有,相当于恢复出厂设置

 

五、测试

1、在master主机上创建一个crushlinux的库

MariaDB [(none)]> create database crushlinux;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crushlinux         |
| MysqL              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

2、查看两台从slave主机

第一台:
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crushlinux         |
| MysqL              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

------------------------------------------------------------------------
第二台:
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crushlinux         |
| MysqL              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐