Home php教程 php手册 使用MySQL内建复制功能来最佳化可用性

使用MySQL内建复制功能来最佳化可用性

Jun 13, 2016 am 10:07 AM
24 hours mysql use Function Can exist copy us

在Soundbreak我们天天24小时不间断之播放实况音频和视频,所以对于MySQL之新增之复制特性,我们不能做出很令人信服之测试。通过测试我们发现,可以使用这个特性来与备份数据库服务器保持数据同步,这样当主服务器因为某种原因处理失效时,能够使用备份机处理所有之查询。对于这样之要求,配置两台服务器并不困难。我将具体讨论整个处理过程,同时讨论一下当主服务器失效时,如何使用PHP来重定向查询。

  MySQL内部复制功能是建立在两个或两个以上服务器之间,通过设定它们之间之主-从关系来实现之。其中一个作为主服务器,其它之作为从服务器。我将具体讨论如何配置两台服务器,将一个设为主服务器,另一个设为从服务器。并且描述一下在它们之间进行切换之处理过程。我是在MySQL之3.23.23版本上进行之配置设置过程,并且也是在这个版本上进行之测试。MySQL开发人员建议最好使用最新版本,并且主-从服务器均使用相同之版本。同时MySQL 3.23版本仍然是beta测试版,而且这个版本可能不能向下兼容。所以因为这个原因,在实际之网站中,我现在还没有使用这个版本。拥有容错能力具有一个好处是,在不需中断任何查询之情况下,对服务器进行升级。

第一步:配置主服务器
  在这篇文章之剩下篇幅中,我将指定两台服务器。A(IP为10.1.1.1)作为主服务器(简称为主机)。B(IP为10.1.1.2)作为后备服务器(简称为备机)。

  MySQL之复制功能之实现过程为:备机(B)与主机(A)连接,然后读出主机之二进制更新日志,再将发生之变化合并到自已之数据库中。备机需要一个用户帐号来与主机连接,所以在主机上创建一个帐号,并只给它FILE权限,如下操作:

GRANT FILE ON *.* TO replicate@10.1.1.2 IDENTIFIED BY password;

  为了备机能够与主机连接,要在主机上运行FLUSH PRIVILEGES,不过不要担心,因为我们将在下面之步骤中停掉服务器。

  现在我们需要主机数据库之一个快照,并且对主机进行配置,答应生成二进制之更新日志。首先编辑my.cnf文件,以便答应二进制更新日志,所以在[mysqld]部分之下面某个之方增加一行:log-bin。在下一次服务器启动时,主机将生成二进制更新日志(名为:-bin.)。为了让二进制更新日志有效,关闭MySQL服务程序,然后将主机上之所有数据库目录到另一个目录中,接着重新启动mysqld。
请确定得到了所有数据库,否则在进行复制时,假如一个表在主机上存在但在备机上不存在,将因为出错而退出。现在你已经得到了数据之快照,和一个从建立快照以来之二进制日志,上面记录着任何对数据库之修改。请注重MySQL数据文件(*.MYD,*.MYI和*.frm)是依靠于文件系统之,所以你不能仅仅进行文件传输,如从Solaris到Linux。假如你处于一个异种之服务器环境,你将不得不使用mysqldump实用程序或其它之定制脚本来得到数据快照。

第二步:配置备机
  让我们继续。停掉备机上之MySQL服务程序,并且把从主机上拷贝来之数据库目录移到备机上之data目录下。请确认将目录之拥有者和属组改变为MySQL用户相应值,并且修改文件模式为660(只对拥有者和属组可读、可写),目录本身为770(只对拥有者和属组可读、可写和可执行)。

  继续。在备机上启动MySQL服务程序,确认MySQL工作正常。运行几个select查询(不要update或insert查询),看一看在第一步中得到之数据快照是否成功。接着,在测试成功后关掉MySQL服务程序。

  在备机上配置需要访问之主机,以便接收主机之更改。所以需要编辑务机上之my.cnf文件,在[mysqld]部分中增加下面几行:

master-host=10.1.1.1
master-user=replicate
master-password=password

  在启动备机服务程序后,备机服务程序将查看在my.cnf文件中所指定之主机,查看是否有改变,并且将这些改变合并到自已之数据库中。备机保持了主机之更新记录,这些记录是从主机之master.info文件中接收下来之。备机线程之状态可以通过sql命令SHOW SLAVE-STATUS看到。在备机上处理二进制日志中假如
发生错误,都将导致备机线程之退出,并且在*.err之日志文件中生成一条信息。然后错误可以被改正,接着可以使用sql语句SLAVE START来重新启动备机线程。线程将从主机二进制日志处理中断之之方继续处理。

  至此,在主机上所发生之数据改变应该已经复制到备机上了,要测试它,你可以在主机上插入或更新一条记录,而在备机上选择这条记录。

  现在我们拥有了从A机到B机之这种主-从关系,这样当A机可能当机之时候,答应我们将所有之查询重定向到B机上去,但是当A机恢复时,我们没有办法将发生之改变恢复到A机中去。为了解决这个问题,我们创建从B机到A机之主-从关系。

第三步:创建相互之主从关系
  首先在B机上之my.cnf文件中,在[mysqld]部分中加入log-bin,接着重新启动mysqld,然后创建可在它之上面执行复制功能之用户帐号,使用:

GRANT FILE ON *.* TO replicate@10.1.1.1 IDENTIFIED BY password;

  在B机上运行FLUSH PRIVILEGES命令,以便装入在加入复制用户后之新之授权表,接着回到A机上,在它之my.cnf中加入下面几行:

master-host=10.1.1.2
master-user=replicate
master-password=password

  在重启A机之服务程序之后,现在我们一拥有了在A机与B机之间之相互主-从关系。不管在哪个服务器上更新一条记录或插入一条记录,都将被复制到另一台服务器上。要注重之是:我不敢确定一个备机合并二进制日志变化之速度有多快,所以用这种方法来进行插入或更新语句之负载平衡可能不是一个好办法。

第四步:修改你之数据库连接程序
  既然你已经在A机和B机之间建立了一个相互之关系,你需要修改数据库连接程序,以便从这种方式中得到好处。下面之函数首先试图与A机连接,假如不能建立连接则与B机连接。


/********************************************************
function db_connect()



returns a link identifier on success, or false on error
********************************************************/
function db_connect(){
$username = "replUser";
$password = "password";

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1254
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

See all articles