MySQL主主復(fù)制搭建

一.工作環(huán)境及條件

主數(shù)據(jù)庫1(master1):172.25.0.254

主數(shù)據(jù)庫2(master2):172.25.0.11

操作系統(tǒng):RHRL7

MySQL版本:mariadb-5.5.35

安裝MYSQL(yum源安裝)

master1

[root@master1 ~]# yum clean all

[root@master1 ~]# yum -y install mariadb*

master2

[root@master2 ~]# yum clean all

[root@master2 ~]# yum -y install mariadb*

開啟mariadb服務(wù)

[root@master1 ~]# systemctl start mariadb

[root@master2 ~]# systemctl start mariadb

關(guān)閉防火墻(或者開發(fā)防火墻的mysql服務(wù)和3306端口)

[root@master1 ~]# systemctl stop firewalld.service

[root@master2 ~]# systemctl stop firewalld.service

二.配置方法

master1

修改主庫my.cnf主要設(shè)置個不一樣的ID,以及同步的數(shù)據(jù)庫的名字和端口號

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

server-id=1

log-bin=lhb

port=3306

重啟服務(wù)使配置生效

[root@master1 ~]# systemctl restart mariadb

登錄主庫賦予從庫權(quán)限賬號,允許用戶在主庫上讀取日志(用戶名:admin,密碼:123456)

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

MariaDB [(none)]> grant replication slave on *.* to 'admin'@'172.25.0.11' identified by '123456';

我們可以在master2的主機上用命令作如下去驗證下是否可以進入master1的MySQL

[root@master2 ~]# mysql -uadmin -p'123456' -h 172.25.0.254

顯示主數(shù)據(jù)看的信息

MariaDB [(none)]> show master status;

+------------+----------+--------------+------------------+

| File? ? ? | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------+----------+--------------+------------------+

| lhb.000002 |? ? ? 245 |? ? ? ? ? ? ? |? ? ? ? ? ? ? ? ? |

+------------+----------+--------------+------------------+

1 row in set (0.00 sec)

設(shè)置同步

MariaDB [(none)]> slave stop;

Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> change master to master_host='172.25.0.11',master_user='admin',master_password='123456',master_log_file='lhb.000002',master_log_pos=245;

Query OK, 0 rows affected (0.03 sec)

master2

修改主庫my.cnf主要設(shè)置個不一樣的ID,以及同步的數(shù)據(jù)庫的名字和端口號

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

server-id=2

log-bin=lhb

port=3306

重啟服務(wù)使配置生效

[root@master2 ~]# systemctl restart mariadb

登錄主庫賦予從庫權(quán)限賬號,允許用戶在主庫上讀取日志(用戶名:admin,密碼:123456)

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

MariaDB [(none)]> grant replication slave on *.* to 'admin'@'172.25.0.254' identified by '123456';

我們可以在master1的主機上用命令作如下去驗證下是否可以進入master1的MySQL

[root@master1 ~]# mysql -uadmin -p'123456' -h 172.25.0.11

顯示主數(shù)據(jù)看的信息

MariaDB [(none)]> show master status;

+------------+----------+--------------+------------------+

| File? ? ? | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------+----------+--------------+------------------+

| lhb.000002 |? ? ? 245 |? ? ? ? ? ? ? |? ? ? ? ? ? ? ? ? |

+------------+----------+--------------+------------------+

1 row in set (0.00 sec)

設(shè)置同步

MariaDB [(none)]> slave stop;

MariaDB [(none)]> change master to master_host='172.25.0.254',master_user='admin',master_password='123456',master_log_file='lhb.000002',master_log_pos=245;

在master1 和master2 兩個主機同時敲下

MariaDB [(none)]> slave start;

查看是否成功

MariaDB [(none)]> show slave status\G;

Slave_IO_Running: Yes

? ? Slave_SQL_Running: Yes

兩個為Yes表示成功(兩臺主機都需要查看)

測試:在master1建立lhbdb的數(shù)據(jù)庫,看看在master2是否會同步

master1

MariaDB [(none)]> create database lhbdb;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database? ? ? ? ? |

+--------------------+

| information_schema |

| lhbdb? ? ? ? ? ? ? |

| mysql? ? ? ? ? ? ? |

| performance_schema |

| test? ? ? ? ? ? ? |

+--------------------+

5 rows in set (0.01 sec)

master2

MariaDB [(none)]> show databases;

+--------------------+

| Database? ? ? ? ? |

+--------------------+

| information_schema |

| lhbdb? ? ? ? ? ? ? |

| mysql? ? ? ? ? ? ? |

| performance_schema |

| test? ? ? ? ? ? ? |

+--------------------+

5 rows in set (0.01 sec)

由此可以看到master2 能夠到看lhbdb的數(shù)據(jù)庫,表示MySQL主主搭建成功^_^^_^^_^!!!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容