1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基
1 复制概述
Mysql内建的复制功能是构建大型,高性能应用程序的基础。将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重新执行一遍来实现的。复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。
请注意当你进行复制时,所有对复制中的表的更新必须在主服务器上进行。否则,你必须要小心,以避免用户对主服务器上的表进行的更新与对从服务器上的表所进行的更新之间的冲突。
1.1 mysql支持的复制类型:
(1)基于语句的复制:在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。一旦发现没法精确复制时,会自动选着基于行的复制。
(2)基于行的复制:把改变的内容复制过去,而不是把命令在从服务器上执行一遍. 从mysql5.0开始支持
(3)混合类型的复制: 默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。
1.2 复制解决的问题
MySQL复制技术有以下一些特点:
(1) 数据分布 (Data distribution )
(2) 负载平衡(load balancing)
(3) 备份(Backups)
(4) 高可用性和容错行 High availability and failover
1.3 复制如何工作
整体上来说,复制有3个步骤:
(1) master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
(2) slave将master的binary log events拷贝到它的中继日志(relay log);
(3) slave重做中继日志中的事件,将改变反映它自己的数据。
2 .复制配置
有两台MySQL数据库服务器Master和slave,Master为主服务器,slave为从服务器,初始状态时,Master和slave中的数据信息相同,当Master中的数据发生变化时,slave也跟着发生相应的变化,使得master和slave的数据信息同步,达到备份的目的。
要点:
负责在主、从服务器传输各种修改动作的媒介是主服务器的二进制变更日志,这个日志记载着需要传输给从服务器的各种修改动作。因此,主服务器必须激活二进制日志功能。从服务器必须具备足以让它连接主服务器并请求主服务器把二进制变更日志传输给它的权限。
一、安装MySQL
说明:在两台MySQL服务器192.168.21.169和192.168.21.168上分别进行如下操作,安装MySQL 5.5.22
二、配置MySQL主服务器(192.168.21.169)
mysql -u root -p #进入MySQL控制台 create database AMT_DB; #建立数据库AMT_DB insert into mysql.user(Host,User,Password) values ('localhost','archermind',password('123456')); #创建用户archermind,建立MySQL主从数据库同步用户replication密码123456 flush privileges; #刷新系统授权表 #授权用户replication只能从192.168.21.168这个IP访问主服务器192.168.21.169上面的数据库,并且只具有数据库备份的权限 grant replication slave on *.* to 'replication'@'192.168.21.168' identified by '123456' with grant option;
三、把MySQL主服务器192.168.21.169中的数据库AMT_DB导入到MySQL从服务器192.168.21.168中
1、导出数据库AMT_DB
mysqldump -u root -p AMT_DB > /home/replication.sql #在MySQL主服务器进行操作,导出数据库AMT_DB到/home/replication.sql
备注:在导出之前可以先进入MySQL控制台执行下面命令
flush tables with read lock; #数据库只读锁定命令,防止导出数据库的时候有数据写入 unlock tables; #解除锁定
2、导入数据库到MySQL从服务器
mysql -u root -p #进入从服务器MySQL控制台 create database AMT_DB; #创建数据库 use AMT_DB #进入数据库 source /home/replication.sql #导入备份文件到数据库 mysql -u replication -h 192.168.21.169 -p #测试在从服务器上登录到主服务器
四、配置MySQL主服务器的my.cnf文件
#vim /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容 server-id=1 #设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。 log_bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。 binlog-do-db=AMT_DB #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 binlog-ignore-db=mysql #不同步mysql系统数据库 #service mysqld restart #重启MySQL
mysql -u root -p #进入mysql控制台 show master status; 查看主服务器,出现以下类似信息 +------------------+----------+--------------+------------------+ |File |Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 7131 | AMT_DB |mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
注意:这里记住File的值:mysql-bin.000001和Position的值:7131,后面会用到。
五、配置MySQL从服务器的my.cnf文件
#vim /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容 server-id=2 #配置文件中已经有一行server-id=1,修改其值为2,表示为从数据库 log-bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。 replicate-do-db=AMT_DB #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 replicate-ignore-db=mysql #不同步mysql系统数据库 :wq! #保存退出 #service mysqld restart #重启MySQL
注意:MySQL 5.1.7版本之后,已经不支持把master配置属性写入my.cnf配置文件中了,只需要把同步的数据库和要忽略的数据库写入即可。
mysql -u root -p #进入MySQL控制台 slave stop; #停止slave同步进程 change master to master_host='192.168.21.169',master_user='replication',master_password='123456',master_log_file='mysql-bin.000001' ,master_log_pos=7131; #执行同步语句 slave start; #开启slave同步进程 SHOW SLAVE STATUS\G #查看slave同步信息,出现以下内容 *************************** 1. row *************************** Slave_IO_State:Waiting for master to send event Master_Host:192.168.21.169 Master_User:replication Master_Port:3306 Connect_Retry:60 Master_Log_File:mysql-bin.000001 Read_Master_Log_Pos:7131 Relay_Log_File:MySQLSlave-relay-bin.000001 Relay_Log_Pos:253 Relay_Master_Log_File:mysql-bin.000001 Slave_IO_Running:Yes Slave_SQL_Running:Yes Replicate_Do_DB:AMT_DB Replicate_Ignore_DB:mysql Replicate_Do_Table: Replicate_Ignore_Table: 1 row in set (0.00 sec)
注意查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上这两个参数的值为Yes,即说明配置成功!
六、测试MySQL主从服务器双机热备是否成功
1、进入MySQL主服务器
mysql -u root-p #进入主服务器MySQL控制台 use AMT_DB #进入数据库 CREATETABLE test ( id int not null primary key,name char(20)); #创建test
2、进入MySQL从服务器
mysql -u root-p #进入MySQL控制台 use AMT_DB #进入数据库 show tables; #查看AMT_DB表结构,会看到有一个新建的表test,表示数据库同步成功
至此,MySQL数据库配置主从服务器实现双机热备

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.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

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.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function