search
HomeDatabaseMysql TutorialMySQL主从同步复制 for Debian 操作系统

MySQL复制简述 MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。MySQL复制基

MySQL复制简述

MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新、删除等等)。因此,要进行复制,必须在主服务器上启用二进制日志。每个从服务器从主服务器接收主服务器上已经记录到其二进制日志的保存的更新。当一个从服务器连接主服务器时,它通知主服务器定位到从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,并在本机上执行相同的更新。然后封锁并等待主服务器通知新的更新。从服务器执行备份不会干扰主服务器,在备份过程中主服务器可以继续处理更新。

1、测试环境说明
主DB服务器IP地址:192.168.113.9
从DB服务器IP地址:192.168.113.8
System OS: Debian 5.0.7-32bit
MySQL Vsertion: 5.0.51a-24+lenny5-log (Debian)
#apt-get install mysql-server mysql-client
2、主DB服务器上操作说明
(1)创建同步用户名和被同步库
mysql> grant replication slave on *.* to 'repuser'@'192.168.113.8' identified by '111111';
mysql> flush privileges;

mysql> create database testdb;
mysql> use testdb;
mysql> create table testuser(id int(4),name varchar(20));
mysql> insert into testuser values(1,'test1');
mysql> flush privileges;
mysql> select * from testuser;
+------+-------+
| id   | name  |
+------+-------+
|    1 | test1 |
+------+-------+  1 row in set (0.00 sec)
 

(2)备份被同步数据库到从DB服务器上
mysql>flush tables with read lock;  //设置读锁
mysql>show master status;           //得到binlog日志文件名和偏移量
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |       98 | testdb       | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql>show master status;  //分行显示主数据库的状态
mysql>show master logs;   //显示主数据库日志
# mysqldump -uroot -p testdb >/home/testdb.sql //方法1:备份要同步的testdb数据库到从DB服务器上还原
#cd /var/lib/mysql                             //方法2:解压到从DB服务器/var/lib/mysql/
#tar zcvf testdb.tar.gz testdb/
#scp -22 testdb.tar.gz root@192.168.113.8:/home/ 
mysql> unlock tables;   //解锁
 

(3)配置主DB的my.cnf文件
#cd /etc/mysql
#cp my.cnf my.cnf_back
#vim my.cnf
bind-address            = 127.0.0.1  //找到后在其前添加 # 注释掉本行
//找到 #server-id 修改如下内容为:
server-id               = 1  //唯一标识,只要不和从服务器相同就行了
log_bin                 = /var/log/mysql/mysql-bin.log //必须开启这里,告诉主服务器以二进制日志,要mysql用户权限
expire_logs_days        = 10
max_binlog_size         = 100M
binlog_do_db            = testdb   //要同步的数据库,,多个数据库可用逗号分隔或者可写多行
binlog_ignore_db       = mysql    //忽略不同步的数据库,多可写多行
#/etc/init.d/mysql restart

3、从DB服务器上操作说明
(1)mysql -u repuser -p -h 192.168.113.1   //测试主DB上创建的repuser账号时候可以远程连接主DB
(2)配置从DB的my.cnf文件
#cd /etc/mysql
#cp my.cnf my.cnf_back
#vim my.cnf      //找到 #server-id 修改如下内容为:
server-id               = 2
master-host             = 192.168.113.9   //主DB服务器IP地址
master-user             = repuser
master-password         = 111111
master-port             = 3306
master-connect-retry    = 15
replicate-do-db         = testdb   //要同步的数据库,多个数据库可用逗号分隔或者可写多行
replicate-ignore-db     = mysql
log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
 

(3)调试从DB
#/etc/init.d/mysql restart
mysql> start slave; //启动复制线程,stop slave 停止复制线程
mysql> show processlist;
 State: Has read all relay log; waiting for the slave I/O thread to update it
 Info: NULL
 //表示slave已经连接上master,开始接受并执行日志
mysql> show slave status\G;   //分行显示复制线程状态
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
//查看以上两项的值,均为Yes则表示状态正常。

linux

Statement
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
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor