Home Database Mysql Tutorial MYSQL主从服务器配置工作原理_MySQL

MYSQL主从服务器配置工作原理_MySQL

Jun 01, 2016 pm 01:32 PM
working principle server

bitsCN.com

MYSQL主从服务器配置工作原理

 

一、        主从配置的原理:

Mysql的 Replication 是一个异步的复制过程,从一个 Mysql instace(我们称之为 Master)复制到另一个

Mysql instance(我们称之 Slave)。在 Master 与 Slave

之间的实现整个复制过程主要由三个线程来完成,其中两个线程(Sql线程和IO线程)在 Slave 端,另外一个线程(IO线程)在 Master

端。

  要实现 MySQL 的 Replication ,首先必须打开 Master 端的Binary

Log(mysql-bin.xxxxxx)功能,否则无法实现。因为整个复制过程实际上就是Slave从Master端获取该日志然后再在自己身上完全

顺序的执行日志中所记录的各种操作。打开 MySQL 的 Binary Log 可以通过在启动 MySQL Server 的过程中使用

“—log-bin” 参数选项,或者在 my.cnf 配置文件中的 mysqld 参数组([mysqld]标识后的参数部分)增加

“log-bin” 参数项。

  MySQL 复制的基本过程如下:

  1. Slave 上面的IO线程连接上 Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容;

 

   2. Master 接收到来自 Slave 的 IO 线程的请求后,通过负责复制的 IO

线程根据请求信息读取指定日志指定位置之后的日志信息,返回给 Slave 端的 IO

线程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息在 Master 端的 Binary Log 文件的名称以及在 Binary

Log 中的位置;

  3. Slave 的 IO 线程接收到信息后,将接收到的日志内容依次写入到 Slave 端的Relay

Log文件(mysql-relay-bin.xxxxxx)的最末端,并将读取到的Master端的bin-log的文件名和位置记录到master-

info文件中,以便在下一次读取的时候能够清楚的高速Master“我需要从某个bin-log的哪个位置开始往后的日志内容,请发给我”

 

   4. Slave 的 SQL 线程检测到 Relay Log 中新增加了内容后,会马上解析该 Log 文件中的内容成为在 Master

端真实执行时候的那些可执行的 Query 语句,并在自身执行这些 Query。这样,实际上就是在 Master 端和 Slave

端执行了同样的 Query,所以两端的数据是完全一样的。

二、        设置mysql主从配置的优点:

1、        解决web应用系统,数据库出现的性能瓶颈,采用数据库集群的方式来实现查询负载;一个系统中数据库的查询操作比更新操作要多得多,通过多台查询服务器将数据库的查询分担到不同的查询服务器上从而提高查询效率。

2、        Mysql数据库支持数据库的主从复制功能,使用主数据库进行数据的插入、删除与更新操作,而从数据库则专门用来进行数据查询操作,这样可以将更新操作和查询操作分担到不同的数据库上,从而提高了查询效率。

三、        主从数据库服务器的配置

1、        主数据库服务器的配置

(1)、修改mysql的配置文件(/etc/my.cnf)在配置文件中设置:

server-id       = 1   ###每一个数据库服务器都要制定一个唯一的server-id,通常主服务器制定为1。

log-bin=mysql-bin     ###mysql进行主从复制是通过二进制的日志文件来进行的,所以必须开启mysql的日志功能

(这个是/etc/my.cnf的默认配置,保持不变即可)

(2)、GRANT REPLICATION SLAVE ON *.* TO ' replication'@'172.28.3.41' IDENTIFIED BY 'koncept';        #####给主数据库服务器授予一个可以进行复制的用户,172.28.3.41为从服务器的IP,这样从服务器就能有钱先来访问主数据库服务器

2、从数据库服务器的设置

修改数据库配置文件/etc/my.cnf,配置如下内容:

   #server-id      = 1  ####必须把server-id      = 1注释掉,

   server-id       = 2  ####设置从的ID号

   master-host     =  172.28.3.43  #####设置主服务器的IP

   master-user     =   replication  #####设置连接主服务器的用户名

   master-password =   concept     #####设置连接主服务器的密码

   replicate-do-db=imtest0  ######设置你要同步的数据库,可以设置多个   

####就是我们前面建的用户名和密码,另外如果有端口号的变化还要配置端口

master-port     =   配置成你设置的端口就OK了!

3、分别重新启动主从服务器 #### 如果不重新启动主服务器在后面查看status的时候会出现问题!

4在从服务器上登录mysql,输入:show slave status/G  如果发现有:

  Slave_IO_Running: Yes

Slave_SQL_Running: Yes

  就说明已经成功了,如果这两个选项不全是Yes,那就说明你钱面的某个配置错了,

  我做的时候没有把主服务器重启,就出现  Slave_IO_Running: NO。重启后好了!

四、        监控服务器的状态

1、        监控主服务器的状态

可通过show master status来监控主服务器的状态,内容如下:

+------------------+----------+--------------+------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000003 |     1164 |              |                  |+------------------+----------+--------------+------------------+
Copy after login

 

#####其中File表示日志文件记录,Position表示日志文件的位置,这个也是数据库执行复制操作的必须标识,后面两字段表示复制的数据库名和不复制的数据库名,也可以在配置文件中你进行配置。

2、        监控从服务器的状态

可以通过:show slave status/G来查看,另外如果从数据库在复制的过程中出现问题,可以通过命令reset slave从数据库服务器复制的线程,从数据库服务器的通常操作命令有:

start slave;  ####启动复制线程

stop slave;  ####停止复制线程

reset slave;  ####重置复制线程

change master to; ###动态改变到主服务器的配置

 

bitsCN.com
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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is SOL coin? How does SOL coin work? What is SOL coin? How does SOL coin work? Mar 16, 2024 am 10:37 AM

Solana Blockchain and SOL Token Solana is a blockchain platform focused on providing high performance, security and scalability for decentralized applications (dApps). As the native asset of the Solana blockchain, SOL tokens are mainly used to pay transaction fees, pledge and participate in governance decisions. Solana’s unique features are its fast transaction confirmation times and high throughput, making it a favored choice among developers and users. Through SOL tokens, users can participate in various activities of the Solana ecosystem and jointly promote the development and progress of the platform. How Solana works Solana uses an innovative consensus mechanism called Proof of History (PoH) that is capable of efficiently processing thousands of transactions.

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

What is the architecture and working principle of Spring Data JPA? What is the architecture and working principle of Spring Data JPA? Apr 17, 2024 pm 02:48 PM

SpringDataJPA is based on the JPA architecture and interacts with the database through mapping, ORM and transaction management. Its repository provides CRUD operations, and derived queries simplify database access. Additionally, it uses lazy loading to only retrieve data when necessary, thus improving performance.

What is Polygon coin? How does Polygon coin work? What is Polygon coin? How does Polygon coin work? Mar 16, 2024 am 09:22 AM

Polygon: A multifunctional blockchain that builds the Ethereum ecosystem Polygon is a multifunctional blockchain platform built on Ethereum, formerly known as MaticNetwork. Its goal is to solve the scalability, high fees, and complexity issues in the Ethereum network. Polygon provides developers and users with a faster, cheaper, and simpler blockchain experience by providing scalability solutions. Here’s how Polygon works: Sidechain Network: Polygon creates a network of multiple sidechains. These sidechains run in parallel with the main Ethereum chain and can handle large volumes of transactions, thereby increasing overall network throughput. Plasma framework: Polygon utilizes the Plasma framework, which

What is VET coin? How does VET coin work? What is VET coin? How does VET coin work? Mar 16, 2024 am 11:40 AM

VET Coin: Blockchain-based IoT ecosystem VeChainThor (VET) is a platform based on blockchain technology that aims to enhance the Internet of Things (IoT) field by ensuring the credibility of data and enabling safe transfer of value. supply chain management and business processes. VET coin is the native token of the VeChainThor blockchain and has the following functions: Pay transaction fees: VET coins are used to pay transaction fees on the VeChainThor network, including data storage, smart contract execution and identity verification. Governance: VET token holders can participate in the governance of VeChainThor, including voting on platform upgrades and proposals. Incentives: VET coins are used to incentivize validators in the network to ensure the

What is SHIB coin? How does SHIB coin work? What is SHIB coin? How does SHIB coin work? Mar 17, 2024 am 08:49 AM

ShibaInu Coin: Dog-Inspired Cryptocurrency ShibaInu Coin (SHIB) is a decentralized cryptocurrency inspired by the iconic Shiba Inu emoji. The cryptocurrency was launched in August 2020 and aims to be an alternative to Dogecoin on the Ethereum network. Working Principle SHIB coin is a digital currency built on the Ethereum blockchain and complies with the ERC-20 token standard. It utilizes a decentralized consensus mechanism, Proof of Stake (PoS), which allows holders to stake their SHIB tokens to verify transactions and earn rewards for doing so. Key Features Huge supply: The initial supply of SHIB coins is 1,000 trillion coins, making it one of the largest cryptocurrencies in circulation. Low price: S

How to install PHP FFmpeg extension on server? How to install PHP FFmpeg extension on server? Mar 28, 2024 pm 02:39 PM

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe

What is Algorand coin? How does Algorand coin work? What is Algorand coin? How does Algorand coin work? Mar 17, 2024 am 08:30 AM

Algorand: A blockchain platform based on pure Byzantine consensus protocol Algorand is a blockchain platform built on pure Byzantine consensus protocol and aims to provide efficient, secure and scalable blockchain solutions. The platform was founded in 2017 by MIT professor Silvio Micali. Working Principle The core of Algorand lies in its unique pure Byzantine consensus protocol, the Algorand consensus. This protocol allows nodes to achieve consensus in a trustless environment, even if there are malicious nodes in the network. Algorand consensus achieves this goal through a series of steps. Key generation: Each node generates a pair of public and private keys. Proposal phase: A randomly selected node proposes a new zone

See all articles