MySQL主从复制(Master-Slave)
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数据库配置主从服务器实现双机热备

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

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 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.

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.

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.

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.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

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.
