MySQL主主复制搭建

一.工作环境及条件

主数据库1(master1):172.25.0.254

主数据库2(master2):172.25.0.11

操作系统: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服务

[root@master1 ~]# systemctl start mariadb

[root@master2 ~]# systemctl start mariadb

关闭防火墙(或者开发防火墙的mysql服务和3306端口)

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

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

二.配置方法

master1

修改主库my.cnf主要设置个不一样的ID,以及同步的数据库的名字和端口号

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

server-id=1

log-bin=lhb

port=3306

重启服务使配置生效

[root@master1 ~]# systemctl restart mariadb

登录主库赋予从库权限账号,允许用户在主库上读取日志(用户名: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

显示主数据看的信息

MariaDB [(none)]> show master status;

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

| File      | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| lhb.000002 |      245 |              |                  |

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

1 row in set (0.00 sec)

设置同步

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主要设置个不一样的ID,以及同步的数据库的名字和端口号

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

server-id=2

log-bin=lhb

port=3306

重启服务使配置生效

[root@master2 ~]# systemctl restart mariadb

登录主库赋予从库权限账号,允许用户在主库上读取日志(用户名: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

显示主数据看的信息

MariaDB [(none)]> show master status;

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

| File      | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| lhb.000002 |      245 |              |                  |

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

1 row in set (0.00 sec)

设置同步

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的数据库,看看在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的数据库,表示MySQL主主搭建成功^_^^_^^_^!!!

你可能感兴趣的:(MySQL主主复制搭建)