2019.10.15 部署MySQL主从异步复制

1、部署MySQL主从异步复制mysql

三台虚拟机:192.168.200.111(主)sql

192.168.200.112(从)vim

192.168.200.113(从)安全

一、全部虚拟机关闭防火墙及安全机制服务器

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

二、在主服务器上配置NTP时间同步服务器ide

[root@localhost ~]# yum -y install ntp测试

[root@localhost ~]# vim /etc/ntp.conf spa

server 127.127.1.0
fudge 127.127.1.0 stratum 8rest

启动NTP服务:

[root@localhost ~]# systemctl enable ntpd
[root@localhost ~]# systemctl start ntpd

三、在两个从服务器上配置与主服务器进行时间同步

[root@localhost ~]# yum -y install ntpdate

[root@localhost ~]# ntpdate 192.168.200.111

15 Oct 12:18:55 ntpdate[38198]: step time server 192.168.200.111 offset -0.569210 sec

四、在全部的服务器上操做安装mariadb和mariadb-server

[root@localhost ~]# yum -y install mariadb mariadb-server

五、配置主服务器

(1)[root@localhost ~]# vim /etc/ntp.conf 

disable monitor
server 127.127.1.0
fudge 127.127.1.0 stratum 8

(2)重启服务器:[root@localhost ~]# systemctl restart mariadb

[root@localhost ~]# 

(3)建立 Replication用户

[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.41-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> grant replication slave on *.* to 'myslave'@'192.168.200.11_' identified by '123';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

(4)得到Master DB的相关信息

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 476 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

备份MySQL原有的数据,若是没有就忽略

六、在两台从服务器上的配置

(1)作一个链接测试:

[root@localhost ~]# mysql -umyslave -p123 -h 192.168.200.111
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.41-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

(2)修改MySQL配置文件

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

relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
server-id=2

[root@localhost ~]# systemctl restart mariadb

(3)两台从服务器链接MySQL环境

[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.41-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

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

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='123',master_log_file='mysql-bin.000001',master_log_pos=2462;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 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-bin.000001
Read_Master_Log_Pos: 476
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

(4)全部主机执行show databases;结果是同样都有四个库,只在主服务器上建立一个库

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

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

两台从服务器上也有这个库:

 

 

相关文章
相关标签/搜索