一.工作環(huán)境及條件
主數(shù)據(jù)庫(kù)1(master1):172.25.0.254
主數(shù)據(jù)庫(kù)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*
開(kāi)啟mariadb服務(wù)
[root@master1 ~]# systemctl start mariadb
[root@master2 ~]# systemctl start mariadb
關(guān)閉防火墻(或者開(kāi)發(fā)防火墻的mysql服務(wù)和3306端口)
[root@master1 ~]# systemctl stop firewalld.service
[root@master2 ~]# systemctl stop firewalld.service
二.配置方法
master1
修改主庫(kù)my.cnf主要設(shè)置個(gè)不一樣的ID,以及同步的數(shù)據(jù)庫(kù)的名字和端口號(hào)
[root@master1 ~]# vim /etc/my.cnf
server-id=1
log-bin=lhb
port=3306
重啟服務(wù)使配置生效
[root@master1 ~]# systemctl restart mariadb
登錄主庫(kù)賦予從庫(kù)權(quán)限賬號(hào),允許用戶(hù)在主庫(kù)上讀取日志(用戶(hù)名:admin,密碼:123456)
[root@master1 ~]# mysql -uroot -p123456
MariaDB [(none)]> grant replication slave on *.* to 'admin'@'172.25.0.11' identified by '123456';
我們可以在master2的主機(jī)上用命令作如下去驗(yàn)證下是否可以進(jìn)入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
修改主庫(kù)my.cnf主要設(shè)置個(gè)不一樣的ID,以及同步的數(shù)據(jù)庫(kù)的名字和端口號(hào)
[root@master2 ~]# vim /etc/my.cnf
server-id=2
log-bin=lhb
port=3306
重啟服務(wù)使配置生效
[root@master2 ~]# systemctl restart mariadb
登錄主庫(kù)賦予從庫(kù)權(quán)限賬號(hào),允許用戶(hù)在主庫(kù)上讀取日志(用戶(hù)名:admin,密碼:123456)
[root@master2 ~]# mysql -uroot -p123456
MariaDB [(none)]> grant replication slave on *.* to 'admin'@'172.25.0.254' identified by '123456';
我們可以在master1的主機(jī)上用命令作如下去驗(yàn)證下是否可以進(jìn)入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 兩個(gè)主機(jī)同時(shí)敲下
MariaDB [(none)]> slave start;
查看是否成功
MariaDB [(none)]> show slave status\G;
Slave_IO_Running: Yes
? ? Slave_SQL_Running: Yes
兩個(gè)為Yes表示成功(兩臺(tái)主機(jī)都需要查看)
測(cè)試:在master1建立lhbdb的數(shù)據(jù)庫(kù),看看在master2是否會(huì)同步
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ù)庫(kù),表示MySQL主主搭建成功^_^^_^^_^!!!