MySQL支持单向、异步复制,一台作为Master服务器、多台作为Slave服务器。
MySQL主从复制通常采用单主星型结构,如A-->B、A-->C;当然,也可以采用链式结构,如A-->B-->C。
主服务器将更新写入二进制日志文件(log-bin),并传递给从服务器再执行一次。通常情况下,一旦主服务器的日志文件更新,从服务器就会立即启动复制,只要网络和硬件性能足够,这个延迟会非常小(趋于0是差不多就是同步了)。
主、从服务器均保持在线,允许同时对外提供服务,也相当于一种负载均衡(只读的)。
从服务器可以设置为只读,以避免误操作造成的不一致性,也因此可以将数据库查询、更新分离(A可读可写,B/C只读)。
##############################################################################
系统环境:
RHEL 5.5 [2.6.18-194.el5]
软件环境:
mysql-server-5.0.77-4.el5_4.2
mysql-5.0.77-4.el5_4.2 采用RHEL5自带的RPM包安装,主、从服务器都装上,默认的数据库目录位于 /var/lib/mysql/
##############################################################################
一、MySQL主服务器(server105,192.168.4.105) 1. 备份现有库 方法1,在线热备份:
[root@server105 ~]# mysqldump -u root -p --all-databases > /tmp/mysql-master.sql 方法2,二进制冷备:
[root@server105 ~]# service mysqld stop #//也可以不停止,改为锁定(mysql> FLUSH TABLES WITH READ LOCK;)
[root@server105 ~]# cd /var/lib/mysql/
[root@server105 mysql]# tar zcpvf /tmp/mysql-master.tar.gz ./
[root@server105 mysql]# service mysqld start
2. 修改my.cnf配置,重启mysqld服务
[root@server105 ~]# cp /etc/my.cnf /etc/my.cnf.origin
[root@server105 ~]# vim /etc/my.cnf
[mysqld]
.. ..
log-bin=mysql-bin #//使用二进制日志文件
server-id=1 #//指定服务器标识,参与复制的每台服务器不能相同
innodb_flush_log_at_trx_commit=1 #//提高InnoDB复制的耐受性、一致性
sync-binlog=1 #//启用日志同步
.. .. [root@server105 ~]# service mysqld restart
3. 创建复制账户
对于仅执行复制的账户,只要授予 REPLICATION SLAVE 权限就够了;
如果计划让从服务器使用 LOAD 方式导入主库(LOAD DATA FROM MASTER、LOAD TABLE FROM MASTER),还需要授予 SUPER、RELOAD 全局权限,以及对相关库表的 SELECT 权限。在执行 LOAD 操作时对于无 SELECT 权限的表将会被忽略到,从而易造成主从的不一致性。 [root@server105 ~]# mysql -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replicater'@'192.168.4.%' IDENTIFIED BY 'pwd @123 '; 或者 [root@server105 ~]# mysql -u root -p
mysql> GRANT REPLICATION SLAVE,SUPER,RELOAD,SELECT ON *.* TO 'replicater'@'192.168.4.%' IDENTIFIED BY 'pwd @123 ';
4. 确认主库状态
记下日志文件名(mysql-bin.000005)、偏移量(3586),从服务器首次复制时需要用到 mysql> SHOW MASTER STATUS;
+---------------- -+------------------+-------------------+----------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+------------------+-------------------+----------------------+
| mysql-bin.000005 | 3586 | | |
+---------------------+------------------+-------------------+----------------------+
1 row in set (0.00 sec) 二、MySQL从服务器(server205,192.168.4.205) 1. 导入现有库(主库的备份) 方法1,在线热导入:
[root@server205 ~]# scp 192.168.4.105:/tmp/mysql-master.sql /tmp/
[root@server205 ~]# mysql -u root -p [root@server205 ~]# service mysqld stop
[root@server205 ~]# rm -rf /var/lib/mysql/* #//此步骤可选
[root@server205 ~]# scp 192.168.4.105:/tmp/mysql-master.tar.gz /tmp/
[root@server205 ~]# tar zxpvf /tmp/mysql-master.tar.gz -C /var/lib/
[root@server205 ~]# service mysqld start
2. 修改my.cnf配置,重启mysqld服务
[root@server205 ~]# cp /etc/my.cnf /etc/my.cnf.origin
[root@server205 ~]# vim /etc/my.cnf
[mysqld]
.. ..
log-bin=mysql-bin #//使用二进制日志文件
server-id=2 #//指定服务器标识,参与复制的每台服务器不能相同
innodb_flush_log_at_trx_commit=1 #//提高InnoDB复制的耐受性、一致性
sync-binlog=1 #//启用日志同步 [root@server205 ~]# service mysqld restart
3. 启动从库复制 1)首次复制操作
[root@server205 ~]# mysql -u root -p
mysql> CHANGE MASTER TO MASTER_HOST='192.168.4.105',
MASTER_USER='replicater',
MASTER_PASSWORD='pwd @123 ',
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=3586; mysql> SLAVE START; #//启用从库复制 2)查看连接信息
[root@server205 ~]# head /var/lib/mysql/master.info #//连接信息会存于此文件,提供给mysqld服务调用
14
mysql-bin.000005
3586
192.168.4.105
replicater
pwd @123
3306
60
0
.. .. 3)确认复制状态
mysql> SHOW SLAVE STATUS/G
******************************* 1. row **********************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.105
Master_USER: replicater
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 3586
Relay_Log_File: mysqld-relay-bin-bin.000016
Relay_Log_Pos: 372
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes #//从服务器的IO线程已运行
Slave_SQL_Running: Yes #//从服务器的SQL线程已运行
.. ..
.. ..
Seconds_Behind_Master: 0 #//此项可反映主-->从复制延迟,0为最理想状态(无延迟)
1 row in set (0.00 sec)
4. 调整服务脚本,重启mysqld服务
通过修改start()函数的执行语句,可以调控mysqld服务的启动选项。例如:
--log-slave-updates=1 #//记录从库更新,以便允许链式复制
--read-only #//将从库设为只读,仅允许从服务器线程或具有SUPER权限的用户执行
--relay-log=mysqld-relay-bin #//使用固定的中继日志文件
--report-host=server205 #//报告给主服务器的主机名或IP地址
--slave_compressed_protocol=1 #//复制过程启用压缩,若启用此项,主、从服务器都应该添加
--replicate-do-db=mysql #//仅复制指定的库,其他库将被忽略,此选项可设置多条(省略时复制所有库)
--replicate-do-db=mytestdb
--replicate-ignore-db=test #//不复制(忽略)指定的库,此选项也可以设置多条,do和ignore用其中一种就可以了
--skip-slave-start #//服务器启动时跳过复制,需要手动 SLAVE START
--slave-net-timeout=60 #//当从服务器网络中断时,再次重试之前等待的时间(默认为60秒)
--master-connect-retry=60 #//当主服务器连接丢失时,再次重试之前等待的时间(默认为60秒)
.. .. [root@server205 ~]# vim /etc/init.d/mysqld
.. ..
sart() {
.. ..
EXTRA_ARGS="--log-slave-updates=1 --read-only --relay-log=mysqld-relay-bin --report-host=server205 --slave_compressed_protocol=1 --replicate-do-db=mysql -- replicate-do-db=mytestdb"
/usr/bin/mysqld_safe --datadir="$datadir" --socket="$socketfile" /
--log-error="$errlogfile" --pid-file="$mypidfile" /
--user=mysql $EXTRA_ARGS > /dev/null 2>&1 &
ret=$?
.. ..
}
.. .. [root@server205 ~]# service mysqld restart
三、验证主、从同步效果 1. 在主服务器上,执行更新操作
针对配置为执行复制的库,可以做建库/删库、建表/删表、添加/修改/删除表记录等各种更新操作。 2. 在从服务器上,观察数据库的变化
通过SELECT查询确认结果,与主服务器的数据库操作结果一致,几乎是实时的。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

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.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

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


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

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

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