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” 博客,,请务必保留此出处

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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.

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.

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.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.
