一、部署MySQL主从异步复制

三台虚拟机:192.168.200.111(主)

192.168.200.112(从)

192.168.200.113(从)

1、所有虚拟机关闭防火墙及安全机制

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

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

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

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

server 127.127.1.0
fudge 127.127.1.0 stratum 8

启动NTP服务:

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

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

[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

4、在所有的服务器上操作安装mariadb和mariadb-server

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

5、配置主服务器

(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原有的数据,如果没有就忽略

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

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

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