10分钟实现MariaDB多源复制(多主一丛)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

 

 环境:

192.168.1.248 HE1 主库

192.168.1.249 HE2 主库

192.168.1.250 HE3 从库

 

主库授权备份账户

1
2
mysql>   grant  SELECT ,RELOAD,SHOW DATABASES,SUPER,LOCK TABLES,REPLICATION CLIENT,SHOW  VIEW ,EVENT,FILE  on  *.*  to  backup@ 'localhost'  identified  by  'MANAGER' ;
mysql> flush  privileges ;

 

 

建立主从复制的用户名和密码,指定哪个IP地址用户使用这个用户可以访问主库

1
2
mysql>   grant  replication client,replication slave  on  *.*  to  'mysync' @ '192.168.1.%'  identified  by  'MANAGER' ;
mysql> flush  privileges ;

   

 

 

主库全库备份

[root@HE1 ~]# mysqldump -ubackup -p  --single-transaction --databases 248db --master-data=2 >248.sql

[root@HE2 ~]# mysqldump -ubackup -p  --single-transaction --databases 249db --master-data=2 >249.sql

 

拷贝主库备份文件到从库

[root@HE1 ~]# scp -rp 248.sql root@192.168.1.250:/root

[root@HE2 ~]# scp -rp 249.sql root@192.168.1.250:/root

 

从库还原

[root@HE3 ~]# mysql -uroot -p <248.sql

Enter password:

[root@HE3 ~]# mysql -uroot -p <249.sql

Enter password:

 

 

 

查看主库备份集中的binlogposition偏移量

[root@HE3 ~]# cat 248.sql |grep "CHANGE MASTER TO MASTER_LOG_FILE='"

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000017', MASTER_LOG_POS=581;

[root@HE3 ~]# cat 249.sql |grep "CHANGE MASTER TO MASTER_LOG_FILE='"

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=581;

 

从库:

在从库配置读取主库的IP地址,复制的用户名和密码,从主库哪个BINLOG文件开始读取,偏移量是多少

1
MariaDB [(none)]>CHANGE MASTER   'Master248'  TO  MASTER_HOST= '192.168.1.248' ,MASTER_USER= 'mysync' ,MASTER_PASSWORD= 'MANAGER' ,MASTER_PORT=3306,MASTER_LOG_FILE= 'mysql-bin.000017' ,MASTER_LOG_POS=581;
1
MariaDB [(none)]>CHANGE MASTER   'Master249'  TO  MASTER_HOST= '192.168.1.249' ,MASTER_USER= 'mysync' ,MASTER_PASSWORD= 'MANAGER' ,MASTER_PORT=3306,MASTER_LOG_FILE= 'mysql-bin.000004' ,MASTER_LOG_POS=581;

 

开启从库复制开关

1
2
3
4
5
6
7
MariaDB [(none)]> start slave  'Master248' ;
  
MariaDB [(none)]> show slave  'Master248'  status\G
  
MariaDB [(none)]> start slave  'Master249' ;
  
MariaDB [(none)]> show slave  'Master249'  status\G

 

验证从库状态是否正常主要看下面这两个状态是否为yes          

  Slave_IO_Running: Yes

  Slave_SQL_Running: Yes

 

1
2
3
4
5
6
7
8
9
10
11
12
MariaDB [(none)]> show databases;
+ --------------------+
Database            |
+ --------------------+
| 248db              |
| 249db              |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+ --------------------+
rows  in  set  (0.00 sec)

 

 


248主机上创建表并插入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> use 248db;
Database  changed
mysql>  create  table  aixuan1(
     -> id  int (10) unsigned  NOT  NULL  AUTO_INCREMENT,
     -> text  varchar (20)  NOT  NULL  DEFAULT  '' ,
     ->  PRIMARY  KEY (id))
     -> ENGINE=innodb AUTO_INCREMENT=1
     ->  DEFAULT  CHARSET=utf8;
Query OK, 0  rows  affected (0.15 sec)
  
mysql>
mysql>  insert  into  aixuan1(text)  values ( 'aa' ),( 'bb' ),( 'cc' ),( 'dd' ),( 'ee' ),( 'ff' );
Query OK, 6  rows  affected (0.02 sec)
Records: 6  Duplicates: 0  Warnings: 0

 

从库验证

1
2
3
4
5
6
7
8
9
10
11
12
MariaDB [(none)]> use 248db;
Reading  table  information  for  completion  of  table  and  column  names
You can turn  off  this feature  to  get a quicker startup  with  -A
  
Database  changed
MariaDB [248db]> show tables;
+ -----------------+
| Tables_in_248db |
+ -----------------+
| aixuan1         |
+ -----------------+
1 row  in  set  (0.00 sec)

 

 

249主机上创建表并插入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> use 249db;
Database  changed
mysql>  create  table  helei1(
     -> id  int (10) unsigned  NOT  NULL  AUTO_INCREMENT,
     -> text  varchar (20)  NOT  NULL  DEFAULT  '' ,
     ->  PRIMARY  KEY (id))
     -> ENGINE=innodb AUTO_INCREMENT=1
     ->  DEFAULT  CHARSET=utf8;
Query OK, 0  rows  affected (0.15 sec)
  
mysql>
mysql>  insert  into  helei1(text)  values ( 'aaa' ),( 'bbb' ),( 'ccc' ),( 'ddd' ),( 'eee' ),( 'fff' );
Query OK, 6  rows  affected (0.02 sec)
Records: 6  Duplicates: 0  Warnings: 0

 

从库验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
MariaDB [248db]> use 249db;
Reading  table  information  for  completion  of  table  and  column  names
You can turn  off  this feature  to  get a quicker startup  with  -A
  
Database  changed
MariaDB [249db]> show tables;
+ -----------------+
| Tables_in_249db |
+ -----------------+
| helei1          |
+ -----------------+
1 row  in  set  (0.00 sec)
  
MariaDB [249db]>  select  from  helei1;
+ ----+------+
| id | text |
+ ----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
|  4 | ddd  |
|  5 | eee  |
|  6 | fff  |
+ ----+------+
rows  in  set  (0.00 sec)

 

 

至此,MariaDB多源复制搭建成功。




 本文转自 dbapower 51CTO博客,原文链接:http://blog.51cto.com/suifu/1830682,如需转载请自行联系原作者


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
SQL 监控 关系型数据库
|
6月前
|
NoSQL 关系型数据库 MySQL
阿里云RDS关系型数据库大全_MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等
阿里云RDS关系型数据库如MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等,NoSQL数据库如Redis、Tair、Lindorm和MongoDB
254 0
|
6月前
|
NoSQL 关系型数据库 MySQL
阿里云关系型数据库详细介绍MySQL/MariaDB/SQL Server/PolarDB/PostgreSQL等
阿里云关系型数据库详细介绍MySQL/MariaDB/SQL Server/PolarDB/PostgreSQL等,阿里云RDS关系型数据库如MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等
114 0
|
6月前
|
NoSQL Cloud Native 关系型数据库
阿里云RDS数据库_MySQL_SQL Server_MariaDB_PolarDB_PostgreSQL
阿里云RDS关系型数据库大全:MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等
110 0
|
存储 自然语言处理 关系型数据库
mysql/mariadb 实现全文检索
mysql/mariadb 实现全文检索
mysql/mariadb 实现全文检索
|
8月前
|
关系型数据库 MySQL API
MariaDB数据库中如何允许远程链接mysql并开放3306端口
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。
546 0
|
8月前
|
存储 关系型数据库 MySQL
mysql--Centos安装MariaDB(mysql)
mysql--Centos安装MariaDB(mysql)
1325 0
|
8月前
|
Ubuntu 关系型数据库 MySQL
Ubuntu安装MariaDB-10.3数据库(等同于Mysql-5.7)
Ubuntu安装MariaDB-10.3数据库(等同于Mysql-5.7)
192 0

推荐镜像

更多