Detailed explanation of MySQL master-slave replication read-write separation construction

Detailed explanation of MySQL master-slave replication read-write separation construction

MySQL master-slave setup

MySQL master-slave replication and read-write separation settings are very simple:

Modify the configuration my.cnf file

The master and slave settings are similar:

[mysqld]
log-bin=mysql-bin 
server-id=222

log-bin=mysql-bin means: enable binary log.

server-id=222 means setting the unique ID of the server. The default value is 1, which is usually the last segment of the IP address. You can write it to something else as long as it is not repeated with other MySQL servers.

Here, some MySQL default my.cnf files reference all cnf files in the /etc/mysql/conf.d path. Therefore, you can also add a cnf file in the /etc/mysql/conf.d directory and add the above content

Restart mysql on the master and slave machines respectively

/etc/init.d/mysql restart

Create an account on the master and authorize the slave, and also make relevant configurations on the slave

Log in to the master and slave machines respectively as root user:

mysql -uroot -p //After entering, you will be asked to enter the root password. Entering the root password will log you in.

On the master machine

Create an account and authorize the slave machine. Pay attention to the username and password. You can set them at will, but be sure to write them down because the slave will need them later.

GRANT REPLICATION SLAVE ON *.* to 'ryugou'@'%' identified by 'ryugou';

Generally, the root account is not used. @'%' means that all clients can connect as long as the account and password are correct. The specific client IP address, such as 192.168.145.226, can be used here to enhance security.

On the slave machine

Check the master status

show master status;

Enter in the mysql terminal

Copy the code as follows:

change master to master_host='xxx.xxx.xxx.xxx',master_user='ryugou',master_password='ryugou',master_log_file='mysql-bin.000004',master_log_pos=120;

master_log_file is the corresponding content on the master machine, and so is master_log_pos. Pay attention to the content on the master status.

master_user and master_password are the username and password set during authorization on the master machine.

Start the slave server

start slave;

Check the status of the slave replication function

show slave status\G;

A list will appear

Mainly check that the status of Slave_IO_Running and Slave_SQL_Running must both be Yes.

The master creates the database and sets user access rights from the server

Create a database test on the master, and you will find that test is also created on the slave. Now you want to separate read and write. A user named gou on the slave machine can access it, but can only read.

grant select on test.* to gou;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Mysql database master-slave separation example code
  • MySQL master-slave synchronization, read-write separation configuration steps
  • Detailed explanation of MySQL master-slave replication, read-write separation, backup and recovery
  • Detailed explanation of the configuration method of MySQL master-slave replication read-write separation
  • Steps to configure MySQL master-slave separation under Linux system

<<:  Docker uses busybox to create a base image

>>:  Sample code for implementing login and registration template in Vue

Recommend

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the func...

How to set horizontal navigation structure in Html

This article shares with you two methods of setti...

How to set npm to load packages from multiple package sources at the same time

Table of contents 1. Build local storage 2. Creat...

Ubuntu Docker installation in vmware (container building)

1. Mind Map 2. How to build a container 2.1 Prepa...

Put frameset in body through iframe

Because frameset and body are on the same level, y...

CentOS7 uses rpm package to install mysql 5.7.18

illustrate This article was written on 2017-05-20...

Text mode in IE! Introduction to the role of DOCTYPE

After solving the form auto-fill problem discussed...

MySQL Packet for query is too large problem and solution

Problem description: Error message: Caused by: co...

Five guidelines to help you write maintainable CSS code

1. Add a comment block at the beginning of the sty...

What does the n after int(n) in MySQL mean?

You may already know that the length 1 of int(1) ...

How to install Windows Server 2008 R2 on Dell R720 server

Note: All pictures in this article are collected ...

Detailed explanation of the use cases of Vue listeners

The first one is to use jQuery's ajax to send...

Implementation of mysql configuration SSL certificate login

Table of contents Preface 1. MySQL enables SSL co...

js to achieve the effect of dragging the slider

This article shares the specific code of how to d...