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

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