Home Database Mysql Tutorial Mysql主从复制搭建_MySQL

Mysql主从复制搭建_MySQL

May 27, 2016 pm 01:45 PM
mysql master-slave replication

环境简介

主服务器:CentOS6.5下mysql5.6.30

安装请参考这里网址:http://blog.csdn.net/hsd2012/article/details/51232612

从服务器:win7下5.6.17

原理简介及优缺点

MySQL从3.23版本开始提供复制功能,复制主要是指将主服务器的DDL和DML操作,通过二进制日志(binLog日志),传到服务的服务器上,然后在从服务器上对这些日志从新执行,从而实现从服务器与主服务器的数据同步。MySQL支持一台主服务器同时向多台从服务器进行复制,从服务器同时也可以作为其他服务器的主服务器,实现链状的复制。

优点

如果主服务器出现问题,可以快速切换到从服务器提供服务。 可以在从服务器上执行查询,降低主服务器的压力 可以在从服务器上执行备份,以避免备份期间影响主服务器的性能

局限

由于MySQL实现的是异步复制,所以主从服务器之间的数据存在一定差异,对实时性要求高的数据仍然需要从主服务器上获得。

前期准备

1.在linux下创建账号

在win的命令提示符中输入ipconfig,查看ip

 

这里写图片描述

 

因为我的win下ip为192.168.153.1,所以,我创建将Host设置为192.168.153.%,关于原因,可参考这里

 

这里写图片描述

 

2.赋予权限

 

这里写图片描述

 

REPLICATION SLAVE权限针对所有的数据库,只能通过. ,而不能shool.*,因为REPLICATION SLAVE是复制binlog日志。

 

这里写图片描述

 

3.将主服务器中的数据,复制到从服务器,确保两者搭建主从之前,数据一致。关于这,之前写过一篇文章专门介绍,可以参考这里

配置

主服务器配置

1.开启binlog日志,并设置server_id

 

这里写图片描述

 

从服务器配置

1.配置server_id

设置:server-id=2

 

这里写图片描述

 

2.指定主服务器配置格式如下

change master to

master_host=’master_host_name’, //从服务器的主机

master_user=’replication_user_name’,//执行复制用户名

master_password=’replication_password’,//执行复制用户密码

master_log_file=’recorded_log_file_name’,//二进制日志文件名

master_log_pos=’recorded_log_postion’;//复制开始位置

根据我的配置信息

bin-log信息

 

这里写图片描述

 

复制用户信息

 

这里写图片描述

 

我执行的配置代码如下:

change master to

master_host=’192.168.153.140’,

master_user=’lidequan’,

master_password=’lidequan’,

master_log_file=’bin-log.000003’,

master_log_pos=120;

 

这里写图片描述

 

查看从服务器是否已连接主服务器

执行start slave;

 

这里写图片描述

 

执行show processlist;

 

这里写图片描述

 

修改主服务器数据,查看同步效果

在这里修改主服务器数据,主要是执行插入,更新操作。

操作之前,数据信息如下

 

这里写图片描述

 

执行插入操作

insert into `class` (`name`) values ('三年二班'),('三年五班'),('三年七班');

 

这里写图片描述

 

此时从服务器中也有数据了

 

这里写图片描述

 

执行更新操作

update class set `name`='三年三班' where id=3;

观察下图,发现数据确实发生了变化

 

这里写图片描述

 

删除操作

观察下图,数据也是同步的

 

这里写图片描述

 

数据表定义操作

1.添加一个student表

CREATE TABLE student(

`id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT'学生编号',

`name` CHAR(10) NOT NULL DEFAULT '' COMMENT'学生名',

`class_id` INT(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT'班级编号',

PRIMARY KEY(`id`)

) ENGINE=MYISAM DEFAULT CHARSET=utf8;

 

这里写图片描述

 

2.往student表中添加一个字段

ALTER TABLE student ADD COLUMN age TINYINT(2) UNSIGNED NOT NULL DEFAULT 0 COMMENT'学生年龄';

 

这里写图片描述

 

指定负责的数据库或者表

可以通过replicate-do-db、replicate-do-table、replicate-ignore-db、replicate-ignore-table或者replicate-wild-do-table来指定主从数据库复制到从数据库的数据库或者表。

关于数据复制的一些重要参数

在上面搭建主从服务器的时候,使用了MASTER_HOST,MASTER_PORT,MASTER_USER,MASTER_PASSWORD,MASTER_LOG_FILE,MASTER_LOG_POS这些参数都要在从服务器上配置,下面再来说几个常用的启动选项,如log_slave_updates、read_only、master_verify_checksum

log_slave_updates

log_slave_updates这个参数用来配置从服务器上的更新操作是否写入二进制日志,默认是不打开的。

首先我们来看一下刚刚win下mysql的binlog日志内容

 

这里写图片描述

 

可以发现,刚刚我们执行了增、删、改等操作,它并没有记录。

我们可以看到log_slave_updates是没有启动的

 

这里写图片描述

 

且该属性是只读属性,不可以动态的设置,只能在配置文件中设置,如下图设置将会报错

 

这里写图片描述

 

read_only

read-only选项:对所有的非临时表进行只读控制。但是有两种特殊情况

1. 对replication threads例外,以保证slave能够正常的进行replication。

2. 对于拥有super权限的用户,可以ignore这个选项。

 

这里写图片描述

 

当以没有拥有super权限的用户登录时候,会提示如下:

 

这里写图片描述

 

这样就确保了从数据只负责读数据操作,而拒绝写数据的操作。

补充:

SUPER 权限 :

1. 可以有change master to, kill其他用户的线程的权限。

2. Purge binary logs 来删除binary log, set global来动态设置变量的权限。

3. 执行mysqladmin debug命令,开启或者关闭log,在read-only打开时执行update/insert操作。

4. 执行start slave, stop slave.

5. 当连接数已经达到max_connections的最大值时,也可以连接到server。

master_verify_checksum

由于软硬件或者网络传输出错,导致主服务器上运行的sql语句与从服务器上运行的sql语句不一致,很难找到问题原因,mysql的开发人员在 5.6 Milestone Development Release版本中加入了 replication event checksum(主从复制事件校验)功能。master_verify_checksum主要用于复制事件校验。当一个event被写入binary log(二进制日志)的时候,checksum也同时写入binary log,然后在event通过网络传输到从服务器(slave)之后,再在从服务器中对其进行验证并写入从服务器的relay log。由于每一步都记录了event和checksum,所以我们可以很快地找出问题所在。

管理与维护

查看从服务器状态

使用show slave stauts;

 

这里写图片描述

 

在查看这些信息中,比较重要的是”slave_io_runing”和”slave_sql_runing”这两个进程

slave_io_runing :此进程负责从服务器从主服务器上读取Binlog日志,并写入从服务器上的中继日志中。

Slave_SQL_Runing:此进程负责读取并执行中继日志中的binlog日子。

只要期中有一个进程的状态时no,则表示复制进程停止。

总结

主从配置

一、主服务器上配置

1.创建用户,并赋予REPLICATION SLAVE权限

2.开启binlog日志,并设置server_id

二、从服务器配置

1.指定server_id

2.指定主服务器配置

备注:

1.win下mysql开启与关闭(前提是需要配置path路径)

 

这里写图片描述

 

2.server-id做什么用的

mysql的同步的数据中是包含server-id的,用于标识该语句最初是从哪个server写入的,所以server-id一定要有的,如果设置多个从服务器,每个从服务器必须有一个唯一的server-id值,必须与主服务器的以及其它从服务器的不相同。 每一个同步中的slave在master上都对应一个master线程,该线程就是通过slave的server-id来标识的;每个slave在master端最多有一个master线程,如果两个slave的server-id 相同,则后一个连接成功时,前一个将被踢掉。 在mysql做主从同步时,要保证一条数据不会陷入死循环,这里就是靠server-id来实现的。

 以上就是Mysql主从复制搭建_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles