Home Database Mysql Tutorial mysql数据库的主从同步

mysql数据库的主从同步

Jun 07, 2016 pm 03:47 PM
mysql Synchronize backup database plan

数据库备份方案 mysql数据库的主从同步 一.实验环境部署 主服务器(mysqlmaster) IP:192.168.1.107 端口3306 从服务器(mysqlslave) IP: 192.168.1.127 端口3306 I. 主服务器的操作 1. 关于主服务器的相关配置 1.1 设置server-id并开启binlog参数 根据m

数据库备份方案

mysql数据库的主从同步

mysql数据库的主从同步

一.       实验环境部署

主服务器(mysql master) IP:192.168.1.107  端口3306

从服务器(mysql slave)  IP: 192.168.1.127  端口3306

I.    主服务器的操作

1.      关于主服务器的相关配置

1.1 设置server-id值并开启binlog参数

根据mysql的同步原理:关键因素就是binlog日志。

编辑/etc/my.cnf配置文件,修改和添加相关参数。

[root@localhost ~]# vi/etc/my.cnf

[mysqld]

server-id = 1

log-bin = mysql-bin

mysql数据库的主从同步

备注:

#. 上面两参数放在my.cnf中的[mysqld]模块下,否则会出错;

#. 要先在my.cnf文件中查找相关参数,并按具体要求修改,不存在时添加相关参数,切记,参数不能重复;

#. 修改my.cnf配置后需要重启数据库

命令为:/etc/init.d/mysqld restart,

修改完配置文件,检查配置后的结果:

[root@localhost ~]# grep -E"server-id|log-bin" /etc/my.cnf

mysql数据库的主从同步

重启mysql数据库

[root@localhost ~]#/etc/init.d/mysqld restart

1.2 建立用于主、从数据同步的帐号《rep》

[root@localhost ~]#mysql–uroot–pmyrootpw

Mysql>selectuser();

Mysql>grantreplication slave on *.* to rep@192.168.1.%identified by ‘123456’;

备注:

#replication slave:为mysql同步的必须权限,此处不要授权all

#*.*:表示所有库所有表,库也是可以指定具体的库和表进行复制,如test.test1(test库的test1表);

#binlog-do-db = test :需要备份数据,多个写多行,不写全部都备份
binlog-ignore-db= mysql :不需要备份的数据库,多个写多行

#rep@192.168.1.%:rep为同步账号,192.168.1.%为授权主机,使用了%表示允许整个192.168.1.0网段以rep用户访问;

#identified by "123456": 123456为密码,实际环境用复杂密码

查看用户权限

Mysql>showgrants forrep@192.168.1.127;

mysql数据库的主从同步

1.3   对主数据库锁表只读:

注:实际环境中,操作主从复制,需要申请停机时间,锁表会影响业务。

mysql>flush tables with read lock;

注:这个锁表命令的时间,在不同引擎的情况,会受下面参数的控制,锁表超过设置时间不操作会自动解锁;

默认情况下的时长为:

mysql>show variables like "%timeout%"; 可以查看到默认锁表时间最大值。

mysql数据库的主从同步

完成后测试是否锁表成功:打开另一窗口创建一test1表,是不会执行的,证明锁表不能更新,但可读,不可写,因为是read读锁,锁表主要是为了导出数据库文件,从而取得正确的偏移量的值,保证导入从数据库,数据一致。

1.4 查看主库状态

查看主库状态,即当前日志文件名和二进制日志偏移量

mysql>show master status;

命令显示的信息要记录在案,后面的从库复制时是从这个位置开始的。

mysql数据库的主从同步

1.5 导出主数据库数据

[root@localhost ~]#mkdir backup

[root@localhost ~]#mysqldump–uroot–pmyrootpw–A –B|gzip>backup/mysql_bak.$(date +%F)sql.gz

注:-A表示备份所有库, -B表示增加user DB和drop等参数(导库时会直接覆盖所有的)。

[root@localhost backup]# ll

mysql数据库的主从同步

为了确保导库期间,数据库没有数据插入,可以再检查下主库状态信息

[root@localhost backup]# mysql -uroot -pmyrootpw -e "show masterstatus"

注:无特殊情况,binlog文件及位置点是保持不变的。

导库后,解锁主库,恢复可写;

mysql>unlock tables;

特别提示,有读者这里犯迷糊,实际上做从库的,无论主库更新多少数据了,最后从库都会从上面show master status 的位置很快赶上主库的位置进度的。

1.6  把主库备份的mysql数据迁移到从库

[root@localhost ~]# scp backup/mysql_bak.2012-07-09.sql.gzroot@192.168.1.127:/backup

II.  从服务器的操作

1.    关于从服务器的相关配置

1.1 设置server-id值并关闭binlog设置

注:数据库的server-id在LAN内是唯一的,这里的server-id要和主库及其他从库不同,并注释掉从库的binlog参数配置;

编辑/etc/my.cnf配置文件,修改相关的参数设置

master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差

[root@localhost ~]# vi /etc/my.cnf

[mysqld]

server-id = 2

#log-bin = mysql-bin

检查配置后的结果

[root@localhost ~]#grep–E “server-id|log-bin” /etc/my.cnf

重启从数据库

[root@localhost ~]#/etc/init.d/mysqld restart

1.2 还原主库导出的数据到从库

解压主库备份的数据

[root@localhost backup]# ls

还原主库解压出的数据到从库

[root@localhost backup]#mysql–uroot–pmyrootpw

1.3 登录从库配置同步参数

mysql>change master to #连接主数据库

mysql>master_host=”192.168.1.107”, #主库的IP地址

mysql>master_port=3306, #主库的端口,从库的端口可以和主库不同

mysql>master_user=”rep”, #主库上建立的用于数据同步的用户《rep》

mysql>master_password=”123456”, #用户《rep》的密码mysql>master_log_file=”mysql-bin.000003”,  #是mysql>showmaster status时看到的二进制日志文件名称,不能多空格。mysql>master_log_pos=376213;  #是mysql>show master status时查看到的二进制日志偏移量,不能多空格。

1.4 启动从库同步开关

启动从库同步开关,并查看同步状态

[root@localhost backup]#mysql–uroot–pmyrootpw–e “start slave”

[root@localhost backup]#mysql–uroot–pmyrootpw–e “show slave status\G”

也可以登录从库,在数据库下面执行相关命令:

mysql>start slave;

mysql>show slave status\G;

判断搭建是否成功就看如下IO和SQL两个线程是否显示为“yes”状态

Slave_to_Running:YES #负责从库去主库读取binlog日志,并写入从库中继日志中

Slave_SQL_Running:YES #负责读取并执行中继日志中的binlog转换sql语句后应用到数据库汇总。

也可以执行命令过滤查看如下:

[root@localhost backup]# mysql -uroot -pmyrootpw -e"show slave status\G" | egrep "IO_Running|SQL_Running"

1.5 测试主从同步

在主库创建 —>数据库以及查看

在主库中创建库“mytable”用于主从同步:

[root@localhost]#mysql–uroot–pmyrootpw–e “show databases;

[root@localhost]#mysql–uroot–pmyrootpw–e “create database mytable;”

在从库查看是否主从同步:

[root@localhost]#mysql–uroot–pmyrootpw–e “show databases;

到此!主从数据库同步成功完成;从数据库可以实现数据同步。

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 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's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

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.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

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.

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.

See all articles