演示一下在MySQL 5.7下搭建多主一从的过程

下面演示一下在MySQL 5.7下搭建多主一从的过程:

实验环境:

Master_1: 192.168.10.128

Master_2: 192.168.10.129

Slave_3:  192.168.10.130

一、分别在Master_1和Master_2上导出需要同步的数据库:

在Master_1:

[[email protected]_1 mysql]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --databases  --add-drop-database  xuanzhi  >xuanzhi.sql

在Master_2:

[[email protected]_2 mysql]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --databases  --add-drop-database  xuanzhi_2  >xuanzhi_2.sql

把分别把备份scp到Slave上:

[[email protected]_1 mysql]# scp -P22 xuanzhi.sql 192.168.10.130:/data/service/mysql/

[[email protected]_2 mysql]# scp -P22 xuanzhi_2.sql 192.168.10.130:/data/service/mysql/

二、在Master_1和Master_2上创建复制账号,这个操作跟MySQL 5.7之前版本一样:

在Master_1:

<Master_1>[(none)]> grant replication slave on *.* to ‘repl‘@‘192.168.10.130‘ identified by ‘123456‘;

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

在Master_2:

<Master_2> [(none)]> grant replication slave on *.* to ‘repl‘@‘192.168.10.130‘ identified by ‘123456‘;

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

三、分别Slave上把Master_1和Master_2的数据导入Slave服务器,在导入前先修改MySQL存储master-info和relay-info的方式,即从文件存储改为表存储,在my.cnf里添加以下选择:

master_info_repository=TABLE

relay_log_info_repository=TABLE

也可以在线修改,灰常方便:

<Slave> [(none)]> stop slave;

Query OK, 0 rows affected (0.02 sec)

<Slave> [(none)]> SET GLOBAL master_info_repository = ‘TABLE‘;

Query OK, 0 rows affected (0.00 sec)

<Slave> [(none)]> SET GLOBAL relay_log_info_repository = ‘TABLE‘;

Query OK, 0 rows affected (0.00 sec)

<Slave> [(none)]>

更多的详细解析可以参考:http://dev.mysql.com/doc/refman/5.7/en/slave-logs.html

下面进行数据导入:

[[email protected] mysql]# mysql -uroot -p  <./xuanzhi.sql

[roo[email protected] mysql]# mysql -uroot -p123456  <./xuanzhi_2.sql

分别找出Master_1和Master_2的binlog位置和Pos位置:

[[email protected] mysql]# cat xuanzhi.sql |grep " CHANGE MASTER"

-- CHANGE MASTER TO MASTER_LOG_FILE=‘Master_1-bin.000001‘, MASTER_LOG_POS=1539;

[[email protected] mysql]# cat xuanzhi_2.sql |grep " CHANGE MASTER"

-- CHANGE MASTER TO MASTER_LOG_FILE=‘Master_2-bin.000003‘, MASTER_LOG_POS=630;

[[email protected] mysql]#

四、登录Slave进行同步操作,分别change master到两台Master服务器,后面以FOR CHANNEL ‘CHANNEL_NAME‘区分

<Slave> [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.10.128‘,MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘Master_1-bin.000001‘,MASTER_LOG_POS=1539 FOR CHANNEL ‘Master_1‘;

Query OK, 0 rows affected, 2 warnings (0.05 sec)

<Slave> [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.10.129‘,MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘Master_2-bin.000003‘,MASTER_LOG_POS=630 FOR CHANNEL ‘Master_2‘;

Query OK, 0 rows affected, 2 warnings (0.04 sec)

进行启动slave操作,可以通过start slave的方式去启动所有的复制,也可以通过启动单个复制源的方式,下面进行单个复制源的启动进行演示(停止也是一样):

<Slave> [(none)]> start slave for CHANNEL  ‘Master_1‘;

Query OK, 0 rows affected (0.01 sec)

<Slave> [(none)]> start slave for CHANNEL  ‘Master_2‘;

Query OK, 0 rows affected (0.02 sec)

正常启动后,可以查看同步的状态:执行SHOW SLAVE STATUS FOR CHANNEL ‘channel_name‘\G

查看复制源Master_1的同步状态:

<Slave> [(none)]> SHOW SLAVE STATUS FOR CHANNEL ‘Master_1‘\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.10.128

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: Master_1-bin.000001

Read_Master_Log_Pos: 1987

Relay_Log_File: localhost-relay-bin-master_1.000002

Relay_Log_Pos: 771

Relay_Master_Log_File: Master_1-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 1987

Relay_Log_Space: 991

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 100128

Master_UUID: 44b653d4-8843-11e5-b97e-000c29dfaaf7

Master_Info_File: mysql.slave_master_info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name: master_1

Master_TLS_Version:

1 row in set (0.00 sec)

查看复制源Master_2的同步状态:

<Slave> [(none)]> SHOW SLAVE STATUS FOR CHANNEL ‘Master_2‘\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.10.129

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: Master_2-bin.000003

Read_Master_Log_Pos: 1078

Relay_Log_File: localhost-relay-bin-master_2.000002

Relay_Log_Pos: 771

Relay_Master_Log_File: Master_2-bin.000003

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 1078

Relay_Log_Space: 991

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 100129

Master_UUID: 583f5433-43ef-11e5-8958-000c29d5bdfa

Master_Info_File: mysql.slave_master_info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name: master_2

Master_TLS_Version:

1 row in set (0.00 sec)

也可以通过查看performance_schema相关的表查看同步状态,执行命令:SELECT * FROM performance_schema.replication_connection_status; 监控复制状态。

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

| CHANNEL_NAME | GROUP_NAME | SOURCE_UUID                          | THREAD_ID | SERVICE_STATE | COUNT_RECEIVED_HEARTBEATS | LAST_HEARTBEAT_TIMESTAMP | RECEIVED_TRANSACTION_SET | LAST_ERROR_NUMBER | LAST_ERROR_MESSAGE | LAST_ERROR_TIMESTAMP |

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

| master_1     |            | 44b653d4-8843-11e5-b97e-000c29dfaaf7 |        34 | ON            |                       184 | 2015-08-14 08:06:10      |                          |                 0 |                    | 0000-00-00 00:00:00  |

| master_2     |            | 583f5433-43ef-11e5-8958-000c29d5bdfa |        36 | ON            |                       183 | 2015-08-14 08:06:24      |                          |                 0 |                    | 0000-00-00 00:00:00  |

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

2 rows in set (0.00 sec)

<Slave> [(none)]>

五、验证数据是否同步

在Master_1上插入两条数据:

<Master_1>[xuanzhi]> insert into tb1(name)  values (‘user1‘),(‘user2‘);

Query OK, 2 rows affected (0.01 sec)

在Master_2上插入两条数据:

<Master_2> [xuanzhi_2]> insert into tb2(name) values (‘user3‘),(‘user4‘);

Query OK, 2 rows affected (0.04 sec)

回到Slave上查看数据是否正常把数据同步过来了:

<Slave> [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| xuanzhi            |

| xuanzhi_2          |

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

6 rows in set (0.03 sec)

<Slave> [(none)]> select * from xuanzhi.tb1;

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

| id | name  |

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

|  1 | user1 |

|  2 | user2 |

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

2 rows in set (0.00 sec)

<Slave> [(none)]> select * from xuanzhi_2.tb2;

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

| id | name  |

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

|  1 | user3 |

|  2 | user4 |

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

2 rows in set (0.00 sec)

成功的实现了多主一从的环境搭建

时间: 2024-11-08 20:19:33

演示一下在MySQL 5.7下搭建多主一从的过程的相关文章

MySql集群讲解(二)Mysql Liunx环境下搭建

MySql集群讲解(二) MySQL Linux环境下搭建 A:下载: wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz B:安装前环境检查: 1.检查Linux是否安装了mariadb数据库,mariadb数据库是mysql的分支,执行命令: yum list installed | grep mariadb rpm -qa | grep mariadb 2.若Lin

MySQL命令窗口下中文显示乱码的解决过程

在dos客户端输出窗口中查询表中的数据,还有项目部署到服务器上时前台的页面,中文数据都显示成乱码,如下图所示: 这个问题困扰了我一天,后来解决了才发现原来我的方向错了,一直我以为是SpringBoot项目的原因,因为之前我已经检查过数据库这边的字符集问题,把所有字符集更改成utf8,包括这个character-set-result=utf8,如下图: 结果后来仔细一查才发现,原来我的win7默认使用字符集是GB2312,所以在输出窗口使用的字符集不是UTF8而是GB2312,更改后如下图: 注:

Windows下搭建PHP开发环境,整合Apache+PHP+MySQL(举例软件为32位)

原文来自:http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html 在原作者的基础上,只是做了详细的解释.避免走弯路.(注:红色字体为本人加的) 一.准备工作-下载所需软件 Apache  httpd-2.2.22-win32-x86-openssl-0.9.8t.msi PHP       php-5.3.10-Win32-VC9-x86.zip MySQL   mysql-5.5.20-win32.msi 二.安装软件 安装A

Linux平台下搭建freeRADIUS集成MySQL并整合VPN

昨天分享了自己在Ubuntu下搭建L2TP VPN服务器的历程,并贴出了自己测试运行的效果图片,这个比较适合个人购买服务器搭建VPN.但是搭建VPN后,无法实现AAA功能,管理账号也是利用文件.为此,选择搭建freeRADIUS同时与MySQL整合,同样选择Ubuntu平台.闲话少说 ,就是干....... 1)部署freeradius服务器 @1主要文件作用: radiusd.conf 和sql.conf  作用为radius与mysql 接连,client.conf 是NAS 和freera

ubuntu14.04下搭建python+mysql环境

简略记录ubuntu14.04下搭建python操作的mysql服务器的过程和其中遇到的问题及解决方法. 第一部分: 安装mysql 安装步骤:1. sudo apt-get install mysql-server 2. sudo  apt-get isntall mysql-client 3. sudo apt-get install libmysqlclient-dev 检查是否安装成功: 输入mysql -u root -p 命令检查mysql是否安装成功,该命令输入后会提示输入密码,此

linux下搭建nginx+php(FastCGI)+mysql运行环境

一.安装环境 1.CentOS5.5 2.php5.4 3.MySQL5.5.19 二.安装程序依赖库和开发环境 为了省事把所需要的库文件全都安装上,可以使用rpm包安装,也可以用yum命令安装, 1 yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc gli

在64位Ubuntu下搭建Java web +mysql

今天搭建了一个ubuntu 64位的tomcat + java + mysql的环境,现在总结一下: 1.下载jdk 地址: http://download.oracle.com/otn-pub/java/jdk/7u51-b13/jdk-7u51-linux-x64.tar.gz?AuthParam=1394967394_1531ce17d13be0962e25ec8fe3e45f1d 安装: tar xvf 解压刚下载的jdk压缩包. 然后到/etc/profile(注释:使用vi命令)文件

Windows下搭建MySql Master-Master Replication

1.首先下载最新版的MySql Server (http://dev.mysql.com/downloads/windows/installer/) 2.安装MySql Server到两台机器上 MySql 1: 192.168.0.104 (以下简称104) MySql 2: 192.168.0.103  (以下简称103) 3. 配置Mysql Server启动Binary Logging. 在104的my.ini文件(一般在C:\ProgramData\MySql\MySql Server

ubuntu 13.04下MYSQL 5.5环境搭建

解决的问题: 安装mysql server和mysql client 5.5 新建远程账户 远程访问权限 MYSQL默认字符集修改为UTF8 检查防火墙 一.安装 BTW:可以使用查找命令查看安装包 sudo apt- 安装命令 sudo apt-get install mysql-server-5.5  回车  (有一个带core的,) sudo apt-get install mysql-client-5.5 然后,输入mysql可查看 二.新建远程账户并赋与权限 grant all pri