Table of Contents
使用MySQL内建复制功能来最佳化可用性(二)
Home php教程 php手册 使用MySQL内建复制功能来最佳化可用性(二)

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

Jun 21, 2016 am 09:09 AM
connect mysql quot

mysql

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




第三步:创建相互的主从关系
  首先在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";
$primary = "10.1.1.1";
$backup = "10.1.1.2";

# attempt connection to primary
if(!$link_id = @mysql_connect($primary, $username, $password))
# attempt connection to secondary
$link_id = @mysql_connect($secondary, $username, $password)
return $link_id;
}

?>

  我在两种情况下对使用了上面技术的数据库连接建立过程进行了测试,一种是主MySQL服务程序关闭了,但是服务器还在运行,另一种情况是主服务器关闭了。如果只是mysqld关闭了,连接会马上转向备机;但是如果整个服务器关闭了,就出现了无限地等待(两分钟后我放弃了跟踪 -- 很短的注意跨度),因为PHP在查找一个不存在的服务器。不幸地是,不象fsockopen函数,mysql_connect函数没有一个超时参数,然而我们可以使用fsockopen来模拟一个超时处理。

第五步:一个改进的数据库连接程序

/********************************************************
function db_connect_plus()

returns a link identifier on success, or false on error
********************************************************/
function db_connect_plus(){
$username = "username";
$password = "password";
$primary = "10.1.1.1";
$backup = "10.1.1.2";
$timeout = 15; // timeout in seconds

if($fp = fsockopen($primary, 3306, &$errno, &$errstr, $timeout)){
fclose($fp);
return $link = mysql_connect($primary, $username, $password);
}
if($fp = fsockopen($secondary, 3306, &$errno, &$errstr, $timeout)){
fclose($fp);
return $link = mysql_connect($secondary, $username, $password);
}

return 0;
}

?>

  这个新改进的函数向我们提供了一个可调的超时特性,这正是mysql_connect函数所缺少的。如果连接立即失败,这种情况如机器"活"着,但mysqld"当"掉了,函数立即移到第二个服务器。上面的函数相当健壮,在试图进行连接之前先测试一下,查看服务程序是否在指定端口进行监听,让你的脚本在一段可接受的时间段后超时,允许你适当地对出错情况进行处理。如果你修改了缺省端口3306,请保证对端口号进行修改。

结论和意见
  首先,要确定得到了一个完整的数据快照。如果忘记拷贝一个表或数据库将导致备机线程序停止。生成快照的时刻是很关健的。你应该确保在拷贝数据文件之前二进制日志功能是无效的。如果在得到快照之前就允许了二进制日志功能,备机的线程可能会停止,原因就是当线程试图导入重要的记录时,可能会由于主键重复而停止。最好就是接照第二部分所讨论的处理办法来做:关闭-拷贝-允许二进制日志功能重启。

  你可能想要按照最初的一种方式来配制复制处理,并且在合适的时间关注备机,确保备机与主机保持同步。

  我没有测试过一个使用了复制特性的系统的负载平衡处理性能,但是我会灵活地使用这样系统来平衡插入和更新。例如,如果在两台服务器上两条记录都给出了同一个auto_increment值,这种情况备机线程会在哪一条记录上停掉呢?象这样的问题将会让负载平衡作为只读的处理,一台服务器处理所有的插入和更新,同时一组备机(是的,你可以有多个与主机分离的备机)处理所有的选择。

  我非常高兴,MySQL已经具备了复制系统的某些功能,并且配置很简单。使用它,你就可以开始针对失控的事件提供额外的安全措施了。我仅仅涉及了复制特性,这个我已经测试并且使用了,但是在MySQL的在线文档中的第11部分有中更详细的说明。



--------------------------------------------------------------------------------

译者的话:

  由于我原来使用的是3.22版的MySQL,所以为了测试一下我只好下载了3.23.24版的最新程序。而且因为只有一台机器,我只是增加了二进制日志的设置。不过,正如本文所说,的确有文件生成。如果大家对此感兴趣只好请自行测试了。另外,在最新的MySQL的使用手册中,我发现这个复制功能是在3.23.15版以后才有的,请大家检查自已的MySQL的版本。同时,文中关于二进制日志的设置是说在my.cnf中设置的。在我使用的3.23.24版本中,手册上说可以有三个文件进行参数设置,分别为windows目录下的my.ini文件,c:my.cnf和c:mysqldatamy.cnf中可以设置。我在设置'log-bin'时(不需要先设log参数)是使用mysql自带的
WinMySQLadmin软件进行设置的,并且在my.ini中设定的,与文中不同,请大家自行测试。

原作者:Michael  
来源:PHPBuilder.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 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

How to build a SQL database How to build a SQL database Apr 09, 2025 pm 04:24 PM

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.

See all articles