Table of Contents
##Master-slave replication configuration" >##Master-slave replication configuration
Main server configuration" >Main server configuration
Create users and permissions" >Create users and permissions
is created, restart the mysql service through the command " > is created, restart the mysql service through the command
Check the status through the command show master status" > Check the status through the command show master status
slave slave server configuration" >slave slave server configuration
Restart the mysql service through the command" >Restart the mysql service through the command
View slave information" >View slave information
Test" >Test
Home Database Mysql Tutorial Implement MySQL master-slave replication

Implement MySQL master-slave replication

Aug 29, 2020 pm 04:45 PM
mysql master-slave replication

Implement MySQL master-slave replication

Related learning recommendations: mysql tutorial

The test server uses centos7.2 for relevant configuration

The article content refers to the master-slave synchronization of the mysql database to achieve read-write separation

Overview

Large-scale In order for a website to handle a large number of concurrent accesses, it is not enough to implement distributed load balancing on the website. When it comes to the data business layer and data access layer, if the traditional data structure is still used, or if only one server is used to handle so many database connection operations, the database will inevitably collapse, especially if the data is lost, the consequences will be unimaginable. At this time, we will consider how to reduce database connections, and let’s move on to today’s topic.

Use the master-slave database to separate reading and writing, thereby sharing the pressure on the master database. Deploy mysql on multiple servers, consider one of them as the master database, and the others as slave databases to achieve master-slave synchronization. The master database is responsible for active writing operations, while the slave database is only responsible for active reading operations (the slave database will still passively perform write operations in order to maintain data consistency), so that data loss can be avoided to a large extent. It can also reduce database connections and reduce the load on the main database.

Installing mysql

I refer to the following two articles to install mysql on the server

Centos7.2 Installation Mysql5.7 (Alibaba Cloud)

Centos7 cannot connect to the mysql database remotely

During the installation of the database, some pitfalls were encountered due to the inability to connect remotely. First, after configuring the database, database authorization is required to allow access, and then open the firewall settings and set 3306 The port is opened, allowing the database management tool to access the database through the port. I just kept running into trouble without setting up the firewall.

Mysql was installed on the two servers under test and the test database was imported at the same time

Modify the main server mysql configuration, the file is located in /etc/my.cnf

#在[mysqld]中添加:
server-id=1log_bin=master-bin
log_bin_index=master-bin.index
binlog_do_db=master

#server-id 服务器唯一标识。
#log_bin 启动MySQL二进制日志,即数据同步语句,从数据库会一条一条的执行这些语句。
#binlog_do_db 指定记录二进制日志的数据库,即需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可。
#binlog_ignore_db 指定不记录二进制日志的数据库,即不需要复制的数据库名,如果有多个数据库,重复设置这个选项即可。
#其中需要注意的是,binlog_do_db和binlog_ignore_db为互斥选项,一般只需要一个即可。
Copy after login

grant replication slave on

. to masterbackup@'%' identified by '123456';

% wildcard, indicating that any IP can access the master server. For formal environment, please configure the specified slave After the server IP

service mysql restart

Implement MySQL master-slave replication

The same modification is located in the /etc/my.cnf directory Configuration

#在[mysqld]中添加:
server-id=2relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
#replicate-do-db=master


#server-id 服务器唯一标识,如果有多个从服务器,每个服务器的server-id不能重复,跟IP一样是唯一标识,如果你没设置server-id或者设置为0,则从服务器不会连接到主服务器。
#relay-log 启动MySQL二进制日志,可以用来做数据备份和崩溃恢复,或主服务器挂掉了,将此从服务器作为其他从服务器的主服务器。
#replicate-do-db 指定同步的数据库,如果复制多个数据库,重复设置这个选项即可。若在master端不指定binlog-do-db,则在slave端可用replication-do-db来过滤。
#replicate-ignore-db 不需要同步的数据库,如果有多个数据库,重复设置这个选项即可。
#其中需要注意的是,replicate-do-db和replicate-ignore-db为互斥选项,一般只需要一个即可。
Copy after login

service mysql restart

Connect to the main database
#连接master主服务器
mysql> change master to master_host='103.246.246.225',master_port=3306,master_user='masterbackup',master_password='123456',master_log_file='master-bin.000001',master_log_pos=120;#master_host对应主服务器的IP地址。
#master_port对应主服务器的端口。
#master_log_file对应show master status显示的File列:master-bin.000001。
#master_log_pos对应show master status显示的Position列:120。
Copy after login

Enable slave synchronization data
#启动slave数据同步
mysql> start slave;#停止slave数据同步(若有需要)
mysql> stop slave;
Copy after login

View slave information through the command show slave status\G;

Implement MySQL master-slave replication

Slave_IO_Running and Slave_SQL_Running are both yes, which means the synchronization is successful.

Implement MySQL master-slave replication

Insert a piece of data into any data table

Implement MySQL master-slave replication

View the corresponding table from the slave database

Implement MySQL master-slave replication

You can see that the corresponding data has been successfully synchronized from the slave database! !

If you want to know more about programming learning, please pay attention to the

php training column!

The above is the detailed content of Implement MySQL master-slave replication. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

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

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: 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.

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 connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

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 start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles