一.工作環(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的主機(jī)上用命令作如下去驗證下是否可以進(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
修改主庫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的主機(jī)上用命令作如下去驗證下是否可以進(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 兩個主機(jī)同時敲下
MariaDB [(none)]> slave start;
查看是否成功
MariaDB [(none)]> show slave status\G;
Slave_IO_Running: Yes
? ? Slave_SQL_Running: Yes
兩個為Yes表示成功(兩臺主機(jī)都需要查看)
測試:在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主主搭建成功^_^^_^^_^!!!