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)

兩臺從服務器上也有這個庫:

 

 

相關文章
相關標籤/搜索