部署MySQL-主從異步複製

實驗環境  三臺虛擬機 一主兩從mysql

主:     192.168.200.111sql

從1:  192.168.200.112數據庫

從2:  192.168.200.113vim

[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

末行添加rest

server 127.127.1.0日誌

fudge 127.127.1.0 startum 8

[root@localhost ~]# systemctl restart ntpd

在兩臺從服務器上同步時間

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

[root@localhost ~]# ntpdate 192.168.200.111
15 Oct 12:02:12 ntpdate[10406]: adjust time server 192.168.200.111 offset 0.008413 sec

slave2服務器與slave1服務器相同

在全部的服務器上操做安裝mariadb和mariadb-server

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

配置MySQL Master主服務器

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

[mysqld]

server-id=1                      //三臺不要相同
log-bin=mysql-bin
log-slave-updates=true   //手動添加,開啓從日誌

[root@localhost ~]# systemctl restart mariadb

給從服務器受權

[root@localhost ~]# mysql -uroot -p123456

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

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

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 | 474 |                 |                        |                     //數據後面要用到
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

備份以前主服務器有數據須要先傳給從服務器,沒有就忽略。

主:mysqldump -uroot -all-databases > /root/alldbacup.sql

scp傳給從服務器

從:mysql -uroot -p <  /root/alldbacup.sql

配置從服務器

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

[mysqld]

server-id=2                                               //id號不能相同
relay-log=relay-log-bin                              //開啓中繼日誌
relay-log-index=slave-relay-bin.index 

[root@localhost ~]# systemctl restart mariadb

[root@localhost ~]# mysql -uroot -p123456

MariaDB [(none)]> stop slave;                       //把本身從的角色功能先停掉
Query OK, 0 rows affected, 1 warning (0.01 sec)

MariaDB [(none)]> change master to -> master_host='192.168.200.111',-> master_user='root',-> master_password='123456',-> master_log_file='mysql-bin.000010',-> master_log_pos=474;

Query OK, 0 rows affected (0.01 sec)

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

參數說明:

CHANGE MASTER TO

MASTER_HOST='master_host_name',                    //主服務器的IP地址

MASTER_USER='replication_user_name',              //主服務器受權的用戶

MASTER_PASSWORD='replication_password',      //主服務器受權的密碼

MASTER_LOG_FILE='recorded_log_file_name',    //主服務器二進制日誌的文件名

MASTER_LOG_POS=recorded_log_position;          //日誌文件的開始位置

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: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 474
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

........................................

經過查看slave狀態,確保Slave_IO_Running: Yes      Slave_SQL_Running: Yes

slave2配置相同

測試:

在主服務器建立數據庫,在從服務器查看是否同步

主:

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

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

從:

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

相關文章
相關標籤/搜索