search
HomeDatabaseMysql Tutorial MySQL主从,双主,半同步及SSL复制

1.主从复制(主写从读)主服务器:1、改server-id配置文件中server-id=102、启用二进制日志与数据目录分离与权限log-bin=/tmp/binlogs/mysql-bin3、创建有复制权限

1. 主从复制 (主写从读) 主服务器: 1、改server-id 配置文件中 server-id = 10 2、启用二进制日志 与数据目录分离与权限 log-bin=/tmp/binlogs/mysql-bin 3、创建有复制权限的帐号 grant replication client, replication slave on *.* to repl@'172.16.%.%' identified by 'repl'; flush privileges; . 从服务器: 1、改server-id 配置文件中 server-id = 200 2、启用中继日志 relay-log=/tmp/relaylogs/relay-bin # 关闭二进制日志 3、连接主服务器 change master to master_host='172.16.43.200',master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=312; 4、启动复制线程 start slave; . 使用场景: 常见,适合于多重应用场景,从服务器位置的变化而变化 . 主从复制演进 --多从复制(演进多个从服务器,从服务器server-id一定要不一样) 使用场景: 1 台远程容灾 1 台关键本地主备 1 台开发测试学习之用 . 2. 双主复制 (互为主从,可读可写) 配置注意: service-id 不可相同 均开启二进制日志与中继日志 log-bin=/data/binlogs/mysql-bin relay-log=/data/relaylogs/relay-log 均授权复制账号给对方 grant replication client, replication slave on *.* to repl@'172.16.%.%' identified by 'repl'; flush privileges; 均连接对方服务器 change master to master_host='172.16.43.200',master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=312; 均启动复制线程 start salve; . *** 自动增长列的配置要隔开 auto-increment-offset = 1 auto-increment-increment = 2 *** 数据不一致时,需要手动同步(利用二进制) . 使用场景: 远程办公,两地职权相等 . 双主复制演进 --多源复制 -- mysql 5.6 多源需要加入不同的connection_name 例: change master 'connection_name' ... 多源复制操作需要在不同库之间操作 使用场景: 数据收集,中央集权式 . --环形复制 注意双主模型的配置即可 . 3. GTID主从 -- mysql 5.6+ MariaDB 10.0+ 配置过程 主服务器: 1. 配置文件中新增 log-slave-updates=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-threads=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 report-port=3306 report-host=master.king.com 2. 创建有复制权限的帐号 grant replication client, replication slave on *.* to repl@'172.16.%.%' identified by 'repl'; flush privileges; . 从服务器: 1. 配置文件中 与主服务器无异,唯一不同server-id 2. 连接主服务器 change master to master_host='172.16.43.200', master_port=3306, master_user='repl', master_use_gtid=cuurent_pos; 3. 启动从服务器 start slave; . 4. 半同步复制 (在一主多从的场景中,多从的一致复制状态回馈变成了不小的延迟 为了解决这个问题,出现了半同步的快速响应) . 主服务器: install plugin rpl_semi_sync_master soname 'semisync_master.so'; show global variables like '%semi%'; set global rpl_semi_sync_master_enabled=ON; set global rpl_semi_sync_master_timeout=1000; . 从服务器: install plugin rpl_semi_sync_slave soname 'semisync_slave.so'; set global rpl_semi_sync_slave_enabled=ON; stop slave; start slave; . 在主服务器验正半同步复制是否生效: show global status like '%semi%'; . 使用场景: 适合高并发快速响应用户的环境 . 5. 多级复制 (中间节点必须开启二进制日志与中继日志) . 多级复制演进 --多级分发复制 中间分发节点的库类型为blackhole,仅保存二进制,为其他从复制节点做分发 . 6. 安全复制 (主服务器(CA)开启需求验证,客户端发送由服务器颁发的证书) 主服务器: 1. 配置文件中加入 ssl-ca=/usr/local/mysql/ssl/cacert.pem ssl-cert=/usr/local/mysql/ssl/master.crt ssl-key=/usr/local/mysql/ssl/master.key 2. 配置文件注意修改 server-id 3. 登陆服务器并授权 grant replication client, replication slave on *.* to repl@'172.16.%.%' identified by 'repl' require ssl; flush privileges; . 从服务器: 1. 配置文件中 与主服务器无异,不同是server-id与证书信息 2. 连接服务器时指明证书信息 change master to master_host='172.16.43.200',master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=312, master_ssl=1, master_ssl_ca='/usr/local/mysql/ssl/cacert.pem',master_ssl_cert='/usr/local/mysql/ssl/slave.crt',master_ssl_key='/usr/local/mysql/ssl/slave.key'; 3. 启动服务器 start slave . 测试: 查看ssl状态在主从上 show slave status\G -> ssl这些项 使用mysql --ssl-ca=/path --ssl-cert=/path --ssl-key=/path信息登陆主服务器


本文出自 “Apprentice” 博客,,请务必保留此出处

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
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment